Skip to content

Commit 81c3d1c

Browse files
authored
Merge pull request #1645 from yastij/automated-cherry-pick-of-#1644-upstream-release-1.4
Automated cherry pick of #1644: Allow mutation of OS field and only when it's empty
2 parents 52affec + bd94386 commit 81c3d1c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

apis/v1beta1/vspherevm_webhook.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ func (r *VSphereVM) ValidateUpdate(old runtime.Object) error {
106106
delete(oldVSphereVMNetwork, "devices")
107107
delete(newVSphereVMNetwork, "devices")
108108

109+
// allow changes to os only if the old spec has empty OS field
110+
if _, ok := oldVSphereVMSpec["os"]; !ok {
111+
delete(oldVSphereVMSpec, "os")
112+
delete(newVSphereVMSpec, "os")
113+
}
114+
109115
if !reflect.DeepEqual(oldVSphereVMSpec, newVSphereVMSpec) {
110116
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "cannot be modified"))
111117
}

apis/v1beta1/vspherevm_webhook_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ func TestVSphereVM_ValidateUpdate(t *testing.T) {
132132
vSphereVM: createVSphereVM("vsphere-vm-1", "bar.com", biosUUID, "", []string{"192.168.0.1/32", "192.168.0.10/32"}, nil, Linux),
133133
wantErr: true,
134134
},
135+
{
136+
name: "updating OS can be done only when empty",
137+
oldVSphereVM: createVSphereVM("vsphere-vm-1-os", "foo.com", "", "", []string{"192.168.0.1/32"}, nil, ""),
138+
vSphereVM: createVSphereVM("vsphere-vm-1-os", "foo.com", "", "", []string{"192.168.0.1/32"}, nil, Linux),
139+
wantErr: false,
140+
},
141+
{
142+
name: "updating OS cannot be done when alreadySet",
143+
oldVSphereVM: createVSphereVM("vsphere-vm-1-os", "foo.com", "", "", []string{"192.168.0.1/32"}, nil, Windows),
144+
vSphereVM: createVSphereVM("vsphere-vm-1-os", "foo.com", "", "", []string{"192.168.0.1/32"}, nil, Linux),
145+
wantErr: true,
146+
},
135147
}
136148
for _, tc := range tests {
137149
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)