@@ -104,9 +104,12 @@ func (h *ControlPlaneVirtualIP) Mutate(
104
104
func (obj * controlplanev1.KubeadmControlPlaneTemplate ) error {
105
105
if controlPlaneEndpointVar .VirtualIPSpec == nil {
106
106
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
108
108
// 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
+ )
110
113
return nil
111
114
}
112
115
@@ -133,7 +136,10 @@ func (h *ControlPlaneVirtualIP) Mutate(
133
136
virtualIPProvider .Name (),
134
137
))
135
138
136
- mergeFiles (obj , files ... )
139
+ obj .Spec .Template .Spec .KubeadmConfigSpec .Files = mergeFiles (
140
+ obj .Spec .Template .Spec .KubeadmConfigSpec .Files ,
141
+ files ... ,
142
+ )
137
143
138
144
if len (preKubeadmCommands ) > 0 {
139
145
log .WithValues (
@@ -168,35 +174,34 @@ func (h *ControlPlaneVirtualIP) Mutate(
168
174
)
169
175
}
170
176
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 -- {
173
179
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 )
178
182
break
179
183
}
180
184
}
181
185
}
186
+
187
+ return files
182
188
}
183
189
184
190
// mergeFiles will merge the files into the KubeadmControlPlaneTemplate,
185
191
// 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 {
187
193
// replace any existing files with the same path
188
194
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 ]
192
198
filesToMerge = slices .Delete (filesToMerge , i , i + 1 )
193
199
break
194
200
}
195
201
}
196
202
}
197
203
// 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
202
207
}
0 commit comments