Skip to content

Commit 58c3aa8

Browse files
committed
fix: use nodeadm mutations
1 parent eee70a9 commit 58c3aa8

File tree

7 files changed

+84
-85
lines changed

7 files changed

+84
-85
lines changed

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: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ package taints
55

66
import (
77
"context"
8-
"strings"
8+
"encoding/json"
9+
"fmt"
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/apimachinery/pkg/runtime"
16+
kubeletconfig "k8s.io/kubelet/config/v1beta1"
1417
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
1518
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
1619
ctrl "sigs.k8s.io/controller-runtime"
@@ -48,6 +51,10 @@ func newTaintsWorkerPatchHandler(
4851
}
4952
}
5053

54+
type KubeletRegisterOptions struct {
55+
RegisterWithTaints []v1.Taint `json:"registerWithTaints,omitempty"`
56+
}
57+
5158
func (h *taintsWorkerPatchHandler) Mutate(
5259
ctx context.Context,
5360
obj *unstructured.Unstructured,
@@ -103,25 +110,56 @@ func (h *taintsWorkerPatchHandler) Mutate(
103110

104111
if err := patches.MutateIfApplicable(
105112
obj, vars, &holderRef,
106-
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "EKSConfigTemplate"), log,
107-
func(obj *eksbootstrapv1.EKSConfigTemplate) error {
113+
selectors.WorkersConfigTemplateSelector(eksbootstrapv1.GroupVersion.String(), "NodeadmConfigTemplate"), log,
114+
func(obj *eksbootstrapv1.NodeadmConfigTemplate) error {
108115
log.WithValues(
109116
"patchedObjectKind", obj.GetObjectKind().GroupVersionKind().String(),
110117
"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)
118+
).Info("adding taints to worker NodeadmConfig template")
119+
kubeletOptions := obj.Spec.Template.Spec.Kubelet
120+
config := &kubeletconfig.KubeletConfiguration{}
121+
hasKubeletConfigSet := false
122+
var flags []string
123+
var configRaw []byte
124+
// if we have config, use that
125+
if kubeletOptions != nil {
126+
flags = kubeletOptions.Flags
127+
if kubeletOptions.Config != nil {
128+
hasKubeletConfigSet = true
129+
configObj := kubeletOptions.Config.Object
130+
var ok bool
131+
config, ok = configObj.(*kubeletconfig.KubeletConfiguration)
132+
if !ok {
133+
return fmt.Errorf(
134+
"expected Kubelet Config object in NodeadmConfigTemplate.Spec.Template.Spec.Kubelet.Config but got %v",
135+
configObj,
136+
)
137+
}
138+
config.RegisterWithTaints = toCoreTaints(
139+
config.RegisterWithTaints,
140+
taintsVar,
141+
)
142+
configRaw, err = json.Marshal(config)
143+
if err != nil {
144+
return fmt.Errorf("failed to marshal config %w", err)
145+
}
146+
}
114147
}
115-
116-
existingTaintsFlagValue := obj.Spec.Template.Spec.KubeletExtraArgs["register-with-taints"]
117-
118-
newTaintsFlagValue := toEKSConfigTaints(taintsVar)
119-
120-
if existingTaintsFlagValue != "" {
121-
newTaintsFlagValue = existingTaintsFlagValue + "," + newTaintsFlagValue
148+
if !hasKubeletConfigSet {
149+
newConfig := KubeletRegisterOptions{
150+
toCoreTaints(nil, taintsVar),
151+
}
152+
configRaw, err = json.Marshal(newConfig)
153+
if err != nil {
154+
return fmt.Errorf("failed to marshal config %w", err)
155+
}
156+
}
157+
obj.Spec.Template.Spec.Kubelet = &eksbootstrapv1.KubeletOptions{
158+
Config: &runtime.RawExtension{
159+
Raw: configRaw,
160+
},
161+
Flags: flags,
122162
}
123-
124-
obj.Spec.Template.Spec.KubeletExtraArgs["register-with-taints"] = newTaintsFlagValue
125163
return nil
126164
}); err != nil {
127165
return err
@@ -156,16 +194,3 @@ func toCoreTaints(existingTaints []v1.Taint, newTaints []v1alpha1.Taint) []v1.Ta
156194
return append(existingTaints, newCoreTaints...)
157195
}
158196
}
159-
160-
func toEKSConfigTaints(newTaints []v1alpha1.Taint) string {
161-
taintValues := lo.Map(newTaints, func(t v1alpha1.Taint, _ int) string {
162-
taint := t.Key
163-
if t.Value != "" {
164-
taint += "=" + t.Value
165-
}
166-
taint += ":" + string(t.Effect)
167-
return taint
168-
})
169-
170-
return strings.Join(taintValues, ",")
171-
}

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

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
. "github.com/onsi/ginkgo/v2"
1010
"github.com/onsi/gomega"
11+
"github.com/onsi/gomega/gstruct"
1112
"github.com/stretchr/testify/assert"
1213
v1 "k8s.io/api/core/v1"
1314
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
@@ -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,12 +78,22 @@ 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+
"config",
87+
gomega.HaveKeyWithValue(
88+
"registerWithTaints",
89+
gomega.ConsistOf(
90+
gstruct.MatchKeys(gstruct.IgnoreExtras, gstruct.Keys{
91+
"key": gomega.Equal("key"),
92+
"value": gomega.Equal("value"),
93+
"effect": gomega.Equal("NoExecute"),
94+
}),
95+
),
96+
),
8697
),
8798
}},
8899
},
@@ -144,40 +155,3 @@ func Test_toCoreTaints(t *testing.T) {
144155
})
145156
}
146157
}
147-
148-
func Test_toEKSConfigTaints(t *testing.T) {
149-
t.Parallel()
150-
151-
tests := []struct {
152-
name string
153-
newTaints []v1alpha1.Taint
154-
want string
155-
}{{
156-
name: "nil taints",
157-
want: "",
158-
}, {
159-
name: "new taints",
160-
newTaints: []v1alpha1.Taint{
161-
{Key: "key", Effect: v1alpha1.TaintEffectNoExecute, Value: "value"},
162-
},
163-
want: "key=value:NoExecute",
164-
}, {
165-
name: "multiple new taints",
166-
newTaints: []v1alpha1.Taint{
167-
{Key: "key", Effect: v1alpha1.TaintEffectNoExecute, Value: "value"},
168-
{Key: "key2", Effect: v1alpha1.TaintEffectNoExecute, Value: "value2"},
169-
},
170-
want: "key=value:NoExecute,key2=value2:NoExecute",
171-
}, {
172-
name: "empty but non-nil new taints",
173-
newTaints: []v1alpha1.Taint{},
174-
want: "",
175-
}}
176-
for _, tt := range tests {
177-
t.Run(tt.name, func(t *testing.T) {
178-
t.Parallel()
179-
180-
assert.Equal(t, tt.want, toEKSConfigTaints(tt.newTaints))
181-
})
182-
}
183-
}

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)