-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get calico blockaffinity by etcd
Signed-off-by: renxiangyu_yewu <[email protected]>
- Loading branch information
renxiangyu_yewu
committed
Feb 8, 2025
1 parent
c19942f
commit fdfcbab
Showing
6 changed files
with
244 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
pkg/clusterlink/controllers/nodecidr/adaper/calico_etcd_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package adaper | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"crypto/tls" | ||
"crypto/x509" | ||
"github.com/projectcalico/calico/libcalico-go/lib/apiconfig" | ||
"github.com/projectcalico/calico/libcalico-go/lib/backend/api" | ||
"github.com/projectcalico/calico/libcalico-go/lib/backend/etcdv3" | ||
"github.com/projectcalico/calico/libcalico-go/lib/backend/model" | ||
"github.com/projectcalico/calico/libcalico-go/lib/net" | ||
"github.com/stretchr/testify/assert" | ||
clientv3 "go.etcd.io/etcd/client/v3" | ||
) | ||
|
||
func TestGetCIDRByNodeName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
nodeName string | ||
expectCidrs []string | ||
}{ | ||
{ | ||
name: "test1", | ||
nodeName: "nodeName", | ||
expectCidrs: []string{"10.xxx.xx.0/26"}, | ||
}, | ||
{ | ||
name: "test1", | ||
nodeName: "no-nodeName", | ||
expectCidrs: nil, | ||
}, | ||
} | ||
etcdClient, err := createEtcdClient() | ||
if err != nil { | ||
t.Errorf("connect to etcd failed, err:%v\n", err) | ||
} | ||
adapter := &CalicoETCDAdapter{ | ||
etcdClient: etcdClient, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cidrs, err := adapter.GetCIDRByNodeName(tt.nodeName) | ||
if err != nil { | ||
t.Errorf("get cidr by node name failed, err:%v\n", err) | ||
} | ||
assert.Equal(t, tt.expectCidrs, cidrs) | ||
}) | ||
} | ||
} | ||
|
||
func createEtcdClient() (api.Client, error) { | ||
etcdConfig := apiconfig.EtcdConfig{ | ||
EtcdEndpoints: "127.0.0.1:2379", | ||
EtcdKeyFile: "/ssl/server.key", | ||
EtcdCertFile: "/ssl/server.crt", | ||
EtcdCACertFile: "/ssl/ca.crt", | ||
} | ||
etcdV3Client, err := etcdv3.NewEtcdV3Client(&etcdConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return etcdV3Client, nil | ||
} | ||
|
||
func TestEtcdV3Client(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cidr string | ||
nodeName string | ||
expectValue model.BlockAffinity | ||
}{ | ||
{ | ||
name: "test1", | ||
cidr: "10.222.42.0/26", | ||
nodeName: "node1", | ||
expectValue: model.BlockAffinity{ | ||
State: "confirmed", | ||
Deleted: false, | ||
}, | ||
}, | ||
} | ||
|
||
cli, err := createEtcdClient() | ||
if err != nil { | ||
t.Errorf("connect to etcd failed, err:%v\n", err) | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
_, cidr, err := net.ParseCIDR(test.cidr) | ||
if err != nil { | ||
t.Fatalf("Failed to parse CIDR: %v", err) | ||
} | ||
key := model.BlockAffinityKey{ | ||
Host: test.nodeName, | ||
CIDR: *cidr, | ||
} | ||
get, err := cli.Get(ctx, key, "") | ||
if err != nil { | ||
fmt.Printf("create from etcd failed, err:%v\n", err) | ||
return | ||
} | ||
value, ok := get.Value.(*model.BlockAffinity) | ||
if !ok { | ||
t.Errorf("Value is not BlockAffinity, err:%v\n", err) | ||
} | ||
assert.Equal(t, test.expectValue.State, value.State) | ||
assert.Equal(t, test.expectValue.Deleted, value.Deleted) | ||
}) | ||
} | ||
|
||
cancel() | ||
} | ||
|
||
func TestEtcdClient(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
key string | ||
expectValue []byte | ||
}{ | ||
{ | ||
name: "test1", | ||
key: "/calico/ipam/v2/host/nodeName/ipv4/block/10.xxx.xx.0-26", | ||
expectValue: []byte(`{"state":"confirmed","deleted":false}`), | ||
}, | ||
} | ||
|
||
cfg := struct { | ||
EtcdEndpoints string | ||
certFile string | ||
keyFile string | ||
caFile string | ||
}{ | ||
EtcdEndpoints: "127.0.0.1:2379", | ||
certFile: "/ssl/server.crt", | ||
keyFile: "/ssl/server.key", | ||
caFile: "/ssl/ca.crt", | ||
} | ||
tlsConfig, err := tls.LoadX509KeyPair(cfg.certFile, cfg.keyFile) | ||
if err != nil { | ||
fmt.Printf("load tls cert failed, err:%v\n", err) | ||
return | ||
} | ||
caCert, err := os.ReadFile(cfg.caFile) | ||
if err != nil { | ||
fmt.Printf("read ca cert failed, err:%v\n", err) | ||
return | ||
} | ||
caCertPool := x509.NewCertPool() | ||
caCertPool.AppendCertsFromPEM(caCert) | ||
tlsCfg := &tls.Config{ | ||
Certificates: []tls.Certificate{tlsConfig}, | ||
RootCAs: caCertPool, | ||
} | ||
|
||
cli, err := clientv3.New(clientv3.Config{ | ||
Endpoints: []string{cfg.EtcdEndpoints}, | ||
DialTimeout: 5 * time.Second, | ||
TLS: tlsCfg, | ||
}) | ||
if err != nil { | ||
fmt.Printf("connect to etcd failed, err:%v\n", err) | ||
return | ||
} | ||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
resp, err := cli.Get(ctx, test.key) | ||
if err != nil { | ||
fmt.Printf("get from etcd failed, err:%v\n", err) | ||
return | ||
} | ||
for _, ev := range resp.Kvs { | ||
assert.Equal(t, test.expectValue, ev.Value) | ||
} | ||
}) | ||
} | ||
cancel() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters