From bda6aa31201118a21e8557949aa72fbc47775dc9 Mon Sep 17 00:00:00 2001 From: duanmengkk Date: Wed, 15 Nov 2023 20:33:53 +0800 Subject: [PATCH] fix: fix node is not found when a cluster is joined Signed-off-by: duanmengkk --- .../cluster-manager/cluster_controller.go | 2 +- .../utils/leaf_model_handler.go | 83 ++++++++----------- 2 files changed, 36 insertions(+), 49 deletions(-) diff --git a/pkg/clustertree/cluster-manager/cluster_controller.go b/pkg/clustertree/cluster-manager/cluster_controller.go index 536767dd0..2b42f940a 100644 --- a/pkg/clustertree/cluster-manager/cluster_controller.go +++ b/pkg/clustertree/cluster-manager/cluster_controller.go @@ -192,7 +192,7 @@ func (c *ClusterController) Reconcile(ctx context.Context, request reconcile.Req nodes, err := c.createNode(ctx, cluster, leafClient) if err != nil { - return reconcile.Result{}, fmt.Errorf("create node with err %v, cluster %s", err, cluster.Name) + return reconcile.Result{RequeueAfter: RequeueTime}, fmt.Errorf("create node with err %v, cluster %s", err, cluster.Name) } // TODO @wyz for _, node := range nodes { diff --git a/pkg/clustertree/cluster-manager/utils/leaf_model_handler.go b/pkg/clustertree/cluster-manager/utils/leaf_model_handler.go index 1115dc163..7bdc98a3f 100644 --- a/pkg/clustertree/cluster-manager/utils/leaf_model_handler.go +++ b/pkg/clustertree/cluster-manager/utils/leaf_model_handler.go @@ -59,29 +59,23 @@ func (h AggregationModelHandler) CreateNodeInRoot(ctx context.Context, cluster * nodeName := fmt.Sprintf("%s%s", utils.KosmosNodePrefix, cluster.Name) node, err := h.RootClientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{}) if err != nil { - if errors.IsNotFound(err) { - node = utils.BuildNodeTemplate(nodeName) - node.Status.NodeInfo.KubeletVersion = gitVersion - node.Status.DaemonEndpoints = corev1.NodeDaemonEndpoints{ - KubeletEndpoint: corev1.DaemonEndpoint{ - Port: listenPort, - }, - } + if !errors.IsNotFound(err) { + return nil, err + } + node = utils.BuildNodeTemplate(nodeName) + node.Status.NodeInfo.KubeletVersion = gitVersion + node.Status.DaemonEndpoints = corev1.NodeDaemonEndpoints{ + KubeletEndpoint: corev1.DaemonEndpoint{ + Port: listenPort, + }, + } - node, err = h.RootClientset.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{}) - if err != nil { - if !errors.IsAlreadyExists(err) { - return nil, err - } else { - nodes = append(nodes, node) - } - } - } else { + node, err = h.RootClientset.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{}) + if err != nil { return nil, err } - } else { - nodes = append(nodes, node) } + nodes = append(nodes, node) return nodes, nil } @@ -169,40 +163,33 @@ func (h DispersionModelHandler) CreateNodeInRoot(ctx context.Context, cluster *k // todo only support nodeName now if leafModel.NodeSelector.NodeName != "" { nodeName := leafModel.NodeSelector.NodeName - node, err := h.RootClientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{}) if err != nil { - if errors.IsNotFound(err) { - node = utils.BuildNodeTemplate(nodeName) - - nodeAnnotations := node.GetAnnotations() - if nodeAnnotations == nil { - nodeAnnotations = make(map[string]string, 1) - } - nodeAnnotations[utils.KosmosNodeOwnedByClusterAnnotations] = cluster.Name - node.SetAnnotations(nodeAnnotations) - - node.Status.NodeInfo.KubeletVersion = gitVersion - node.Status.DaemonEndpoints = corev1.NodeDaemonEndpoints{ - KubeletEndpoint: corev1.DaemonEndpoint{ - Port: listenPort, - }, - } - - node, err = h.RootClientset.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{}) - if err != nil { - if !errors.IsAlreadyExists(err) { - return nil, err - } else { - nodes = append(nodes, node) - } - } - } else { + if !errors.IsNotFound(err) { + return nil, err + } + + node = utils.BuildNodeTemplate(nodeName) + nodeAnnotations := node.GetAnnotations() + if nodeAnnotations == nil { + nodeAnnotations = make(map[string]string, 1) + } + nodeAnnotations[utils.KosmosNodeOwnedByClusterAnnotations] = cluster.Name + node.SetAnnotations(nodeAnnotations) + + node.Status.NodeInfo.KubeletVersion = gitVersion + node.Status.DaemonEndpoints = corev1.NodeDaemonEndpoints{ + KubeletEndpoint: corev1.DaemonEndpoint{ + Port: listenPort, + }, + } + + node, err = h.RootClientset.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{}) + if err != nil { return nil, err } - } else { - nodes = append(nodes, node) } + nodes = append(nodes, node) } } return nodes, nil