Skip to content

Commit 6e01788

Browse files
faiqvijayaraghavanr31
authored andcommitted
fix: use nodeadm mutations (#1332)
**What problem does this PR solve?**: stacked on #1329 to use nodeadm in handlers **Which issue(s) this PR fixes**: https://jira.nutanix.com/browse/NCN-110198 **How Has This Been Tested?**: <!-- Please describe the tests that you ran to verify your changes. Provide output from the tests and any manual steps needed to replicate the tests. --> **Special notes for your reviewer**: <!-- Use this to provide any additional information to the reviewers. This may include: - Best way to review the PR. - Where the author wants the most review attention on. - etc. -->
1 parent daaab18 commit 6e01788

File tree

10 files changed

+127
-42
lines changed

10 files changed

+127
-42
lines changed

charts/cluster-api-runtime-extensions-nutanix/defaultclusterclasses/eks-cluster-class.yaml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ spec:
3131
bootstrap:
3232
ref:
3333
apiVersion: bootstrap.cluster.x-k8s.io/v1beta2
34-
kind: EKSConfigTemplate
34+
kind: NodeadmConfigTemplate
3535
name: eks-quick-start-worker-configtemplate
3636
infrastructure:
3737
ref:
@@ -73,11 +73,18 @@ metadata:
7373
spec:
7474
template:
7575
spec:
76+
ami:
77+
eksLookupType: AmazonLinux2023
78+
cloudInit:
79+
insecureSkipSecretsManager: true
80+
instanceMetadataOptions:
81+
httpPutResponseHopLimit: 2
82+
httpTokens: required
7683
instanceType: PLACEHOLDER
7784
sshKeyName: ""
7885
---
7986
apiVersion: bootstrap.cluster.x-k8s.io/v1beta2
80-
kind: EKSConfigTemplate
87+
kind: NodeadmConfigTemplate
8188
metadata:
8289
labels:
8390
cluster.x-k8s.io/provider: eks

hack/examples/bases/eks/clusterclass/clusterclass.yaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ spec:
2424
ref:
2525
name: "quick-start-worker-configtemplate"
2626
apiVersion: bootstrap.cluster.x-k8s.io/v1beta2
27-
kind: EKSConfigTemplate
27+
kind: NodeadmConfigTemplate
2828
infrastructure:
2929
ref:
3030
name: "quick-start-worker-machinetemplate"
@@ -58,10 +58,17 @@ metadata:
5858
name: "quick-start-worker-machinetemplate"
5959
spec:
6060
template:
61-
spec: {}
61+
spec:
62+
cloudInit:
63+
insecureSkipSecretsManager: true
64+
ami:
65+
eksLookupType: AmazonLinux2023
66+
instanceMetadataOptions:
67+
httpTokens: required
68+
httpPutResponseHopLimit: 2
6269
---
6370
apiVersion: bootstrap.cluster.x-k8s.io/v1beta2
64-
kind: EKSConfigTemplate
71+
kind: NodeadmConfigTemplate
6572
metadata:
6673
name: "quick-start-worker-configtemplate"
6774
spec:

hack/examples/overlays/clusterclasses/eks/kustomizeconfig.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ nameReference:
1616
fieldSpecs:
1717
- kind: ClusterClass
1818
path: spec/controlPlane/ref/name
19-
- kind: EKSConfigTemplate
19+
- kind: NodeadmConfigTemplate
2020
fieldSpecs:
2121
- kind: ClusterClass
2222
path: spec/workers/machineDeployments/template/bootstrap/ref/name

pkg/handlers/eks/mutation/testutils/request.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,28 @@ func NewEKSControlPlaneRequestItem(
4747
)
4848
}
4949

50-
func NewEKSConfigTemplateRequestItem(
50+
func NewNodeadmConfigTemplateRequestItem(
5151
uid types.UID,
52-
existingSpec ...eksbootstrapv1.EKSConfigTemplateSpec,
52+
existingSpec ...eksbootstrapv1.NodeadmConfigTemplateSpec,
5353
) runtimehooksv1.GeneratePatchesRequestItem {
54-
eksConfigTemplate := &eksbootstrapv1.EKSConfigTemplate{
54+
nodeadmConfigTemplate := &eksbootstrapv1.NodeadmConfigTemplate{
5555
TypeMeta: metav1.TypeMeta{
5656
APIVersion: eksbootstrapv1.GroupVersion.String(),
57-
Kind: "EKSConfigTemplate",
57+
Kind: "NodeadmConfigTemplate",
5858
},
5959
}
6060

6161
switch len(existingSpec) {
6262
case 0:
6363
// Do nothing.
6464
case 1:
65-
eksConfigTemplate.Spec = existingSpec[0]
65+
nodeadmConfigTemplate.Spec = existingSpec[0]
6666
default:
6767
panic("can only take at most one existing spec")
6868
}
6969

7070
return request.NewRequestItem(
71-
eksConfigTemplate,
71+
nodeadmConfigTemplate,
7272
&runtimehooksv1.HolderReference{
7373
Kind: "MachineDeployment",
7474
FieldPath: "spec.template.spec.bootstrap.configRef",

pkg/handlers/generic/mutation/ntp/inject.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,12 @@ func (h *ntpPatchHandler) Mutate(
124124

125125
if err := patches.MutateIfApplicable(
126126
obj, vars, &holderRef,
127-
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "EKSConfigTemplate"), log,
128-
func(obj *eksbootstrapv1.EKSConfigTemplate) error {
127+
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "NodeadmConfigTemplate"), log,
128+
func(obj *eksbootstrapv1.NodeadmConfigTemplate) error {
129129
log.WithValues(
130130
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(),
131131
"patchedObjectName", client.ObjectKeyFromObject(obj),
132-
).Info("setting users in worker node EKS config template")
132+
).Info("setting users in worker node NodeadmConfig template")
133133
obj.Spec.Template.Spec.NTP = &eksbootstrapv1.NTP{
134134
Enabled: ptr.To(true),
135135
Servers: ntp.Servers,

pkg/handlers/generic/mutation/ntp/inject_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ var _ = Describe("Generate NTP patches", func() {
160160
},
161161
},
162162
{
163-
Name: "NTP configuration is set for worker nodes with single server for EKSConfigTemplate",
164-
RequestItem: testutils.NewEKSConfigTemplateRequestItem(""),
163+
Name: "NTP configuration is set for worker nodes with single server for NodeadmConfigTemplate",
164+
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem(""),
165165
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{
166166
{
167167
Operation: "add",

pkg/handlers/generic/mutation/taints/inject_worker.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package taints
55

66
import (
77
"context"
8+
"fmt"
89
"strings"
910

1011
"github.com/samber/lo"
1112
v1 "k8s.io/api/core/v1"
1213
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1314
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
15+
"k8s.io/utils/ptr"
1416
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
1517
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1618
ctrl "sigs.k8s.io/controller-runtime"
@@ -48,6 +50,10 @@ func newTaintsWorkerPatchHandler(
4850
}
4951
}
5052

53+
type KubeletRegisterOptions struct {
54+
RegisterWithTaints []v1.Taint `json:"registerWithTaints,omitempty"`
55+
}
56+
5157
func (h *taintsWorkerPatchHandler) Mutate(
5258
ctx context.Context,
5359
obj *unstructured.Unstructured,
@@ -103,25 +109,16 @@ func (h *taintsWorkerPatchHandler) Mutate(
103109

104110
if err := patches.MutateIfApplicable(
105111
obj, vars, &holderRef,
106-
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "EKSConfigTemplate"), log,
107-
func(obj *eksbootstrapv1.EKSConfigTemplate) error {
112+
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "NodeadmConfigTemplate"), log,
113+
func(obj *eksbootstrapv1.NodeadmConfigTemplate) error {
108114
log.WithValues(
109115
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(),
110116
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj),
111-
).Info("adding taints to worker node EKS config template")
112-
if obj.Spec.Template.Spec.KubeletExtraArgs == nil {
113-
obj.Spec.Template.Spec.KubeletExtraArgs = make(map[string]string, 1)
114-
}
115-
116-
existingTaintsFlagValue := obj.Spec.Template.Spec.KubeletExtraArgs["register-with-taints"]
117-
118-
newTaintsFlagValue := toEKSConfigTaints(taintsVar)
119-
120-
if existingTaintsFlagValue != "" {
121-
newTaintsFlagValue = existingTaintsFlagValue + "," + newTaintsFlagValue
122-
}
123-
124-
obj.Spec.Template.Spec.KubeletExtraArgs["register-with-taints"] = newTaintsFlagValue
117+
).Info("adding taints to worker NodeadmConfig template")
118+
newTaints := toEKSConfigTaints(taintsVar)
119+
kubeletOptions := ptr.Deref(obj.Spec.Template.Spec.Kubelet, eksbootstrapv1.KubeletOptions{})
120+
kubeletOptions.Flags = append(kubeletOptions.Flags, fmt.Sprintf("--register-with-taints=%s", newTaints))
121+
obj.Spec.Template.Spec.Kubelet = &kubeletOptions
125122
return nil
126123
}); err != nil {
127124
return err

pkg/handlers/generic/mutation/taints/inject_worker_test.go

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1414
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1515

16+
eksbootstrapv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-provider-aws/v2/bootstrap/eks/api/v1beta2"
1617
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1718
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/mutation"
1819
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/testutils/capitest"
@@ -59,7 +60,7 @@ var _ = Describe("Generate taints patches for Worker", func() {
5960
}},
6061
},
6162
{
62-
Name: "taints for workers set for EKSConfigTemplate",
63+
Name: "taints for workers set for NodeadmConfigTemplate",
6364
Vars: []runtimehooksv1.Variable{
6465
capitest.VariableWithValue(
6566
v1alpha1.WorkerConfigVariableName,
@@ -77,15 +78,88 @@ var _ = Describe("Generate taints patches for Worker", func() {
7778
},
7879
),
7980
},
80-
RequestItem: testutils.NewEKSConfigTemplateRequestItem(""),
81+
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem(""),
8182
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
8283
Operation: "add",
83-
Path: "/spec/template/spec/kubeletExtraArgs",
84+
Path: "/spec/template/spec/kubelet",
8485
ValueMatcher: gomega.HaveKeyWithValue(
85-
"register-with-taints", "key=value:NoExecute",
86+
"flags",
87+
gomega.ContainElement("--register-with-taints=key=value:NoExecute"),
8688
),
8789
}},
8890
},
91+
{
92+
Name: "taints for workers set for NodeadmConfigTemplate with existing flags argument",
93+
Vars: []runtimehooksv1.Variable{
94+
capitest.VariableWithValue(
95+
v1alpha1.WorkerConfigVariableName,
96+
[]v1alpha1.Taint{{
97+
Key: "key",
98+
Effect: v1alpha1.TaintEffectNoExecute,
99+
Value: "value",
100+
}},
101+
VariableName,
102+
),
103+
capitest.VariableWithValue(
104+
"builtin",
105+
apiextensionsv1.JSON{
106+
Raw: []byte(`{"machineDeployment": {"class": "a-worker"}}`),
107+
},
108+
),
109+
},
110+
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem("", eksbootstrapv1.NodeadmConfigTemplateSpec{
111+
Template: eksbootstrapv1.NodeadmConfigTemplateResource{
112+
Spec: eksbootstrapv1.NodeadmConfigSpec{
113+
Kubelet: &eksbootstrapv1.KubeletOptions{
114+
Flags: []string{
115+
"--max-pods=110",
116+
},
117+
},
118+
},
119+
},
120+
}),
121+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
122+
Operation: "add",
123+
Path: "/spec/template/spec/kubelet/flags/1",
124+
ValueMatcher: gomega.Equal("--register-with-taints=key=value:NoExecute"),
125+
}},
126+
},
127+
{
128+
Name: "taints for workers set for NodeadmConfigTemplate with existing flags with register-with-taints ",
129+
Vars: []runtimehooksv1.Variable{
130+
capitest.VariableWithValue(
131+
v1alpha1.WorkerConfigVariableName,
132+
[]v1alpha1.Taint{{
133+
Key: "key",
134+
Effect: v1alpha1.TaintEffectNoExecute,
135+
Value: "value",
136+
}},
137+
VariableName,
138+
),
139+
capitest.VariableWithValue(
140+
"builtin",
141+
apiextensionsv1.JSON{
142+
Raw: []byte(`{"machineDeployment": {"class": "a-worker"}}`),
143+
},
144+
),
145+
},
146+
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem("", eksbootstrapv1.NodeadmConfigTemplateSpec{
147+
Template: eksbootstrapv1.NodeadmConfigTemplateResource{
148+
Spec: eksbootstrapv1.NodeadmConfigSpec{
149+
Kubelet: &eksbootstrapv1.KubeletOptions{
150+
Flags: []string{
151+
"--register-with-taints=key1=value1:NoSchedule",
152+
},
153+
},
154+
},
155+
},
156+
}),
157+
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
158+
Operation: "add",
159+
Path: "/spec/template/spec/kubelet/flags/1",
160+
ValueMatcher: gomega.Equal("--register-with-taints=key=value:NoExecute"),
161+
}},
162+
},
89163
}
90164

91165
// create test node for each case

pkg/handlers/generic/mutation/users/inject.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ func (h *usersPatchHandler) Mutate(
115115

116116
if err := patches.MutateIfApplicable(
117117
obj, vars, &holderRef,
118-
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "EKSConfigTemplate"), log,
119-
func(obj *eksbootstrapv1.EKSConfigTemplate) error {
118+
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "NodeadmConfigTemplate"), log,
119+
func(obj *eksbootstrapv1.NodeadmConfigTemplate) error {
120120
log.WithValues(
121121
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(),
122122
"patchedObjectName", ctrlclient.ObjectKeyFromObject(obj),
123-
).Info("setting users in worker node EKS config template")
123+
).Info("setting users in worker node NodeadmConfig template")
124124
eksBootstrapUsers := make([]eksbootstrapv1.User, 0, len(bootstrapUsers))
125125
for _, user := range bootstrapUsers {
126126
var passwdFrom *eksbootstrapv1.PasswdSource

pkg/handlers/generic/mutation/users/inject_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ var _ = Describe("Generate Users patches", func() {
194194
}},
195195
},
196196
{
197-
Name: "users set for EKSConfigTemplate generic worker",
197+
Name: "users set for NodeadmConfigTemplate generic worker",
198198
Vars: []runtimehooksv1.Variable{
199199
capitest.VariableWithValue(
200200
v1alpha1.ClusterConfigVariableName,
@@ -210,7 +210,7 @@ var _ = Describe("Generate Users patches", func() {
210210
},
211211
),
212212
},
213-
RequestItem: testutils.NewEKSConfigTemplateRequestItem(""),
213+
RequestItem: testutils.NewNodeadmConfigTemplateRequestItem(""),
214214
ExpectedPatchMatchers: []capitest.JSONPatchMatcher{{
215215
Operation: "add",
216216
Path: "/spec/template/spec/users",

0 commit comments

Comments
 (0)