diff --git a/api/v1beta1/azuremanagedcontrolplanetemplate_webhook.go b/api/v1beta1/azuremanagedcontrolplanetemplate_webhook.go index 05936f0a8bd..ac8dfc1952b 100644 --- a/api/v1beta1/azuremanagedcontrolplanetemplate_webhook.go +++ b/api/v1beta1/azuremanagedcontrolplanetemplate_webhook.go @@ -17,6 +17,7 @@ limitations under the License. package v1beta1 import ( + "context" "reflect" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -32,22 +33,38 @@ import ( const AzureManagedControlPlaneTemplateImmutableMsg = "AzureManagedControlPlaneTemplate spec.template.spec field is immutable. Please create new resource instead. ref doc: https://cluster-api.sigs.k8s.io/tasks/experimental-features/cluster-class/change-clusterclass.html" // SetupWebhookWithManager will set up the webhook to be managed by the specified manager. -func (mcp *AzureManagedControlPlaneTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error { +func SetupAzureManagedControlPlaneTemplateWithManager(mgr ctrl.Manager) error { + mcpw := &azureManagedControlPlaneTemplateWebhook{Client: mgr.GetClient()} return ctrl.NewWebhookManagedBy(mgr). - For(mcp). + For(&AzureManagedControlPlaneTemplate{}). + WithDefaulter(mcpw). + WithValidator(mcpw). Complete() } // +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedcontrolplanetemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedcontrolplanetemplates,versions=v1beta1,name=validation.azuremanagedcontrolplanetemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 // +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedcontrolplanetemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedcontrolplanetemplates,versions=v1beta1,name=default.azuremanagedcontrolplanetemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1 +type azureManagedControlPlaneTemplateWebhook struct { + Client client.Client +} + // Default implements webhook.Defaulter so a webhook will be registered for the type. -func (mcp *AzureManagedControlPlaneTemplate) Default(_ client.Client) { +func (mcpw *azureManagedControlPlaneTemplateWebhook) Default(ctx context.Context, obj runtime.Object) error { + mcp, ok := obj.(*AzureManagedControlPlaneTemplate) + if !ok { + return apierrors.NewBadRequest("expected an AzureManagedControlPlaneTemplate") + } mcp.setDefaults() + return nil } // ValidateCreate implements webhook.Validator so a webhook will be registered for the type. -func (mcp *AzureManagedControlPlaneTemplate) ValidateCreate(client client.Client) error { +func (mcpw *azureManagedControlPlaneTemplateWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) error { + mcp, ok := obj.(*AzureManagedControlPlaneTemplate) + if !ok { + return apierrors.NewBadRequest("expected an AzureManagedControlPlaneTemplate") + } // NOTE: AzureManagedControlPlane relies upon MachinePools, which is behind a feature gate flag. // The webhook must prevent creating new objects in case the feature flag is disabled. if !feature.Gates.Enabled(capifeature.MachinePool) { @@ -57,13 +74,20 @@ func (mcp *AzureManagedControlPlaneTemplate) ValidateCreate(client client.Client ) } - return mcp.validateManagedControlPlaneTemplate(client) + return mcp.validateManagedControlPlaneTemplate(mcpw.Client) } // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type. -func (mcp *AzureManagedControlPlaneTemplate) ValidateUpdate(oldRaw runtime.Object, _ client.Client) error { +func (mcpw *azureManagedControlPlaneTemplateWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) error { var allErrs field.ErrorList - old := oldRaw.(*AzureManagedControlPlaneTemplate) + old, ok := oldObj.(*AzureManagedControlPlaneTemplate) + if !ok { + return apierrors.NewBadRequest("expected an AzureManagedControlPlaneTemplate") + } + mcp, ok := newObj.(*AzureManagedControlPlaneTemplate) + if !ok { + return apierrors.NewBadRequest("expected an AzureManagedControlPlaneTemplate") + } if !reflect.DeepEqual(mcp.Spec.Template.Spec, old.Spec.Template.Spec) { allErrs = append(allErrs, field.Invalid(field.NewPath("AzureManagedControlPlaneTemplate", "spec", "template", "spec"), mcp, AzureManagedControlPlaneTemplateImmutableMsg), @@ -77,6 +101,6 @@ func (mcp *AzureManagedControlPlaneTemplate) ValidateUpdate(oldRaw runtime.Objec } // ValidateDelete implements webhook.Validator so a webhook will be registered for the type. -func (mcp *AzureManagedControlPlaneTemplate) ValidateDelete(_ client.Client) error { +func (mcpw *azureManagedControlPlaneTemplateWebhook) ValidateDelete(ctx context.Context, _ runtime.Object) error { return nil } diff --git a/main.go b/main.go index b81da918138..4c1521c84b6 100644 --- a/main.go +++ b/main.go @@ -537,6 +537,11 @@ func registerWebhooks(mgr manager.Manager) { os.Exit(1) } + if err := infrav1.SetupAzureManagedControlPlaneTemplateWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create webhook", "webhook", "AzureManagedControlPlaneTemplate") + os.Exit(1) + } + if err := mgr.AddReadyzCheck("webhook", mgr.GetWebhookServer().StartedChecker()); err != nil { setupLog.Error(err, "unable to create ready check") os.Exit(1)