Skip to content

Commit f1cb94c

Browse files
committed
feat: add the clusters and clusternodes validator
Signed-off-by: wangyizhi1 <[email protected]>
1 parent 2d0b9bb commit f1cb94c

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

Diff for: pkg/clusterlink/network-manager/controller.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,19 @@ func (c *Controller) Reconcile(ctx context.Context, request reconcile.Request) (
123123
}
124124

125125
str := c.NetworkManager.GetConfigsString()
126-
klog.V(4).Infof(str)
126+
klog.V(6).Infof(str)
127+
128+
// clear nodeConfigs
129+
for i, nc := range nodeConfigList.Items {
130+
if _, ok := nodeConfigs[nc.Name]; !ok {
131+
err = c.Delete(ctx, &nodeConfigList.Items[i])
132+
if err != nil {
133+
klog.Warningf("failed to delete nodeConfig %s, err: %v", nc.Name, err)
134+
continue
135+
}
136+
klog.Infof("nodeConfig %s has been deleted", nc.Name)
137+
}
138+
}
127139

128140
for nodeName, config := range nodeConfigs {
129141
nc := &clusterlinkv1alpha1.NodeConfig{

Diff for: pkg/clusterlink/network-manager/helpers/filter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Filter struct {
1313
clustersMap map[string]*v1alpha1.Cluster
1414
}
1515

16-
func NewFilter(clusterNodes []v1alpha1.ClusterNode, clusters []v1alpha1.Cluster, nodeConfigs []v1alpha1.NodeConfig) *Filter {
16+
func NewFilter(clusters []v1alpha1.Cluster, clusterNodes []v1alpha1.ClusterNode, nodeConfigs []v1alpha1.NodeConfig) *Filter {
1717
cm := buildClustersMap(clusters)
1818
cs := convertToPointerSlice(clusters)
1919
cns := convertToPointerSlice(clusterNodes)

Diff for: pkg/clusterlink/network-manager/network_manager.go

+47-1
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,55 @@ func NewManager() *Manager {
1919
return &Manager{}
2020
}
2121

22+
// ExcludeInvalidItems Verify whether clusterNodes and clusters are valid and give instructions
23+
func ExcludeInvalidItems(clusters []v1alpha1.Cluster, clusterNodes []v1alpha1.ClusterNode) (cs []v1alpha1.Cluster, cns []v1alpha1.ClusterNode) {
24+
klog.Infof("Start verifying clusterNodes and clusters")
25+
clustersMap := map[string]v1alpha1.Cluster{}
26+
for _, c := range clusters {
27+
if c.Spec.ClusterLinkOptions == nil {
28+
klog.Infof("the cluster %s's ClusterLinkOptions is empty, will exclude.", c.Name)
29+
continue
30+
}
31+
clustersMap[c.Name] = c
32+
cs = append(cs, c)
33+
}
34+
35+
for _, cn := range clusterNodes {
36+
if len(cn.Spec.ClusterName) == 0 {
37+
klog.Infof("the clusterNode %s's clusterName is empty, will exclude.", cn.Name)
38+
continue
39+
}
40+
if len(cn.Spec.InterfaceName) == 0 {
41+
klog.Infof("the clusterNode %s's interfaceName is empty, will exclude.", cn.Name)
42+
continue
43+
}
44+
45+
if _, ok := clustersMap[cn.Spec.ClusterName]; !ok {
46+
klog.Infof("the cluster which clusterNode %s belongs to does not exist, or the cluster lacks the spec.clusterLinkOptions configuration.", cn.Name)
47+
continue
48+
}
49+
50+
c := clustersMap[cn.Spec.ClusterName]
51+
supportIPv4 := c.Spec.ClusterLinkOptions.IPFamily == v1alpha1.IPFamilyTypeALL || c.Spec.ClusterLinkOptions.IPFamily == v1alpha1.IPFamilyTypeIPV4
52+
supportIPv6 := c.Spec.ClusterLinkOptions.IPFamily == v1alpha1.IPFamilyTypeALL || c.Spec.ClusterLinkOptions.IPFamily == v1alpha1.IPFamilyTypeIPV6
53+
if supportIPv4 && len(cn.Spec.IP) == 0 {
54+
klog.Infof("the clusterNode %s's ip is empty, but cluster's ipFamily is %s", cn.Name, c.Spec.ClusterLinkOptions.IPFamily)
55+
continue
56+
}
57+
if supportIPv6 && len(cn.Spec.IP6) == 0 {
58+
klog.Infof("the clusterNode %s's ip6 is empty, but cluster's ipFamily is %s", cn.Name, c.Spec.ClusterLinkOptions.IPFamily)
59+
continue
60+
}
61+
62+
cns = append(cns, cn)
63+
}
64+
return
65+
}
66+
2267
// CalculateNetworkConfigs Calculate the network configuration required for each node
2368
func (n *Manager) CalculateNetworkConfigs(clusters []v1alpha1.Cluster, clusterNodes []v1alpha1.ClusterNode, nodeConfigs []v1alpha1.NodeConfig) (map[string]*handlers.NodeConfig, error) {
24-
filter := helpers.NewFilter(clusterNodes, clusters, nodeConfigs)
69+
cs, cns := ExcludeInvalidItems(clusters, clusterNodes)
70+
filter := helpers.NewFilter(cs, cns, nodeConfigs)
2571

2672
c := &handlers.Context{
2773
Filter: filter,

0 commit comments

Comments
 (0)