Skip to content

Commit

Permalink
Fix_Bug: validate non nil AzureMachine.Spec.Diagnostic
Browse files Browse the repository at this point in the history
- when upgrading from v1alpha4 -> v1beta1, AzureMachine CRD gets updated upon cluster upgrade.
This upgrade updates AzureMachine.Spec.Diagnostics to "Managed", matching the existing behavior as implemented in #901

- This PR enables upgrading nil AzureMachine.Spec.Diagnostic to "Managed" and unblocks v1alpha4 -> v1beta1 upgrades.
- adds test cases to test validateUpdate() in case of nil AzureMachine.Spec.Diagnostics
  • Loading branch information
nawazkh committed Dec 21, 2022
1 parent e401d0b commit d92ea9a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
12 changes: 7 additions & 5 deletions api/v1beta1/azuremachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ func (m *AzureMachine) ValidateUpdate(oldRaw runtime.Object) error {
allErrs = append(allErrs, err)
}

if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "Diagnostics"),
old.Spec.Diagnostics,
m.Spec.Diagnostics); err != nil {
allErrs = append(allErrs, err)
if old.Spec.Diagnostics != nil {
if err := webhookutils.ValidateImmutable(
field.NewPath("Spec", "Diagnostics"),
old.Spec.Diagnostics,
m.Spec.Diagnostics); err != nil {
allErrs = append(allErrs, err)
}
}

if len(allErrs) == 0 {
Expand Down
42 changes: 42 additions & 0 deletions api/v1beta1/azuremachine_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,48 @@ func TestAzureMachine_ValidateUpdate(t *testing.T) {
},
wantErr: false,
},
{
name: "validTest: azuremachine.spec.Diagnostics should not error on updating nil diagnostics",
oldMachine: &AzureMachine{
Spec: AzureMachineSpec{},
},
newMachine: &AzureMachine{
Spec: AzureMachineSpec{
Diagnostics: &Diagnostics{Boot: &BootDiagnostics{StorageAccountType: ManagedDiagnosticsStorage}},
},
},
wantErr: false,
},
{
name: "invalidTest: azuremachine.spec.Diagnostics is immutable",
oldMachine: &AzureMachine{
Spec: AzureMachineSpec{
Diagnostics: &Diagnostics{},
},
},
newMachine: &AzureMachine{
Spec: AzureMachineSpec{
Diagnostics: &Diagnostics{Boot: &BootDiagnostics{StorageAccountType: ManagedDiagnosticsStorage}},
},
},
wantErr: true,
},
{
name: "invalidTest: azuremachine.spec.Diagnostics is immutable",
oldMachine: &AzureMachine{
Spec: AzureMachineSpec{
Diagnostics: &Diagnostics{
Boot: &BootDiagnostics{},
},
},
},
newMachine: &AzureMachine{
Spec: AzureMachineSpec{
Diagnostics: &Diagnostics{Boot: &BootDiagnostics{StorageAccountType: ManagedDiagnosticsStorage}},
},
},
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit d92ea9a

Please sign in to comment.