diff --git a/pkg/kubenest/controller/global.node.controller/global_node_controller.go b/pkg/kubenest/controller/global.node.controller/global_node_controller.go index 17cf46146..5fdc98f4e 100644 --- a/pkg/kubenest/controller/global.node.controller/global_node_controller.go +++ b/pkg/kubenest/controller/global.node.controller/global_node_controller.go @@ -155,56 +155,44 @@ func (r *GlobalNodeController) SyncState(ctx context.Context, globalNode *v1alph } func (r *GlobalNodeController) SyncLabel(ctx context.Context, globalNode *v1alpha1.GlobalNode) error { - var client kubernetes.Interface - if globalNode.Spec.State != v1alpha1.NodeInUse { - client = r.RootClientSet - } else { - vclist, err := r.KosmosClient.KosmosV1alpha1().VirtualClusters("").List(ctx, metav1.ListOptions{}) - if err != nil { - klog.Warningf("global-node-controller: SyncState: cannot list virtual cluster, err: %s", globalNode.Name) - return err - } - var targetVirtualCluster v1alpha1.VirtualCluster - for _, v := range vclist.Items { - if v.Name == globalNode.Status.VirtualCluster { - targetVirtualCluster = v - break - } - } - if targetVirtualCluster.Status.Phase != v1alpha1.Completed && targetVirtualCluster.Status.Phase != v1alpha1.AllNodeReady { - klog.Warningf("global-node-controller: SyncState: virtual cluster is not completed, err: %s", globalNode.Name) - return nil - } - virtualClient, err := util.GenerateKubeclient(&targetVirtualCluster) + err := retry.RetryOnConflict(retry.DefaultRetry, func() error { + rootNode, err := r.RootClientSet.CoreV1().Nodes().Get(ctx, globalNode.Name, metav1.GetOptions{}) if err != nil { - klog.Warningf("global-node-controller: SyncState: cannot generate kubeclient, err: %s", globalNode.Name) + klog.Errorf("global-node-controller: SyncState: can not get root node, err: %s", globalNode.Name) return err } - client = virtualClient - } - - err := retry.RetryOnConflict(retry.DefaultRetry, func() error { - hostNode, err := client.CoreV1().Nodes().Get(ctx, globalNode.Name, metav1.GetOptions{}) - if err != nil { + // If global node does not exist, create it + if _, err = r.KosmosClient.KosmosV1alpha1().GlobalNodes().Get(context.TODO(), globalNode.Name, metav1.GetOptions{}); err != nil { + if apierrors.IsNotFound(err) { + var nodeIP string + for _, a := range rootNode.Status.Addresses { + if a.Type == "InternalIP" { + nodeIP = a.Address + break + } + } + globalNode = &v1alpha1.GlobalNode{ + Spec: v1alpha1.GlobalNodeSpec{ + NodeIP: nodeIP, + State: v1alpha1.NodeReserved, + }, + } + if _, err = r.KosmosClient.KosmosV1alpha1().GlobalNodes().Create(context.TODO(), globalNode, metav1.CreateOptions{}); err != nil { + klog.Errorf("global-node-controller: SyncState: can not create global node, err: %s", globalNode.Name) + return err + } + } klog.Errorf("global-node-controller: SyncState: can not get global node, err: %s", globalNode.Name) return err } - updateHostNode := hostNode.DeepCopy() - needUpdate := false - for k, v := range globalNode.Spec.Labels { - if updateHostNode.Labels[k] != v { - updateHostNode.Labels[k] = v - needUpdate = true - } - } - if !needUpdate { - return nil - } + // Use management plane node label to override global node + updateGlobalNode := globalNode.DeepCopy() + updateGlobalNode.Labels = rootNode.Labels - if _, err := client.CoreV1().Nodes().Update(ctx, updateHostNode, metav1.UpdateOptions{}); err != nil { - klog.Errorf("global-node-controller: SyncState: update node label failed, err: %s", globalNode.Name) + if _, err = r.KosmosClient.KosmosV1alpha1().GlobalNodes().Update(context.TODO(), updateGlobalNode, metav1.UpdateOptions{}); err != nil { + klog.Errorf("global-node-controller: SyncState: update global node label failed, err: %s", globalNode.Name) return err } return nil