Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validations for immutable AzureCluster Updates #1098

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion api/v1alpha3/azurecluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ limitations under the License.
package v1alpha3

import (
"reflect"

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
Expand Down Expand Up @@ -56,8 +60,35 @@ func (c *AzureCluster) ValidateCreate() error {
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (c *AzureCluster) ValidateUpdate(oldRaw runtime.Object) error {
clusterlog.Info("validate update", "name", c.Name)
var allErrs field.ErrorList
old := oldRaw.(*AzureCluster)
return c.validateCluster(old)
furkatgofurov7 marked this conversation as resolved.
Show resolved Hide resolved

if !reflect.DeepEqual(c.Spec.ResourceGroup, old.Spec.ResourceGroup) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "ResourceGroup"),
c.Spec.ResourceGroup, "field is immutable"),
)
}

if !reflect.DeepEqual(c.Spec.SubscriptionID, old.Spec.SubscriptionID) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "SubscriptionID"),
c.Spec.SubscriptionID, "field is immutable"),
)
}

if !reflect.DeepEqual(c.Spec.Location, old.Spec.Location) {
allErrs = append(allErrs,
field.Invalid(field.NewPath("spec", "Location"),
c.Spec.Location, "field is immutable"),
)
}

if len(allErrs) == 0 {
return c.validateCluster(old)
}

return apierrors.NewInvalid(GroupVersion.WithKind("AzureCluster").GroupKind(), c.Name, allErrs)
furkatgofurov7 marked this conversation as resolved.
Show resolved Hide resolved
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
Expand Down
52 changes: 48 additions & 4 deletions api/v1alpha3/azurecluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ func TestAzureCluster_ValidateUpdate(t *testing.T) {
g := NewWithT(t)

tests := []struct {
name string
cluster *AzureCluster
wantErr bool
name string
oldCluster *AzureCluster
cluster *AzureCluster
wantErr bool
}{
{
name: "azurecluster with pre-existing vnet - valid spec",
Expand Down Expand Up @@ -157,10 +158,53 @@ func TestAzureCluster_ValidateUpdate(t *testing.T) {
}(),
wantErr: true,
},
{
name: "azurecluster resource group is immutable",
oldCluster: &AzureCluster{
Spec: AzureClusterSpec{
ResourceGroup: "demoResourceGroup",
},
},
cluster: &AzureCluster{
Spec: AzureClusterSpec{
ResourceGroup: "demoResourceGroup-2",
},
},
wantErr: true,
},
{
name: "azurecluster subscription ID is immutable",
oldCluster: &AzureCluster{
Spec: AzureClusterSpec{
SubscriptionID: "212ec1q8",
},
},
cluster: &AzureCluster{
Spec: AzureClusterSpec{
SubscriptionID: "212ec1q9",
},
},
wantErr: true,
},
{
name: "azurecluster location is immutable",
oldCluster: &AzureCluster{
Spec: AzureClusterSpec{
Location: "North Europe",
},
},
cluster: &AzureCluster{
Spec: AzureClusterSpec{
Location: "West Europe",
},
},
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := tc.cluster.ValidateUpdate(createValidCluster())
t.Parallel()
err := tc.cluster.ValidateUpdate(tc.oldCluster)
if tc.wantErr {
g.Expect(err).To(HaveOccurred())
} else {
Expand Down