@@ -156,3 +156,77 @@ func TestKubeadmControlPlaneTemplateValidationMetadata(t *testing.T) {
156
156
g .Expect (warnings ).To (BeEmpty ())
157
157
})
158
158
}
159
+
160
+ func TestKubeadmControlPlaneTemplateUpdateValidation (t * testing.T ) {
161
+ t .Run ("update KubeadmControlPlaneTemplate should pass if only defaulted fields are different" , func (t * testing.T ) {
162
+ g := NewWithT (t )
163
+ oldKCPTemplate := & controlplanev1.KubeadmControlPlaneTemplate {
164
+ Spec : controlplanev1.KubeadmControlPlaneTemplateSpec {
165
+ Template : controlplanev1.KubeadmControlPlaneTemplateResource {
166
+ Spec : controlplanev1.KubeadmControlPlaneTemplateResourceSpec {
167
+ MachineTemplate : & controlplanev1.KubeadmControlPlaneTemplateMachineTemplate {
168
+ NodeDrainTimeout : & metav1.Duration {Duration : time .Duration (10 ) * time .Minute },
169
+ },
170
+ },
171
+ },
172
+ },
173
+ }
174
+ newKCPTemplate := & controlplanev1.KubeadmControlPlaneTemplate {
175
+ Spec : controlplanev1.KubeadmControlPlaneTemplateSpec {
176
+ Template : controlplanev1.KubeadmControlPlaneTemplateResource {
177
+ Spec : controlplanev1.KubeadmControlPlaneTemplateResourceSpec {
178
+ KubeadmConfigSpec : bootstrapv1.KubeadmConfigSpec {
179
+ // Only this field is different, but defaulting will set it as well, so this should pass the immutability check.
180
+ Format : bootstrapv1 .CloudConfig ,
181
+ },
182
+ MachineTemplate : & controlplanev1.KubeadmControlPlaneTemplateMachineTemplate {
183
+ NodeDrainTimeout : & metav1.Duration {Duration : time .Duration (10 ) * time .Minute },
184
+ },
185
+ },
186
+ },
187
+ },
188
+ }
189
+ webhook := & KubeadmControlPlaneTemplate {}
190
+ warnings , err := webhook .ValidateUpdate (ctx , oldKCPTemplate , newKCPTemplate )
191
+ g .Expect (err ).ToNot (HaveOccurred ())
192
+ g .Expect (warnings ).To (BeEmpty ())
193
+ })
194
+ t .Run ("update kubeadmcontrolplanetemplate should not pass if fields are different" , func (t * testing.T ) {
195
+ g := NewWithT (t )
196
+ oldKCPTemplate := & controlplanev1.KubeadmControlPlaneTemplate {
197
+ Spec : controlplanev1.KubeadmControlPlaneTemplateSpec {
198
+ Template : controlplanev1.KubeadmControlPlaneTemplateResource {
199
+ Spec : controlplanev1.KubeadmControlPlaneTemplateResourceSpec {
200
+ MachineTemplate : & controlplanev1.KubeadmControlPlaneTemplateMachineTemplate {
201
+ NodeDrainTimeout : & metav1.Duration {Duration : time .Duration (10 ) * time .Minute },
202
+ },
203
+ },
204
+ },
205
+ },
206
+ }
207
+ newKCPTemplate := & controlplanev1.KubeadmControlPlaneTemplate {
208
+ Spec : controlplanev1.KubeadmControlPlaneTemplateSpec {
209
+ Template : controlplanev1.KubeadmControlPlaneTemplateResource {
210
+ Spec : controlplanev1.KubeadmControlPlaneTemplateResourceSpec {
211
+ KubeadmConfigSpec : bootstrapv1.KubeadmConfigSpec {
212
+ // Defaulting will set this field as well.
213
+ Format : bootstrapv1 .CloudConfig ,
214
+ // This will fail the immutability check.
215
+ PreKubeadmCommands : []string {
216
+ "new-cmd" ,
217
+ },
218
+ },
219
+ MachineTemplate : & controlplanev1.KubeadmControlPlaneTemplateMachineTemplate {
220
+ NodeDrainTimeout : & metav1.Duration {Duration : time .Duration (10 ) * time .Minute },
221
+ },
222
+ },
223
+ },
224
+ },
225
+ }
226
+ webhook := & KubeadmControlPlaneTemplate {}
227
+ warnings , err := webhook .ValidateUpdate (ctx , oldKCPTemplate , newKCPTemplate )
228
+ g .Expect (err ).To (HaveOccurred ())
229
+ g .Expect (err .Error ()).To (ContainSubstring ("KubeadmControlPlaneTemplate spec.template.spec field is immutable" ))
230
+ g .Expect (warnings ).To (BeEmpty ())
231
+ })
232
+ }
0 commit comments