diff --git a/api/v1beta3/cloudstackcluster_webhook.go b/api/v1beta3/cloudstackcluster_webhook.go index a8c85517..4cfd4cfb 100644 --- a/api/v1beta3/cloudstackcluster_webhook.go +++ b/api/v1beta3/cloudstackcluster_webhook.go @@ -52,7 +52,7 @@ var ( ) // Default implements webhook.Defaulter so a webhook will be registered for the type. -func (r *CloudStackClusterWebhook) Default(ctx context.Context, objRaw runtime.Object) error { +func (r *CloudStackClusterWebhook) Default(_ context.Context, objRaw runtime.Object) error { obj, ok := objRaw.(*CloudStackCluster) if !ok { return apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackCluster but got a %T", objRaw)) @@ -64,7 +64,7 @@ func (r *CloudStackClusterWebhook) Default(ctx context.Context, objRaw runtime.O } // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. -func (r *CloudStackClusterWebhook) ValidateCreate(ctx context.Context, objRaw runtime.Object) (admission.Warnings, error) { +func (r *CloudStackClusterWebhook) ValidateCreate(_ context.Context, objRaw runtime.Object) (admission.Warnings, error) { obj, ok := objRaw.(*CloudStackCluster) if !ok { return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackCluster but got a %T", objRaw)) @@ -76,18 +76,18 @@ func (r *CloudStackClusterWebhook) ValidateCreate(ctx context.Context, objRaw ru } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. -func (r *CloudStackClusterWebhook) ValidateUpdate(ctx context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { - old, ok := oldRaw.(*CloudStackCluster) +func (r *CloudStackClusterWebhook) ValidateUpdate(_ context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { + oldObj, ok := oldRaw.(*CloudStackCluster) if !ok { return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackCluster but got a %T", oldRaw)) } - new, ok := newRaw.(*CloudStackCluster) + newObj, ok := newRaw.(*CloudStackCluster) if !ok { return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackCluster but got a %T", newRaw)) } - spec := new.Spec + spec := newObj.Spec - oldSpec := old.Spec + oldSpec := oldObj.Spec errorList := field.ErrorList(nil) @@ -103,18 +103,18 @@ func (r *CloudStackClusterWebhook) ValidateUpdate(ctx context.Context, oldRaw ru "controlplaneendpoint.port", errorList) } - if annotations.IsExternallyManaged(old) && !annotations.IsExternallyManaged(new) { + if annotations.IsExternallyManaged(oldObj) && !annotations.IsExternallyManaged(newObj) { errorList = append(errorList, field.Forbidden(field.NewPath("metadata", "annotations"), "removal of externally managed (managed-by) annotation is not allowed"), ) } - return nil, webhookutil.AggregateObjErrors(new.GroupVersionKind().GroupKind(), new.Name, errorList) + return nil, webhookutil.AggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, errorList) } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. -func (r *CloudStackClusterWebhook) ValidateDelete(ctx context.Context, _ runtime.Object) (admission.Warnings, error) { +func (r *CloudStackClusterWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { return nil, nil } diff --git a/api/v1beta3/cloudstackclustertemplate_webhook.go b/api/v1beta3/cloudstackclustertemplate_webhook.go index cdf07c73..65f62669 100644 --- a/api/v1beta3/cloudstackclustertemplate_webhook.go +++ b/api/v1beta3/cloudstackclustertemplate_webhook.go @@ -24,10 +24,11 @@ import ( apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/validation/field" - "sigs.k8s.io/cluster-api-provider-cloudstack/pkg/webhookutil" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/webhook" "sigs.k8s.io/controller-runtime/pkg/webhook/admission" + + "sigs.k8s.io/cluster-api-provider-cloudstack/pkg/webhookutil" ) const cloudStackClusterTemplateImmutableMsg = "CloudStackClusterTemplate spec.template.spec field is immutable. Please create new resource instead." @@ -47,7 +48,7 @@ func (r *CloudStackClusterTemplateWebhook) SetupWebhookWithManager(mgr ctrl.Mana var _ webhook.CustomDefaulter = &CloudStackClusterTemplateWebhook{} // Default implements webhook.CustomDefaulter so a webhook will be registered for the type. -func (r *CloudStackClusterTemplateWebhook) Default(ctx context.Context, objRaw runtime.Object) error { +func (r *CloudStackClusterTemplateWebhook) Default(_ context.Context, objRaw runtime.Object) error { obj, ok := objRaw.(*CloudStackClusterTemplate) if !ok { return apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackClusterTemplate but got a %T", objRaw)) @@ -62,7 +63,7 @@ func (r *CloudStackClusterTemplateWebhook) Default(ctx context.Context, objRaw r var _ webhook.CustomValidator = &CloudStackClusterTemplateWebhook{} // ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type. -func (r *CloudStackClusterTemplateWebhook) ValidateCreate(ctx context.Context, objRaw runtime.Object) (admission.Warnings, error) { +func (r *CloudStackClusterTemplateWebhook) ValidateCreate(_ context.Context, objRaw runtime.Object) (admission.Warnings, error) { obj, ok := objRaw.(*CloudStackClusterTemplate) if !ok { return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackClusterTemplate but got a %T", objRaw)) @@ -73,7 +74,7 @@ func (r *CloudStackClusterTemplateWebhook) ValidateCreate(ctx context.Context, o } // ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type. -func (r *CloudStackClusterTemplateWebhook) ValidateUpdate(ctx context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { +func (r *CloudStackClusterTemplateWebhook) ValidateUpdate(_ context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { var allErrs field.ErrorList newObj, ok := newRaw.(*CloudStackClusterTemplate) if !ok { @@ -93,6 +94,6 @@ func (r *CloudStackClusterTemplateWebhook) ValidateUpdate(ctx context.Context, o } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. -func (r *CloudStackClusterTemplateWebhook) ValidateDelete(ctx context.Context, _ runtime.Object) (admission.Warnings, error) { +func (r *CloudStackClusterTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { return nil, nil } diff --git a/api/v1beta3/cloudstackclustertemplate_webhook_test.go b/api/v1beta3/cloudstackclustertemplate_webhook_test.go index 4a85d58d..d9f99048 100644 --- a/api/v1beta3/cloudstackclustertemplate_webhook_test.go +++ b/api/v1beta3/cloudstackclustertemplate_webhook_test.go @@ -81,7 +81,7 @@ func TestCloudStackClusterTemplateFeatureGateEnabled(t *testing.T) { } for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { + t.Run(tt.name, func(_ *testing.T) { webhook := &CloudStackClusterTemplateWebhook{} warnings, err := webhook.ValidateCreate(ctx, tt.template) if tt.wantError { diff --git a/api/v1beta3/cloudstackmachine_webhook.go b/api/v1beta3/cloudstackmachine_webhook.go index 56a4172c..89f72065 100644 --- a/api/v1beta3/cloudstackmachine_webhook.go +++ b/api/v1beta3/cloudstackmachine_webhook.go @@ -45,7 +45,7 @@ func (r *CloudStackMachineWebhook) SetupWebhookWithManager(mgr ctrl.Manager) err var _ webhook.CustomValidator = &CloudStackMachineWebhook{} // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. -func (r *CloudStackMachineWebhook) ValidateCreate(ctx context.Context, objRaw runtime.Object) (admission.Warnings, error) { +func (r *CloudStackMachineWebhook) ValidateCreate(_ context.Context, objRaw runtime.Object) (admission.Warnings, error) { obj, ok := objRaw.(*CloudStackMachine) if !ok { return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackMachine but got a %T", objRaw)) @@ -63,44 +63,44 @@ func (r *CloudStackMachineWebhook) ValidateCreate(ctx context.Context, objRaw ru } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. -func (r *CloudStackMachineWebhook) ValidateUpdate(ctx context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { - old, ok := oldRaw.(*CloudStackMachine) +func (r *CloudStackMachineWebhook) ValidateUpdate(_ context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { + oldObj, ok := oldRaw.(*CloudStackMachine) if !ok { return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackMachine but got a %T", oldRaw)) } - new, ok := newRaw.(*CloudStackMachine) + newObj, ok := newRaw.(*CloudStackMachine) if !ok { return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackMachine but got a %T", newRaw)) } var errorList field.ErrorList - oldSpec := old.Spec - - errorList = webhookutil.EnsureEqualStrings(new.Spec.Offering.ID, oldSpec.Offering.ID, "offering", errorList) - errorList = webhookutil.EnsureEqualStrings(new.Spec.Offering.Name, oldSpec.Offering.Name, "offering", errorList) - if new.Spec.DiskOffering != nil { - errorList = webhookutil.EnsureEqualStrings(new.Spec.DiskOffering.ID, oldSpec.DiskOffering.ID, "diskOffering", errorList) - errorList = webhookutil.EnsureEqualStrings(new.Spec.DiskOffering.Name, oldSpec.DiskOffering.Name, "diskOffering", errorList) - errorList = webhookutil.EnsureIntFieldsAreNotNegative(new.Spec.DiskOffering.CustomSize, "customSizeInGB", errorList) - errorList = webhookutil.EnsureEqualStrings(new.Spec.DiskOffering.MountPath, oldSpec.DiskOffering.MountPath, "mountPath", errorList) - errorList = webhookutil.EnsureEqualStrings(new.Spec.DiskOffering.Device, oldSpec.DiskOffering.Device, "device", errorList) - errorList = webhookutil.EnsureEqualStrings(new.Spec.DiskOffering.Filesystem, oldSpec.DiskOffering.Filesystem, "filesystem", errorList) - errorList = webhookutil.EnsureEqualStrings(new.Spec.DiskOffering.Label, oldSpec.DiskOffering.Label, "label", errorList) + oldSpec := oldObj.Spec + + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.Offering.ID, oldSpec.Offering.ID, "offering", errorList) + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.Offering.Name, oldSpec.Offering.Name, "offering", errorList) + if newObj.Spec.DiskOffering != nil { + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.DiskOffering.ID, oldSpec.DiskOffering.ID, "diskOffering", errorList) + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.DiskOffering.Name, oldSpec.DiskOffering.Name, "diskOffering", errorList) + errorList = webhookutil.EnsureIntFieldsAreNotNegative(newObj.Spec.DiskOffering.CustomSize, "customSizeInGB", errorList) + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.DiskOffering.MountPath, oldSpec.DiskOffering.MountPath, "mountPath", errorList) + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.DiskOffering.Device, oldSpec.DiskOffering.Device, "device", errorList) + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.DiskOffering.Filesystem, oldSpec.DiskOffering.Filesystem, "filesystem", errorList) + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.DiskOffering.Label, oldSpec.DiskOffering.Label, "label", errorList) } - errorList = webhookutil.EnsureEqualStrings(new.Spec.SSHKey, oldSpec.SSHKey, "sshkey", errorList) - errorList = webhookutil.EnsureEqualStrings(new.Spec.Template.ID, oldSpec.Template.ID, "template", errorList) - errorList = webhookutil.EnsureEqualStrings(new.Spec.Template.Name, oldSpec.Template.Name, "template", errorList) - errorList = webhookutil.EnsureEqualMapStringString(new.Spec.Details, oldSpec.Details, "details", errorList) - errorList = webhookutil.EnsureEqualStrings(new.Spec.Affinity, oldSpec.Affinity, "affinity", errorList) + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.SSHKey, oldSpec.SSHKey, "sshkey", errorList) + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.Template.ID, oldSpec.Template.ID, "template", errorList) + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.Template.Name, oldSpec.Template.Name, "template", errorList) + errorList = webhookutil.EnsureEqualMapStringString(newObj.Spec.Details, oldSpec.Details, "details", errorList) + errorList = webhookutil.EnsureEqualStrings(newObj.Spec.Affinity, oldSpec.Affinity, "affinity", errorList) - if !reflect.DeepEqual(new.Spec.AffinityGroupIDs, oldSpec.AffinityGroupIDs) { // Equivalent to other Ensure funcs. + if !reflect.DeepEqual(newObj.Spec.AffinityGroupIDs, oldSpec.AffinityGroupIDs) { // Equivalent to other Ensure funcs. errorList = append(errorList, field.Forbidden(field.NewPath("spec", "AffinityGroupIDs"), "AffinityGroupIDs")) } - return nil, webhookutil.AggregateObjErrors(new.GroupVersionKind().GroupKind(), new.Name, errorList) + return nil, webhookutil.AggregateObjErrors(newObj.GroupVersionKind().GroupKind(), newObj.Name, errorList) } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. -func (r *CloudStackMachineWebhook) ValidateDelete(ctx context.Context, _ runtime.Object) (admission.Warnings, error) { +func (r *CloudStackMachineWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { return nil, nil } diff --git a/api/v1beta3/cloudstackmachinetemplate_webhook.go b/api/v1beta3/cloudstackmachinetemplate_webhook.go index a76c61e3..57d5e1d8 100644 --- a/api/v1beta3/cloudstackmachinetemplate_webhook.go +++ b/api/v1beta3/cloudstackmachinetemplate_webhook.go @@ -48,7 +48,7 @@ func (r *CloudStackMachineTemplateWebhook) SetupWebhookWithManager(mgr ctrl.Mana var _ webhook.CustomValidator = &CloudStackMachineTemplateWebhook{} // ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type. -func (r *CloudStackMachineTemplateWebhook) ValidateCreate(ctx context.Context, objRaw runtime.Object) (admission.Warnings, error) { +func (r *CloudStackMachineTemplateWebhook) ValidateCreate(_ context.Context, objRaw runtime.Object) (admission.Warnings, error) { obj, ok := objRaw.(*CloudStackMachineTemplate) if !ok { return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackMachineTemplate but got a %T", objRaw)) @@ -76,7 +76,7 @@ func (r *CloudStackMachineTemplateWebhook) ValidateCreate(ctx context.Context, o } // ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type. -func (r *CloudStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { +func (r *CloudStackMachineTemplateWebhook) ValidateUpdate(_ context.Context, oldRaw runtime.Object, newRaw runtime.Object) (admission.Warnings, error) { obj, ok := newRaw.(*CloudStackMachineTemplate) if !ok { return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a CloudStackMachineTemplate but got a %T", newRaw)) @@ -96,6 +96,6 @@ func (r *CloudStackMachineTemplateWebhook) ValidateUpdate(ctx context.Context, o } // ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type. -func (r *CloudStackMachineTemplateWebhook) ValidateDelete(ctx context.Context, _ runtime.Object) (admission.Warnings, error) { +func (r *CloudStackMachineTemplateWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { return nil, nil } diff --git a/api/v1beta3/webhook_suite_test.go b/api/v1beta3/webhook_suite_test.go index 231ebcf7..4a9233b1 100644 --- a/api/v1beta3/webhook_suite_test.go +++ b/api/v1beta3/webhook_suite_test.go @@ -27,7 +27,6 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - //+kubebuilder:scaffold:imports admissionv1 "k8s.io/api/admission/v1" apimachineryruntime "k8s.io/apimachinery/pkg/runtime"