Skip to content

Commit

Permalink
WIP: return nil instead and work on new Waiter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma committed Jul 11, 2023
1 parent dbdf985 commit 31a70fa
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 66 deletions.
4 changes: 2 additions & 2 deletions api/v1beta1/azurecluster_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func (c *AzureCluster) validateCluster(old *AzureCluster) (admission.Warnings, e
allErrs = append(allErrs, c.validateClusterName()...)
allErrs = append(allErrs, c.validateClusterSpec(old)...)
if len(allErrs) == 0 {
return admission.Warnings{}, nil
return nil, nil
}

return admission.Warnings{}, apierrors.NewInvalid(
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: "infrastructure.cluster.x-k8s.io", Kind: "AzureCluster"},
c.Name, allErrs)
}
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/azurecluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (c *AzureCluster) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings
return c.validateCluster(old)
}

return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureCluster").GroupKind(), c.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureCluster").GroupKind(), c.Name, allErrs)
}

// validateSubnetUpdate validates a ClusterSpec.NetworkSpec.Subnets for immutability.
Expand Down Expand Up @@ -189,5 +189,5 @@ func (c *AzureCluster) validateSubnetUpdate(old *AzureCluster) field.ErrorList {

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *AzureCluster) ValidateDelete() (admission.Warnings, error) {
return admission.Warnings{}, nil
return nil, nil
}
4 changes: 2 additions & 2 deletions api/v1beta1/azureclusteridentity_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c *AzureClusterIdentity) validateClusterIdentity() (admission.Warnings, er
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "resourceID"), c.Spec.ResourceID))
}
if len(allErrs) == 0 {
return admission.Warnings{}, nil
return nil, nil
}
return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureClusterIdentity").GroupKind(), c.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureClusterIdentity").GroupKind(), c.Name, allErrs)
}
4 changes: 2 additions & 2 deletions api/v1beta1/azureclusteridentity_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func (c *AzureClusterIdentity) ValidateUpdate(oldRaw runtime.Object) (admission.
if len(allErrs) == 0 {
return c.validateClusterIdentity()
}
return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureClusterIdentity").GroupKind(), c.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureClusterIdentity").GroupKind(), c.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *AzureClusterIdentity) ValidateDelete() (admission.Warnings, error) {
return admission.Warnings{}, nil
return nil, nil
}
4 changes: 2 additions & 2 deletions api/v1beta1/azureclustertemplate_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func (c *AzureClusterTemplate) validateClusterTemplate() (admission.Warnings, er
allErrs = append(allErrs, c.validateClusterTemplateSpec()...)

if len(allErrs) == 0 {
return admission.Warnings{}, nil
return nil, nil
}

return admission.Warnings{}, apierrors.NewInvalid(
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: "infrastructure.cluster.x-k8s.io", Kind: "AzureClusterTemplate"},
c.Name, allErrs)
}
Expand Down
6 changes: 3 additions & 3 deletions api/v1beta1/azureclustertemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ func (c *AzureClusterTemplate) ValidateUpdate(oldRaw runtime.Object) (admission.
}

if len(allErrs) == 0 {
return admission.Warnings{}, nil
return nil, nil
}
return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureClusterTemplate").GroupKind(), c.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureClusterTemplate").GroupKind(), c.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (c *AzureClusterTemplate) ValidateDelete() (admission.Warnings, error) {
return admission.Warnings{}, nil
return nil, nil
}
16 changes: 8 additions & 8 deletions api/v1beta1/azuremachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type azureMachineWebhook struct {
func (mw *azureMachineWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
m, ok := obj.(*AzureMachine)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureMachine resource")
return nil, apierrors.NewBadRequest("expected an AzureMachine resource")
}
spec := m.Spec

Expand All @@ -67,22 +67,22 @@ func (mw *azureMachineWebhook) ValidateCreate(ctx context.Context, obj runtime.O
}

if len(allErrs) == 0 {
return admission.Warnings{}, nil
return nil, nil
}

return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureMachine").GroupKind(), m.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureMachine").GroupKind(), m.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (mw *azureMachineWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
var allErrs field.ErrorList
old, ok := oldObj.(*AzureMachine)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureMachine resource")
return nil, apierrors.NewBadRequest("expected an AzureMachine resource")
}
m, ok := newObj.(*AzureMachine)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureMachine resource")
return nil, apierrors.NewBadRequest("expected an AzureMachine resource")
}

if err := webhookutils.ValidateImmutable(
Expand Down Expand Up @@ -207,14 +207,14 @@ func (mw *azureMachineWebhook) ValidateUpdate(ctx context.Context, oldObj, newOb
}

if len(allErrs) == 0 {
return admission.Warnings{}, nil
return nil, nil
}
return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureMachine").GroupKind(), m.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureMachine").GroupKind(), m.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (mw *azureMachineWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return admission.Warnings{}, nil
return nil, nil
}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
Expand Down
12 changes: 6 additions & 6 deletions api/v1beta1/azuremachinetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func (r *AzureMachineTemplate) ValidateCreate(ctx context.Context, obj runtime.O
}

if len(allErrs) == 0 {
return admission.Warnings{}, nil
return nil, nil
}

return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureMachineTemplate").GroupKind(), t.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureMachineTemplate").GroupKind(), t.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
Expand All @@ -100,7 +100,7 @@ func (r *AzureMachineTemplate) ValidateUpdate(ctx context.Context, oldRaw runtim

req, err := admission.RequestFromContext(ctx)
if err != nil {
return admission.Warnings{}, apierrors.NewBadRequest(fmt.Sprintf("expected a admission.Request inside context: %v", err))
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a admission.Request inside context: %v", err))
}

if !topology.ShouldSkipImmutabilityChecks(req, t) &&
Expand Down Expand Up @@ -131,14 +131,14 @@ func (r *AzureMachineTemplate) ValidateUpdate(ctx context.Context, oldRaw runtim
}

if len(allErrs) == 0 {
return admission.Warnings{}, nil
return nil, nil
}
return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureMachineTemplate").GroupKind(), t.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureMachineTemplate").GroupKind(), t.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *AzureMachineTemplate) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return admission.Warnings{}, nil
return nil, nil
}

// Default implements webhookutil.defaulter so a webhook will be registered for the type.
Expand Down
10 changes: 5 additions & 5 deletions api/v1beta1/azuremanagedcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ func (r *AzureManagedCluster) ValidateCreate() (admission.Warnings, error) {
// NOTE: AzureManagedCluster 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) {
return admission.Warnings{}, field.Forbidden(
return nil, field.Forbidden(
field.NewPath("spec"),
"can be set only if the Cluster API 'MachinePool' feature flag is enabled",
)
}
return admission.Warnings{}, nil
return nil, nil
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
Expand All @@ -72,13 +72,13 @@ func (r *AzureManagedCluster) ValidateUpdate(oldRaw runtime.Object) (admission.W
}

if len(allErrs) != 0 {
return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureManagedCluster").GroupKind(), r.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureManagedCluster").GroupKind(), r.Name, allErrs)
}

return admission.Warnings{}, nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (r *AzureManagedCluster) ValidateDelete() (admission.Warnings, error) {
return admission.Warnings{}, nil
return nil, nil
}
16 changes: 8 additions & 8 deletions api/v1beta1/azuremanagedcontrolplane_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,30 @@ func (mw *azureManagedControlPlaneWebhook) Default(ctx context.Context, obj runt
func (mw *azureManagedControlPlaneWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
m, ok := obj.(*AzureManagedControlPlane)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureManagedControlPlane")
return nil, apierrors.NewBadRequest("expected an AzureManagedControlPlane")
}
// 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) {
return admission.Warnings{}, field.Forbidden(
return nil, field.Forbidden(
field.NewPath("spec"),
"can be set only if the Cluster API 'MachinePool' feature flag is enabled",
)
}

return admission.Warnings{}, m.Validate(mw.Client)
return nil, m.Validate(mw.Client)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (mw *azureManagedControlPlaneWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
var allErrs field.ErrorList
old, ok := oldObj.(*AzureManagedControlPlane)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureManagedControlPlane")
return nil, apierrors.NewBadRequest("expected an AzureManagedControlPlane")
}
m, ok := newObj.(*AzureManagedControlPlane)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureManagedControlPlane")
return nil, apierrors.NewBadRequest("expected an AzureManagedControlPlane")
}

if err := webhookutils.ValidateImmutable(
Expand Down Expand Up @@ -251,15 +251,15 @@ func (mw *azureManagedControlPlaneWebhook) ValidateUpdate(ctx context.Context, o
}

if len(allErrs) == 0 {
return admission.Warnings{}, m.Validate(mw.Client)
return nil, m.Validate(mw.Client)
}

return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureManagedControlPlane").GroupKind(), m.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureManagedControlPlane").GroupKind(), m.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (mw *azureManagedControlPlaneWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return admission.Warnings{}, nil
return nil, nil
}

// Validate the Azure Managed Control Plane and return an aggregate error.
Expand Down
20 changes: 10 additions & 10 deletions api/v1beta1/azuremanagedmachinepool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ func (mw *azureManagedMachinePoolWebhook) Default(ctx context.Context, obj runti
func (mw *azureManagedMachinePoolWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
m, ok := obj.(*AzureManagedMachinePool)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureManagedMachinePool")
return nil, apierrors.NewBadRequest("expected an AzureManagedMachinePool")
}
// NOTE: AzureManagedMachinePool 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) {
return admission.Warnings{}, field.Forbidden(
return nil, field.Forbidden(
field.NewPath("spec"),
"can be set only if the Cluster API 'MachinePool' feature flag is enabled",
)
Expand All @@ -117,18 +117,18 @@ func (mw *azureManagedMachinePoolWebhook) ValidateCreate(ctx context.Context, ob
}
}

return admission.Warnings{}, kerrors.NewAggregate(errs)
return nil, kerrors.NewAggregate(errs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (mw *azureManagedMachinePoolWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
old, ok := oldObj.(*AzureManagedMachinePool)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureManagedMachinePool")
return nil, apierrors.NewBadRequest("expected an AzureManagedMachinePool")
}
m, ok := newObj.(*AzureManagedMachinePool)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureManagedMachinePool")
return nil, apierrors.NewBadRequest("expected an AzureManagedMachinePool")
}
var allErrs field.ErrorList

Expand Down Expand Up @@ -265,23 +265,23 @@ func (mw *azureManagedMachinePoolWebhook) ValidateUpdate(ctx context.Context, ol
}

if len(allErrs) != 0 {
return admission.Warnings{}, apierrors.NewInvalid(GroupVersion.WithKind("AzureManagedMachinePool").GroupKind(), m.Name, allErrs)
return nil, apierrors.NewInvalid(GroupVersion.WithKind("AzureManagedMachinePool").GroupKind(), m.Name, allErrs)
}

return admission.Warnings{}, nil
return nil, nil
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (mw *azureManagedMachinePoolWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
m, ok := obj.(*AzureManagedMachinePool)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureManagedMachinePool")
return nil, apierrors.NewBadRequest("expected an AzureManagedMachinePool")
}
if m.Spec.Mode != string(NodePoolModeSystem) {
return admission.Warnings{}, nil
return nil, nil
}

return admission.Warnings{}, errors.Wrapf(m.validateLastSystemNodePool(mw.Client), "if the delete is triggered via owner MachinePool please refer to trouble shooting section in https://capz.sigs.k8s.io/topics/managedcluster.html")
return nil, errors.Wrapf(m.validateLastSystemNodePool(mw.Client), "if the delete is triggered via owner MachinePool please refer to trouble shooting section in https://capz.sigs.k8s.io/topics/managedcluster.html")
}

// validateLastSystemNodePool is used to check if the existing system node pool is the last system node pool.
Expand Down
12 changes: 6 additions & 6 deletions exp/api/v1beta1/azuremachinepool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,31 @@ func (ampw *azureMachinePoolWebhook) Default(ctx context.Context, obj runtime.Ob
func (ampw *azureMachinePoolWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
amp, ok := obj.(*AzureMachinePool)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureMachinePool")
return nil, apierrors.NewBadRequest("expected an AzureMachinePool")
}
// NOTE: AzureMachinePool is behind MachinePool feature gate flag; the webhook
// must prevent creating new objects in case the feature flag is disabled.
if !feature.Gates.Enabled(capifeature.MachinePool) {
return admission.Warnings{}, field.Forbidden(
return nil, field.Forbidden(
field.NewPath("spec"),
"can be set only if the MachinePool feature flag is enabled",
)
}
return admission.Warnings{}, amp.Validate(nil, ampw.Client)
return nil, amp.Validate(nil, ampw.Client)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (ampw *azureMachinePoolWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) {
amp, ok := newObj.(*AzureMachinePool)
if !ok {
return admission.Warnings{}, apierrors.NewBadRequest("expected an AzureMachinePool")
return nil, apierrors.NewBadRequest("expected an AzureMachinePool")
}
return admission.Warnings{}, amp.Validate(oldObj, ampw.Client)
return nil, amp.Validate(oldObj, ampw.Client)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
func (ampw *azureMachinePoolWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) {
return admission.Warnings{}, nil
return nil, nil
}

// Validate the Azure Machine Pool and return an aggregate error.
Expand Down
Loading

0 comments on commit 31a70fa

Please sign in to comment.