Skip to content

Commit

Permalink
fix: sync label from node to globalnodes
Browse files Browse the repository at this point in the history
Signed-off-by: ONE7live <[email protected]>
  • Loading branch information
ONE7live committed May 15, 2024
1 parent c45f97b commit 5b9f41d
Showing 1 changed file with 29 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5b9f41d

Please sign in to comment.