|
| 1 | +// Copyright 2025 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package nodelabels |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + |
| 12 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 13 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 14 | + bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1" |
| 15 | + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" |
| 16 | + ctrl "sigs.k8s.io/controller-runtime" |
| 17 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 18 | + |
| 19 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 20 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation" |
| 21 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches" |
| 22 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/patches/selectors" |
| 23 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + // VariableName is intentionally empty; this mutation is unconditional for workers. |
| 28 | + VariableName = "" |
| 29 | + |
| 30 | + // kubeletArgNodeLabels is the kubelet extra arg key controlling node labels. |
| 31 | + kubeletArgNodeLabels = "node-labels" |
| 32 | + |
| 33 | + // workerRoleLabel is the canonical worker role label. |
| 34 | + workerRoleLabel = "node-role.kubernetes.io/worker" |
| 35 | +) |
| 36 | + |
| 37 | +type workerNodeLabelsPatchHandler struct { |
| 38 | + variableName string |
| 39 | + variableFieldPath []string |
| 40 | +} |
| 41 | + |
| 42 | +// NewWorkerPatch returns a mutator that ensures worker nodes get the worker role label. |
| 43 | +func NewWorkerPatch() *workerNodeLabelsPatchHandler { |
| 44 | + return &workerNodeLabelsPatchHandler{ |
| 45 | + variableName: v1alpha1.WorkerConfigVariableName, |
| 46 | + variableFieldPath: []string{}, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (h *workerNodeLabelsPatchHandler) Mutate( |
| 51 | + ctx context.Context, |
| 52 | + obj *unstructured.Unstructured, |
| 53 | + vars map[string]apiextensionsv1.JSON, |
| 54 | + holderRef runtimehooksv1.HolderReference, |
| 55 | + _ ctrlclient.ObjectKey, |
| 56 | + _ mutation.ClusterGetter, |
| 57 | +) error { |
| 58 | + log := ctrl.LoggerFrom(ctx).WithValues( |
| 59 | + "holderRef", holderRef, |
| 60 | + ) |
| 61 | + |
| 62 | + // Best-effort fetch of worker variable to keep symmetry with other handlers; ignore not found. |
| 63 | + if _, err := variables.Get[map[string]any]( |
| 64 | + vars, |
| 65 | + h.variableName, |
| 66 | + h.variableFieldPath..., |
| 67 | + ); err != nil && !variables.IsNotFoundError(err) { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + return patches.MutateIfApplicable( |
| 72 | + obj, vars, &holderRef, selectors.WorkersKubeadmConfigTemplateSelector(), log, |
| 73 | + func(obj *bootstrapv1.KubeadmConfigTemplate) error { |
| 74 | + log.WithValues( |
| 75 | + "patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(), |
| 76 | + "patchedObjectName", ctrlclient.ObjectKeyFromObject(obj), |
| 77 | + ).Info("ensuring worker node label via kubelet extra args on join") |
| 78 | + |
| 79 | + if obj.Spec.Template.Spec.JoinConfiguration == nil { |
| 80 | + obj.Spec.Template.Spec.JoinConfiguration = &bootstrapv1.JoinConfiguration{} |
| 81 | + } |
| 82 | + |
| 83 | + args := obj.Spec.Template.Spec.JoinConfiguration.NodeRegistration.KubeletExtraArgs |
| 84 | + if args == nil { |
| 85 | + args = map[string]string{} |
| 86 | + } |
| 87 | + |
| 88 | + updated := ensureWorkerNodeLabel(args) |
| 89 | + if updated { |
| 90 | + obj.Spec.Template.Spec.JoinConfiguration.NodeRegistration.KubeletExtraArgs = args |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | + }, |
| 95 | + ) |
| 96 | +} |
| 97 | + |
| 98 | +// ensureWorkerNodeLabel adds workerRoleLabel to kubeletArgNodeLabels if not already present. |
| 99 | +// It returns true if args were updated. |
| 100 | +func ensureWorkerNodeLabel(args map[string]string) bool { |
| 101 | + // Build set of labels from existing arg, if any. |
| 102 | + existing := args[kubeletArgNodeLabels] |
| 103 | + if existing == "" { |
| 104 | + args[kubeletArgNodeLabels] = workerRoleLabel + "=" |
| 105 | + return true |
| 106 | + } |
| 107 | + |
| 108 | + // Parse comma-separated list of key[=value] entries. |
| 109 | + parts := strings.Split(existing, ",") |
| 110 | + // Track presence and rebuild normalized list. |
| 111 | + hasWorker := false |
| 112 | + normalized := make([]string, 0, len(parts)+1) |
| 113 | + seen := make(map[string]struct{}) |
| 114 | + |
| 115 | + for _, p := range parts { |
| 116 | + p = strings.TrimSpace(p) |
| 117 | + if p == "" { |
| 118 | + continue |
| 119 | + } |
| 120 | + key := p |
| 121 | + if idx := strings.IndexByte(p, '='); idx >= 0 { |
| 122 | + key = p[:idx] |
| 123 | + } |
| 124 | + if key == workerRoleLabel { |
| 125 | + hasWorker = true |
| 126 | + // Normalize to key= |
| 127 | + p = fmt.Sprintf("%s=", workerRoleLabel) |
| 128 | + } |
| 129 | + if _, ok := seen[p]; ok { |
| 130 | + continue |
| 131 | + } |
| 132 | + seen[p] = struct{}{} |
| 133 | + normalized = append(normalized, p) |
| 134 | + } |
| 135 | + |
| 136 | + if !hasWorker { |
| 137 | + normalized = append(normalized, workerRoleLabel+"=") |
| 138 | + } |
| 139 | + |
| 140 | + sort.Strings(normalized) |
| 141 | + newVal := strings.Join(normalized, ",") |
| 142 | + if newVal == existing { |
| 143 | + return false |
| 144 | + } |
| 145 | + args[kubeletArgNodeLabels] = newVal |
| 146 | + return true |
| 147 | +} |
0 commit comments