diff --git a/pkg/clusterlink/network-manager/controller.go b/pkg/clusterlink/network-manager/controller.go index 7bde7592c..50de9a4c9 100644 --- a/pkg/clusterlink/network-manager/controller.go +++ b/pkg/clusterlink/network-manager/controller.go @@ -123,7 +123,19 @@ func (c *Controller) Reconcile(ctx context.Context, request reconcile.Request) ( } str := c.NetworkManager.GetConfigsString() - klog.V(4).Infof(str) + klog.V(6).Infof("the nodeConfigs of this round of calculations: %s", str) + + // clear nodeConfigs + for i, nc := range nodeConfigList.Items { + if _, ok := nodeConfigs[nc.Name]; !ok { + err = c.Delete(ctx, &nodeConfigList.Items[i]) + if err != nil { + klog.Warningf("failed to delete nodeConfig %s, err: %v", nc.Name, err) + continue + } + klog.Infof("nodeConfig %s has been deleted", nc.Name) + } + } for nodeName, config := range nodeConfigs { nc := &clusterlinkv1alpha1.NodeConfig{ diff --git a/pkg/clusterlink/network-manager/helpers/filter.go b/pkg/clusterlink/network-manager/helpers/filter.go index 071a5746b..7752edf73 100644 --- a/pkg/clusterlink/network-manager/helpers/filter.go +++ b/pkg/clusterlink/network-manager/helpers/filter.go @@ -13,7 +13,7 @@ type Filter struct { clustersMap map[string]*v1alpha1.Cluster } -func NewFilter(clusterNodes []v1alpha1.ClusterNode, clusters []v1alpha1.Cluster, nodeConfigs []v1alpha1.NodeConfig) *Filter { +func NewFilter(clusters []v1alpha1.Cluster, clusterNodes []v1alpha1.ClusterNode, nodeConfigs []v1alpha1.NodeConfig) *Filter { cm := buildClustersMap(clusters) cs := convertToPointerSlice(clusters) cns := convertToPointerSlice(clusterNodes) diff --git a/pkg/clusterlink/network-manager/network_manager.go b/pkg/clusterlink/network-manager/network_manager.go index 55fbfa76d..e2d8a9064 100644 --- a/pkg/clusterlink/network-manager/network_manager.go +++ b/pkg/clusterlink/network-manager/network_manager.go @@ -19,9 +19,55 @@ func NewManager() *Manager { return &Manager{} } +// ExcludeInvalidItems Verify whether clusterNodes and clusters are valid and give instructions +func ExcludeInvalidItems(clusters []v1alpha1.Cluster, clusterNodes []v1alpha1.ClusterNode) (cs []v1alpha1.Cluster, cns []v1alpha1.ClusterNode) { + klog.Infof("Start verifying clusterNodes and clusters") + clustersMap := map[string]v1alpha1.Cluster{} + for _, c := range clusters { + if c.Spec.ClusterLinkOptions == nil { + klog.Infof("the cluster %s's ClusterLinkOptions is empty, will exclude.", c.Name) + continue + } + clustersMap[c.Name] = c + cs = append(cs, c) + } + + for _, cn := range clusterNodes { + if len(cn.Spec.ClusterName) == 0 { + klog.Infof("the clusterNode %s's clusterName is empty, will exclude.", cn.Name) + continue + } + if len(cn.Spec.InterfaceName) == 0 { + klog.Infof("the clusterNode %s's interfaceName is empty, will exclude.", cn.Name) + continue + } + + if _, ok := clustersMap[cn.Spec.ClusterName]; !ok { + klog.Infof("the cluster which clusterNode %s belongs to does not exist, or the cluster lacks the spec.clusterLinkOptions configuration.", cn.Name) + continue + } + + c := clustersMap[cn.Spec.ClusterName] + supportIPv4 := c.Spec.ClusterLinkOptions.IPFamily == v1alpha1.IPFamilyTypeALL || c.Spec.ClusterLinkOptions.IPFamily == v1alpha1.IPFamilyTypeIPV4 + supportIPv6 := c.Spec.ClusterLinkOptions.IPFamily == v1alpha1.IPFamilyTypeALL || c.Spec.ClusterLinkOptions.IPFamily == v1alpha1.IPFamilyTypeIPV6 + if supportIPv4 && len(cn.Spec.IP) == 0 { + klog.Infof("the clusterNode %s's ip is empty, but cluster's ipFamily is %s", cn.Name, c.Spec.ClusterLinkOptions.IPFamily) + continue + } + if supportIPv6 && len(cn.Spec.IP6) == 0 { + klog.Infof("the clusterNode %s's ip6 is empty, but cluster's ipFamily is %s", cn.Name, c.Spec.ClusterLinkOptions.IPFamily) + continue + } + + cns = append(cns, cn) + } + return +} + // CalculateNetworkConfigs Calculate the network configuration required for each node func (n *Manager) CalculateNetworkConfigs(clusters []v1alpha1.Cluster, clusterNodes []v1alpha1.ClusterNode, nodeConfigs []v1alpha1.NodeConfig) (map[string]*handlers.NodeConfig, error) { - filter := helpers.NewFilter(clusterNodes, clusters, nodeConfigs) + cs, cns := ExcludeInvalidItems(clusters, clusterNodes) + filter := helpers.NewFilter(cs, cns, nodeConfigs) c := &handlers.Context{ Filter: filter,