Skip to content

Commit

Permalink
feat: add the clusters and clusternodes validator
Browse files Browse the repository at this point in the history
Signed-off-by: wangyizhi1 <[email protected]>
  • Loading branch information
wangyizhi1 committed Nov 28, 2023
1 parent 2d0b9bb commit 119e8b3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
14 changes: 13 additions & 1 deletion pkg/clusterlink/network-manager/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/clusterlink/network-manager/helpers/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 47 additions & 1 deletion pkg/clusterlink/network-manager/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 119e8b3

Please sign in to comment.