From 6af5cdcb6d1ad185c84e6c438151c88454eb0003 Mon Sep 17 00:00:00 2001 From: OrangeBao Date: Sun, 12 Nov 2023 12:56:07 +0800 Subject: [PATCH] feat: add address for knode Signed-off-by: OrangeBao --- .../cluster-manager/cluster_controller.go | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/pkg/clustertree/cluster-manager/cluster_controller.go b/pkg/clustertree/cluster-manager/cluster_controller.go index d03b5adbe..5987a3e7e 100644 --- a/pkg/clustertree/cluster-manager/cluster_controller.go +++ b/pkg/clustertree/cluster-manager/cluster_controller.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "sort" "strings" "sync" "time" @@ -313,6 +314,77 @@ func (c *ClusterController) setupStorageControllers(mgr manager.Manager, nodes [ return nil } +func (c *ClusterController) setNodeAddr(ctx context.Context, nodeName string, leafClient kubernetes.Interface, node *corev1.Node) error { + leafnodes, err := leafClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{ + // TODO: LabelSelector + }) + if err != nil { + klog.Errorf("create node %s failed, cannot get node from leaf cluster, err: %v", nodeName, err) + return err + } + + if len(leafnodes.Items) == 0 { + klog.Errorf("create node %s failed, cannot get node from leaf cluster, len of leafnodes is 0", nodeName) + return err + } + + rootnodes, err := c.RootClient.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) + + if err != nil { + klog.Errorf("create node %s failed, cannot get node from root cluster, err: %v", nodeName, err) + return err + } + + if len(rootnodes.Items) == 0 { + klog.Errorf("create node %s failed, cannot get node from root cluster, len of leafnodes is 0", nodeName) + return err + } + + isIPv4First := true + for _, addr := range rootnodes.Items[0].Status.Addresses { + if addr.Type == corev1.NodeInternalIP { + if utils.IsIPv6(addr.Address) { + isIPv4First = false + } + break + } + } + + node.Status.Addresses = []corev1.NodeAddress{} + + address := []corev1.NodeAddress{} + + for _, addr := range leafnodes.Items[0].Status.Addresses { + if addr.Type == corev1.NodeInternalIP { + address = append(address, corev1.NodeAddress{Type: corev1.NodeInternalIP, Address: addr.Address}) + } + } + + sort.Slice(address, func(i, j int) bool { + if isIPv4First { + if !utils.IsIPv6(address[i].Address) && utils.IsIPv6(address[j].Address) { + return true + } + if utils.IsIPv6(address[i].Address) && !utils.IsIPv6(address[j].Address) { + return false + } + return true + } else { + if !utils.IsIPv6(address[i].Address) && utils.IsIPv6(address[j].Address) { + return false + } + if utils.IsIPv6(address[i].Address) && !utils.IsIPv6(address[j].Address) { + return true + } + return true + } + }) + + node.Status.Addresses = address + + return nil +} + func (c *ClusterController) createNode(ctx context.Context, cluster *kosmosv1alpha1.Cluster, leafClient kubernetes.Interface) ([]*corev1.Node, error) { getNodeLen := func(cluster *kosmosv1alpha1.Cluster) int32 { if cluster.Spec.ClusterTreeOptions.Enable { @@ -342,6 +414,11 @@ func (c *ClusterController) createNode(ctx context.Context, cluster *kosmosv1alp Port: c.Options.ListenPort, }, } + + if err := c.setNodeAddr(ctx, nodeName, leafClient, node); err != nil { + return nil, err + } + node, err = c.RootClient.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{}) if err != nil && !errors.IsAlreadyExists(err) { klog.Errorf("create node %s failed, err: %v", nodeName, err)