Skip to content

Commit

Permalink
Update webhook to use custom defaulter
Browse files Browse the repository at this point in the history
  • Loading branch information
willie-yao committed May 23, 2023
1 parent 092ca98 commit 4f70a4b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
40 changes: 32 additions & 8 deletions api/v1beta1/azuremanagedcontrolplanetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
"context"
"reflect"

apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -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) {
Expand All @@ -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),
Expand All @@ -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
}
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,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)
Expand Down

0 comments on commit 4f70a4b

Please sign in to comment.