Skip to content

Commit b80d1c1

Browse files
authored
Merge pull request #2146 from chrischdi/pr-vspherevm-biosuuid-webhook-change
⚠️ webhook: prevent changes to an already set VSphereVM.spec.biosUUID
2 parents e7716fb + 5d20500 commit b80d1c1

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

apis/v1beta1/vspherevm_webhook.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,24 @@ func (r *VSphereVM) ValidateUpdate(old runtime.Object) (admission.Warnings, erro
100100
return nil, apierrors.NewInternalError(errors.Wrap(err, "failed to convert old VSphereVM to unstructured object"))
101101
}
102102

103+
oldTyped, ok := old.(*VSphereVM)
104+
if !ok {
105+
return nil, apierrors.NewInternalError(fmt.Errorf("failed to typecast old runtime object to VSphereVM"))
106+
}
107+
103108
newVSphereVMSpec := newVSphereVM["spec"].(map[string]interface{})
104109
oldVSphereVMSpec := oldVSphereVM["spec"].(map[string]interface{})
105110

106-
// allow changes to biosUUID, bootstrapRef, thumbprint
107-
keys := []string{"biosUUID", "bootstrapRef", "thumbprint", "powerOffMode", "guestSoftPowerOffTimeout"}
108-
// allow changes to os only if the old spec has empty OS field
109-
if _, ok := oldVSphereVMSpec["os"]; !ok {
111+
// Allow changes to bootstrapRef, thumbprint, powerOffMode, guestSoftPowerOffTimeout.
112+
keys := []string{"bootstrapRef", "thumbprint", "powerOffMode", "guestSoftPowerOffTimeout"}
113+
// Allow changes to os only if the old spec has empty OS field.
114+
if oldTyped.Spec.OS == "" {
110115
keys = append(keys, "os")
111116
}
117+
// Allow changes to biosUUID only if it is not already set.
118+
if oldTyped.Spec.BiosUUID == "" {
119+
keys = append(keys, "biosUUID")
120+
}
112121
r.deleteSpecKeys(oldVSphereVMSpec, keys)
113122
r.deleteSpecKeys(newVSphereVMSpec, keys)
114123

apis/v1beta1/vspherevm_webhook_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ func TestVSphereVM_ValidateUpdate(t *testing.T) {
191191
vSphereVM: createVSphereVM("vsphere-vm-1", "foo.com", biosUUID, "", "BB:CC:DD:EE:FF", []string{"192.168.0.1/32"}, nil, Linux, VirtualMachinePowerOpModeSoft, nil),
192192
wantErr: false,
193193
},
194+
{
195+
name: "biosUUID can be set to a value",
196+
oldVSphereVM: createVSphereVM("vsphere-vm-1", "foo.com", "", "", "AA:BB:CC:DD:EE", []string{"192.168.0.1/32"}, nil, Linux, VirtualMachinePowerOpModeTrySoft, nil),
197+
vSphereVM: createVSphereVM("vsphere-vm-1", "foo.com", biosUUID, "", "AA:BB:CC:DD:EE", []string{"192.168.0.1/32"}, nil, Linux, VirtualMachinePowerOpModeTrySoft, nil),
198+
wantErr: false,
199+
},
200+
{
201+
name: "biosUUID cannot be updated to a different value",
202+
oldVSphereVM: createVSphereVM("vsphere-vm-1", "foo.com", "old-uuid", "", "AA:BB:CC:DD:EE", []string{"192.168.0.1/32"}, nil, Linux, VirtualMachinePowerOpModeTrySoft, nil),
203+
vSphereVM: createVSphereVM("vsphere-vm-1", "foo.com", biosUUID, "", "AA:BB:CC:DD:EE", []string{"192.168.0.1/32"}, nil, Linux, VirtualMachinePowerOpModeTrySoft, nil),
204+
wantErr: true,
205+
},
194206
}
195207
for _, tc := range tests {
196208
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)