Skip to content

Commit 4938107

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

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ 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 =
110+
deleteFiles(obj.Spec.Template.Spec.KubeadmConfigSpec.Files, providers.VirtualIPProviderFileNames...)
110111
return nil
111112
}
112113

@@ -133,7 +134,8 @@ func (h *ControlPlaneVirtualIP) Mutate(
133134
virtualIPProvider.Name(),
134135
))
135136

136-
mergeFiles(obj, files...)
137+
obj.Spec.Template.Spec.KubeadmConfigSpec.Files =
138+
mergeFiles(obj.Spec.Template.Spec.KubeadmConfigSpec.Files, files...)
137139

138140
if len(preKubeadmCommands) > 0 {
139141
log.WithValues(
@@ -168,35 +170,34 @@ func (h *ControlPlaneVirtualIP) Mutate(
168170
)
169171
}
170172

171-
func deleteFiles(obj *controlplanev1.KubeadmControlPlaneTemplate, filePathsToDelete ...string) {
172-
for i := len(obj.Spec.Template.Spec.KubeadmConfigSpec.Files) - 1; i >= 0; i-- {
173+
func deleteFiles(files []bootstrapv1.File, filePathsToDelete ...string) []bootstrapv1.File {
174+
for i := len(files) - 1; i >= 0; i-- {
173175
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-
)
176+
if files[i].Path == path {
177+
files = slices.Delete(files, i, i+1)
178178
break
179179
}
180180
}
181181
}
182+
183+
return files
182184
}
183185

184186
// mergeFiles will merge the files into the KubeadmControlPlaneTemplate,
185187
// overriding any file with the same path and appending the rest.
186-
func mergeFiles(obj *controlplanev1.KubeadmControlPlaneTemplate, filesToMerge ...bootstrapv1.File) {
188+
func mergeFiles(files []bootstrapv1.File, filesToMerge ...bootstrapv1.File) []bootstrapv1.File {
187189
// replace any existing files with the same path
188190
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]
191+
for j := range files {
192+
if files[j].Path == filesToMerge[i].Path {
193+
files[j] = filesToMerge[i]
192194
filesToMerge = slices.Delete(filesToMerge, i, i+1)
193195
break
194196
}
195197
}
196198
}
197199
// append the remaining files
198-
obj.Spec.Template.Spec.KubeadmConfigSpec.Files = append(
199-
obj.Spec.Template.Spec.KubeadmConfigSpec.Files,
200-
filesToMerge...,
201-
)
200+
files = append(files, filesToMerge...)
201+
202+
return files
202203
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ 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(tt.obj.Spec.Template.Spec.KubeadmConfigSpec.Files, tt.filesToDelete...)
372+
assert.Equal(t, tt.expectedFiles, updatedFiles)
373373
})
374374
}
375375
}
@@ -514,8 +514,8 @@ func Test_mergeFiles(t *testing.T) {
514514
t.Run(tt.name, func(t *testing.T) {
515515
t.Parallel()
516516

517-
mergeFiles(tt.obj, tt.files...)
518-
assert.Equal(t, tt.expectedFiles, tt.obj.Spec.Template.Spec.KubeadmConfigSpec.Files)
517+
updatedFiles := mergeFiles(tt.obj.Spec.Template.Spec.KubeadmConfigSpec.Files, tt.files...)
518+
assert.Equal(t, tt.expectedFiles, updatedFiles)
519519
})
520520
}
521521
}

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)