Skip to content

Commit 0b4221c

Browse files
authored
validateUpdate returns warning on k8s version change (#433)
* validateUpdate returns warning on k8s version change * changed test to allow version update with same image
1 parent 549c52e commit 0b4221c

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

exp/api/v1beta2/ocimanagedmachinepool_webhook.go

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,32 +91,40 @@ func (m *OCIManagedMachinePool) validateVersion(allErrs field.ErrorList) field.E
9191

9292
func (m *OCIManagedMachinePool) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
9393
var allErrs field.ErrorList
94+
var warnings admission.Warnings
95+
9496
oldManagedMachinePool, ok := old.(*OCIManagedMachinePool)
9597
if !ok {
9698
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an OCIManagedMachinePool but got a %T", old))
9799
}
98100

99101
allErrs = m.validateVersion(allErrs)
102+
100103
if !reflect.DeepEqual(m.Spec.Version, oldManagedMachinePool.Spec.Version) {
101104
newImage := m.getImageId()
102105
oldImage := oldManagedMachinePool.getImageId()
103-
// if an image has been provided in updated machine pool and it matches old image id,
104-
// and if Kubernetes version has been updated, that means the image is not correct. If the version has
105-
// been changed, the image should have been updated by the user, or set as nil in which case
106-
// CAPOCI will lookup a correct image
107-
if newImage != nil && reflect.DeepEqual(newImage, oldImage) {
108-
allErrs = append(
109-
allErrs,
110-
field.Invalid(field.NewPath("spec", "nodeSourceViaImage", "imageId"),
111-
m.getImageId(), "image id has not been updated for the newer version, "+
112-
"either provide a newer image or set the field as nil"))
113106

107+
if newImage != nil && reflect.DeepEqual(newImage, oldImage) {
108+
// if an image has been provided in updated machine pool and it matches old image id,
109+
// and if Kubernetes version has been updated, that means that it might be a custom image.
110+
// This allows the use of custom images that support multiple Kubernetes versions. If the image is not a custom image
111+
// then the image should have been updated by the user, or set as nil in which case
112+
// CAPOCI will lookup a correct image.
113+
warnMsg := fmt.Sprintf("Kubernetes version was updated from %q to %q without changing the imageId. "+
114+
"If you are not using a custom multi-version image, this may cause nodepool creation to fail.",
115+
*oldManagedMachinePool.Spec.Version, *m.Spec.Version)
116+
warnings = append(warnings, warnMsg)
114117
}
115118
}
116-
if len(allErrs) == 0 {
117-
return nil, nil
119+
120+
// If there are any hard errors (like an invalid version format), return them.
121+
// Also return the list of accumulated warnings.
122+
if len(allErrs) > 0 {
123+
return warnings, apierrors.NewInvalid(m.GroupVersionKind().GroupKind(), m.Name, allErrs)
118124
}
119-
return nil, apierrors.NewInvalid(m.GroupVersionKind().GroupKind(), m.Name, allErrs)
125+
126+
// If there are no errors, return the warnings.
127+
return warnings, nil
120128
}
121129

122130
func (m *OCIManagedMachinePool) ValidateDelete() (admission.Warnings, error) {

exp/api/v1beta2/ocimanagedmachinepool_webhook_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func TestOCIManagedMachinePool_ValidateUpdate(t *testing.T) {
255255
expectErr: false,
256256
},
257257
{
258-
name: "should not allow version update with same image",
258+
name: "should allow version update with same image",
259259
m: &OCIManagedMachinePool{
260260
ObjectMeta: metav1.ObjectMeta{
261261
Name: "abcdefghijklmno",
@@ -278,7 +278,7 @@ func TestOCIManagedMachinePool_ValidateUpdate(t *testing.T) {
278278
},
279279
},
280280
},
281-
expectErr: true,
281+
expectErr: false,
282282
},
283283
}
284284
for _, test := range tests {

0 commit comments

Comments
 (0)