@@ -49,17 +49,29 @@ var (
4949 rScanInterval = regexp .MustCompile (`^(\d+)s$` )
5050)
5151
52- // SetupWebhookWithManager sets up and registers the webhook with the manager.
53- func (m * AzureManagedControlPlane ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
52+ // SetupAzureManagedControlPlaneWebhookWithManager sets up and registers the webhook with the manager.
53+ func SetupAzureManagedControlPlaneWebhookWithManager (mgr ctrl.Manager ) error {
54+ mw := & azureManagedControlPlaneWebhook {Client : mgr .GetClient ()}
5455 return ctrl .NewWebhookManagedBy (mgr ).
55- For (m ).
56+ For (& AzureManagedControlPlane {}).
57+ WithDefaulter (mw ).
58+ WithValidator (mw ).
5659 Complete ()
5760}
5861
5962// +kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedcontrolplane,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedcontrolplanes,verbs=create;update,versions=v1beta1,name=default.azuremanagedcontrolplanes.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
6063
64+ // azureManagedControlPlaneWebhook implements a validating and defaulting webhook for AzureManagedControlPlane.
65+ type azureManagedControlPlaneWebhook struct {
66+ Client client.Client
67+ }
68+
6169// Default implements webhook.Defaulter so a webhook will be registered for the type.
62- func (m * AzureManagedControlPlane ) Default (_ client.Client ) {
70+ func (mw * azureManagedControlPlaneWebhook ) Default (ctx context.Context , obj runtime.Object ) error {
71+ m , ok := obj .(* AzureManagedControlPlane )
72+ if ! ok {
73+ return apierrors .NewBadRequest ("expected an AzureManagedControlPlane" )
74+ }
6375 if m .Spec .NetworkPlugin == nil {
6476 networkPlugin := "azure"
6577 m .Spec .NetworkPlugin = & networkPlugin
@@ -83,12 +95,18 @@ func (m *AzureManagedControlPlane) Default(_ client.Client) {
8395 m .setDefaultSubnet ()
8496 m .setDefaultSku ()
8597 m .setDefaultAutoScalerProfile ()
98+
99+ return nil
86100}
87101
88102// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta1-azuremanagedcontrolplane,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=azuremanagedcontrolplanes,versions=v1beta1,name=validation.azuremanagedcontrolplanes.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
89103
90104// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
91- func (m * AzureManagedControlPlane ) ValidateCreate (client client.Client ) error {
105+ func (mw * azureManagedControlPlaneWebhook ) ValidateCreate (ctx context.Context , obj runtime.Object ) error {
106+ m , ok := obj .(* AzureManagedControlPlane )
107+ if ! ok {
108+ return apierrors .NewBadRequest ("expected an AzureManagedControlPlane" )
109+ }
92110 // NOTE: AzureManagedControlPlane relies upon MachinePools, which is behind a feature gate flag.
93111 // The webhook must prevent creating new objects in case the feature flag is disabled.
94112 if ! feature .Gates .Enabled (capifeature .MachinePool ) {
@@ -111,13 +129,20 @@ func (m *AzureManagedControlPlane) ValidateCreate(client client.Client) error {
111129 )
112130 }
113131
114- return m .Validate (client )
132+ return m .Validate (mw . Client )
115133}
116134
117135// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
118- func (m * AzureManagedControlPlane ) ValidateUpdate (oldRaw runtime. Object , client client. Client ) error {
136+ func (mw * azureManagedControlPlaneWebhook ) ValidateUpdate (ctx context. Context , oldObj , newObj runtime. Object ) error {
119137 var allErrs field.ErrorList
120- old := oldRaw .(* AzureManagedControlPlane )
138+ old , ok := oldObj .(* AzureManagedControlPlane )
139+ if ! ok {
140+ return apierrors .NewBadRequest ("expected an AzureManagedControlPlane" )
141+ }
142+ m , ok := newObj .(* AzureManagedControlPlane )
143+ if ! ok {
144+ return apierrors .NewBadRequest ("expected an AzureManagedControlPlane" )
145+ }
121146
122147 if err := webhookutils .ValidateImmutable (
123148 field .NewPath ("Spec" , "SubscriptionID" ),
@@ -244,14 +269,14 @@ func (m *AzureManagedControlPlane) ValidateUpdate(oldRaw runtime.Object, client
244269 }
245270
246271 if len (allErrs ) == 0 {
247- return m .Validate (client )
272+ return m .Validate (mw . Client )
248273 }
249274
250275 return apierrors .NewInvalid (GroupVersion .WithKind ("AzureManagedControlPlane" ).GroupKind (), m .Name , allErrs )
251276}
252277
253278// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
254- func (m * AzureManagedControlPlane ) ValidateDelete (_ client. Client ) error {
279+ func (mw * azureManagedControlPlaneWebhook ) ValidateDelete (ctx context. Context , obj runtime. Object ) error {
255280 return nil
256281}
257282
0 commit comments