Skip to content

Commit cc34642

Browse files
committed
fixup! feat: read kube-vip template from KCPTemplate
1 parent 1aae2c6 commit cc34642

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,12 @@ func (h *ControlPlaneVirtualIP) Mutate(
104104
func(obj *controlplanev1.KubeadmControlPlaneTemplate) error {
105105
if controlPlaneEndpointVar.VirtualIPSpec == nil {
106106
log.V(5).Info("ControlPlane VirtualIP not set")
107-
// if VirtualIPSpec is not set, delete all template files
107+
// if VirtualIPSpec is not set, delete all VirtualIP providers' template files
108108
// as we do not want them to end up in the generated KCP
109-
deleteFiles(obj, providers.TemplateFileNames...)
109+
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = deleteFiles(
110+
obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
111+
providers.VirtualIPProviderFileNames...,
112+
)
110113
return nil
111114
}
112115

@@ -133,7 +136,10 @@ func (h *ControlPlaneVirtualIP) Mutate(
133136
virtualIPProvider.Name(),
134137
))
135138

136-
mergeFiles(obj, files...)
139+
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = mergeFiles(
140+
obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
141+
files...,
142+
)
137143

138144
if len(preKubeadmCommands) > 0 {
139145
log.WithValues(
@@ -168,35 +174,34 @@ func (h *ControlPlaneVirtualIP) Mutate(
168174
)
169175
}
170176

171-
func deleteFiles(obj *controlplanev1.KubeadmControlPlaneTemplate, filePathsToDelete ...string) {
172-
for i := len(obj.Spec.Template.Spec.KubeadmConfigSpec.Files) - 1; i >= 0; i-- {
177+
func deleteFiles(files []bootstrapv1.File, filePathsToDelete ...string) []bootstrapv1.File {
178+
for i := len(files) - 1; i >= 0; i-- {
173179
for _, path := range filePathsToDelete {
174-
if obj.Spec.Template.Spec.KubeadmConfigSpec.Files[i].Path == path {
175-
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = slices.Delete(
176-
obj.Spec.Template.Spec.KubeadmConfigSpec.Files, i, i+1,
177-
)
180+
if files[i].Path == path {
181+
files = slices.Delete(files, i, i+1)
178182
break
179183
}
180184
}
181185
}
186+
187+
return files
182188
}
183189

184190
// mergeFiles will merge the files into the KubeadmControlPlaneTemplate,
185191
// overriding any file with the same path and appending the rest.
186-
func mergeFiles(obj *controlplanev1.KubeadmControlPlaneTemplate, filesToMerge ...bootstrapv1.File) {
192+
func mergeFiles(files []bootstrapv1.File, filesToMerge ...bootstrapv1.File) []bootstrapv1.File {
187193
// replace any existing files with the same path
188194
for i := len(filesToMerge) - 1; i >= 0; i-- {
189-
for j := range obj.Spec.Template.Spec.KubeadmConfigSpec.Files {
190-
if obj.Spec.Template.Spec.KubeadmConfigSpec.Files[j].Path == filesToMerge[i].Path {
191-
obj.Spec.Template.Spec.KubeadmConfigSpec.Files[j] = filesToMerge[i]
195+
for j := range files {
196+
if files[j].Path == filesToMerge[i].Path {
197+
files[j] = filesToMerge[i]
192198
filesToMerge = slices.Delete(filesToMerge, i, i+1)
193199
break
194200
}
195201
}
196202
}
197203
// append the remaining files
198-
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = append(
199-
obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
200-
filesToMerge...,
201-
)
204+
files = append(files, filesToMerge...)
205+
206+
return files
202207
}

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,11 @@ func Test_deleteFiles(t *testing.T) {
368368
t.Run(tt.name, func(t *testing.T) {
369369
t.Parallel()
370370

371-
deleteFiles(tt.obj, tt.filesToDelete...)
372-
assert.Equal(t, tt.expectedFiles, tt.obj.Spec.Template.Spec.KubeadmConfigSpec.Files)
371+
updatedFiles := deleteFiles(
372+
tt.obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
373+
tt.filesToDelete...,
374+
)
375+
assert.Equal(t, tt.expectedFiles, updatedFiles)
373376
})
374377
}
375378
}
@@ -514,8 +517,11 @@ func Test_mergeFiles(t *testing.T) {
514517
t.Run(tt.name, func(t *testing.T) {
515518
t.Parallel()
516519

517-
mergeFiles(tt.obj, tt.files...)
518-
assert.Equal(t, tt.expectedFiles, tt.obj.Spec.Template.Spec.KubeadmConfigSpec.Files)
520+
updatedFiles := mergeFiles(
521+
tt.obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
522+
tt.files...,
523+
)
524+
assert.Equal(t, tt.expectedFiles, updatedFiles)
519525
})
520526
}
521527
}

pkg/handlers/generic/mutation/controlplanevirtualip/providers/providers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1717
)
1818

19-
var TemplateFileNames = []string{
19+
var VirtualIPProviderFileNames = []string{
2020
kubeVIPFilePath,
2121
}
2222

0 commit comments

Comments
 (0)