Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions api/v1beta2/awscluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta2

import (
"context"
"fmt"
"net"
"strings"
Expand Down Expand Up @@ -44,19 +45,26 @@ var _ = ctrl.Log.WithName("awscluster-resource")
func (r *AWSCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
WithDefaulter(r). // registers webhook.CustomDefaulter
WithValidator(r). // registers webhook.CustomValidator
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-awscluster,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusters,versions=v1beta2,name=validation.awscluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-awscluster,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusters,versions=v1beta2,name=default.awscluster.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var (
_ webhook.Validator = &AWSCluster{}
_ webhook.Defaulter = &AWSCluster{}
_ webhook.CustomValidator = &AWSCluster{}
_ webhook.CustomDefaulter = &AWSCluster{}
)

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSCluster) ValidateCreate() (admission.Warnings, error) {
func (r *AWSCluster) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
r, ok := obj.(*AWSCluster)
if !ok {
return nil, fmt.Errorf("expected *AWSCluster, got %T", obj)
}

var allErrs field.ErrorList
var allWarnings admission.Warnings

Expand All @@ -78,12 +86,17 @@ func (r *AWSCluster) ValidateCreate() (admission.Warnings, error) {
}

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

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
func (r *AWSCluster) ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) (warnings admission.Warnings, err error) {
r, ok := new.(*AWSCluster)
if !ok {
return nil, fmt.Errorf("expected *AWSCluster, got %T", new)
}

var allErrs field.ErrorList
var allWarnings admission.Warnings

Expand Down Expand Up @@ -228,8 +241,13 @@ func (r *AWSCluster) validateControlPlaneLoadBalancerUpdate(oldlb, newlb *AWSLoa
}

// Default satisfies the defaulting webhook interface.
func (r *AWSCluster) Default() {
func (r *AWSCluster) Default(ctx context.Context, obj runtime.Object) error {
r, ok := obj.(*AWSCluster)
if !ok {
return fmt.Errorf("expected *AWSCluster, got %T", obj)
}
SetObjectDefaults_AWSCluster(r)
return nil
}

func (r *AWSCluster) validateGCTasksAnnotation() field.ErrorList {
Expand Down
6 changes: 4 additions & 2 deletions api/v1beta2/awscluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import (

"sigs.k8s.io/cluster-api-provider-aws/v2/feature"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/defaulting"
// "sigs.k8s.io/cluster-api/util/defaulting"
)

func TestAWSClusterDefault(t *testing.T) {
cluster := &AWSCluster{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}}
t.Run("for AWSCluster", defaultValidateTest(cluster, true))
// t.Run("for AWSCluster", defaultValidateTest(cluster, true))
cluster.Default()
g := NewWithT(t)
g.Expect(cluster.Spec.IdentityRef).NotTo(BeNil())
Expand Down Expand Up @@ -1409,6 +1409,7 @@ func TestAWSClusterDefaultAllowedCIDRBlocks(t *testing.T) {
// update and delete.
// NOTE: This is a copy of the DefaultValidateTest function in the cluster-api
// package, but it has been modified to allow warnings to be returned.
/*
func defaultValidateTest(object defaulting.DefaultingValidator, allowWarnings bool) func(*testing.T) {
return func(t *testing.T) {
t.Helper()
Expand Down Expand Up @@ -1448,3 +1449,4 @@ func defaultValidateTest(object defaulting.DefaultingValidator, allowWarnings bo
})
}
}
*/
30 changes: 24 additions & 6 deletions api/v1beta2/awsclustercontrolleridentity_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta2

import (
"context"
"fmt"

"github.com/google/go-cmp/cmp"
Expand All @@ -36,19 +37,26 @@ var _ = ctrl.Log.WithName("awsclustercontrolleridentity-resource")
func (r *AWSClusterControllerIdentity) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
WithDefaulter(r). // registers webhook.CustomDefaulter
WithValidator(r). // registers webhook.CustomValidator
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-awsclustercontrolleridentity,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclustercontrolleridentities,versions=v1beta2,name=validation.awsclustercontrolleridentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-awsclustercontrolleridentity,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclustercontrolleridentities,versions=v1beta2,name=default.awsclustercontrolleridentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var (
_ webhook.Validator = &AWSClusterControllerIdentity{}
_ webhook.Defaulter = &AWSClusterControllerIdentity{}
_ webhook.CustomValidator = &AWSClusterControllerIdentity{}
_ webhook.CustomDefaulter = &AWSClusterControllerIdentity{}
)

// ValidateCreate will do any extra validation when creating an AWSClusterControllerIdentity.
func (r *AWSClusterControllerIdentity) ValidateCreate() (admission.Warnings, error) {
func (r *AWSClusterControllerIdentity) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
r, ok := obj.(*AWSClusterControllerIdentity)
if !ok {
return nil, fmt.Errorf("expected *AWSClusterControllerIdentity, got %T", obj)
}

// Ensures AWSClusterControllerIdentity being singleton by only allowing "default" as name
if r.Name != AWSClusterControllerIdentityName {
return nil, field.Invalid(field.NewPath("name"),
Expand All @@ -67,12 +75,17 @@ func (r *AWSClusterControllerIdentity) ValidateCreate() (admission.Warnings, err
}

// ValidateDelete allows you to add any extra validation when deleting an AWSClusterControllerIdentity.
func (r *AWSClusterControllerIdentity) ValidateDelete() (admission.Warnings, error) {
func (r *AWSClusterControllerIdentity) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
return nil, nil
}

// ValidateUpdate will do any extra validation when updating an AWSClusterControllerIdentity.
func (r *AWSClusterControllerIdentity) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
func (r *AWSClusterControllerIdentity) ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) (warnings admission.Warnings, err error) {
r, ok := new.(*AWSClusterControllerIdentity)
if !ok {
return nil, fmt.Errorf("expected *AWSClusterControllerIdentity, got %T", new)
}

oldP, ok := old.(*AWSClusterControllerIdentity)
if !ok {
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an AWSClusterControllerIdentity but got a %T", old))
Expand All @@ -99,6 +112,11 @@ func (r *AWSClusterControllerIdentity) ValidateUpdate(old runtime.Object) (admis
}

// Default will set default values for the AWSClusterControllerIdentity.
func (r *AWSClusterControllerIdentity) Default() {
func (r *AWSClusterControllerIdentity) Default(ctx context.Context, obj runtime.Object) error {
_, ok := obj.(*AWSClusterControllerIdentity)
if !ok {
return fmt.Errorf("expected *AWSClusterControllerIdentity, got %T", obj)
}
SetDefaults_Labels(&r.ObjectMeta)
return nil
}
14 changes: 8 additions & 6 deletions api/v1beta2/awsclusterroleidentity_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta2

import (
"context"
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -41,12 +42,12 @@ func (r *AWSClusterRoleIdentity) SetupWebhookWithManager(mgr ctrl.Manager) error
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-awsclusterroleidentity,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusterroleidentities,versions=v1beta2,name=default.awsclusterroleidentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var (
_ webhook.Validator = &AWSClusterRoleIdentity{}
_ webhook.Defaulter = &AWSClusterRoleIdentity{}
_ webhook.CustomValidator = &AWSClusterRoleIdentity{}
_ webhook.CustomDefaulter = &AWSClusterRoleIdentity{}
)

// ValidateCreate will do any extra validation when creating an AWSClusterRoleIdentity.
func (r *AWSClusterRoleIdentity) ValidateCreate() (admission.Warnings, error) {
func (r *AWSClusterRoleIdentity) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
if r.Spec.SourceIdentityRef == nil {
return nil, field.Invalid(field.NewPath("spec", "sourceIdentityRef"),
r.Spec.SourceIdentityRef, "field cannot be set to nil")
Expand All @@ -64,12 +65,12 @@ func (r *AWSClusterRoleIdentity) ValidateCreate() (admission.Warnings, error) {
}

// ValidateDelete allows you to add any extra validation when deleting an AWSClusterRoleIdentity.
func (r *AWSClusterRoleIdentity) ValidateDelete() (admission.Warnings, error) {
func (r *AWSClusterRoleIdentity) ValidateDelete(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
return nil, nil
}

// ValidateUpdate will do any extra validation when updating an AWSClusterRoleIdentity.
func (r *AWSClusterRoleIdentity) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
func (r *AWSClusterRoleIdentity) ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) (warnings admission.Warnings, err error) {
oldP, ok := old.(*AWSClusterRoleIdentity)
if !ok {
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an AWSClusterRoleIdentity but got a %T", old))
Expand All @@ -93,6 +94,7 @@ func (r *AWSClusterRoleIdentity) ValidateUpdate(old runtime.Object) (admission.W
}

// Default will set default values for the AWSClusterRoleIdentity.
func (r *AWSClusterRoleIdentity) Default() {
func (r *AWSClusterRoleIdentity) Default(ctx context.Context, obj runtime.Object) error {
SetDefaults_Labels(&r.ObjectMeta)
return nil
}
30 changes: 24 additions & 6 deletions api/v1beta2/awsclusterstaticidentity_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta2

import (
"context"
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -34,19 +35,26 @@ var _ = ctrl.Log.WithName("awsclusterstaticidentity-resource")
func (r *AWSClusterStaticIdentity) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
WithDefaulter(r). // registers webhook.CustomDefaulter
WithValidator(r). // registers webhook.CustomValidator
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-awsclusterstaticidentity,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusterstaticidentities,versions=v1beta2,name=validation.awsclusterstaticidentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-awsclusterstaticidentity,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclusterstaticidentities,versions=v1beta2,name=default.awsclusterstaticidentity.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var (
_ webhook.Validator = &AWSClusterStaticIdentity{}
_ webhook.Defaulter = &AWSClusterStaticIdentity{}
_ webhook.CustomValidator = &AWSClusterStaticIdentity{}
_ webhook.CustomDefaulter = &AWSClusterStaticIdentity{}
)

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSClusterStaticIdentity) ValidateCreate() (admission.Warnings, error) {
func (r *AWSClusterStaticIdentity) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
r, ok := obj.(*AWSClusterStaticIdentity)
if !ok {
return nil, fmt.Errorf("expected *AWSClusterStaticIdentity, got %T", obj)
}

// Validate selector parses as Selector
if r.Spec.AllowedNamespaces != nil {
_, err := metav1.LabelSelectorAsSelector(&r.Spec.AllowedNamespaces.Selector)
Expand All @@ -59,12 +67,17 @@ func (r *AWSClusterStaticIdentity) ValidateCreate() (admission.Warnings, error)
}

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

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSClusterStaticIdentity) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
func (r *AWSClusterStaticIdentity) ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) (warnings admission.Warnings, err error) {
r, ok := new.(*AWSClusterStaticIdentity)
if !ok {
return nil, fmt.Errorf("expected *AWSClusterStaticIdentity, got %T", new)
}

oldP, ok := old.(*AWSClusterStaticIdentity)
if !ok {
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an AWSClusterStaticIdentity but got a %T", old))
Expand All @@ -87,6 +100,11 @@ func (r *AWSClusterStaticIdentity) ValidateUpdate(old runtime.Object) (admission
}

// Default should return the default AWSClusterStaticIdentity.
func (r *AWSClusterStaticIdentity) Default() {
func (r *AWSClusterStaticIdentity) Default(ctx context.Context, obj runtime.Object) error {
r, ok := obj.(*AWSClusterStaticIdentity)
if !ok {
return fmt.Errorf("expected *AWSClusterStaticIdentity, got %T", obj)
}
SetDefaults_Labels(&r.ObjectMeta)
return nil
}
32 changes: 26 additions & 6 deletions api/v1beta2/awsclustertemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package v1beta2

import (
"context"
"fmt"

"github.com/google/go-cmp/cmp"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -29,22 +32,34 @@ import (
func (r *AWSClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
WithDefaulter(r). // registers webhook.CustomDefaulter
WithValidator(r). // registers webhook.CustomValidator
Complete()
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-awsclustertemplate,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclustertemplates,versions=v1beta2,name=validation.awsclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-awsclustertemplate,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=awsclustertemplates,versions=v1beta2,name=default.awsclustertemplate.infrastructure.cluster.x-k8s.io,sideEffects=None,admissionReviewVersions=v1;v1beta1

var _ webhook.Defaulter = &AWSClusterTemplate{}
var _ webhook.Validator = &AWSClusterTemplate{}
var _ webhook.CustomDefaulter = &AWSClusterTemplate{}
var _ webhook.CustomValidator = &AWSClusterTemplate{}

// Default implements webhook.Defaulter so a webhook will be registered for the type.
func (r *AWSClusterTemplate) Default() {
func (r *AWSClusterTemplate) Default(ctx context.Context, obj runtime.Object) error {
r, ok := obj.(*AWSClusterTemplate)
if !ok {
return fmt.Errorf("expected *AWSClusterTemplate, got %T", obj)
}
SetObjectDefaults_AWSClusterTemplate(r)
return nil
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSClusterTemplate) ValidateCreate() (admission.Warnings, error) {
func (r *AWSClusterTemplate) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
r, ok := obj.(*AWSClusterTemplate)
if !ok {
return nil, fmt.Errorf("expected *AWSClusterTemplate, got %T", obj)
}

var allErrs field.ErrorList

allErrs = append(allErrs, r.Spec.Template.Spec.Bastion.Validate()...)
Expand All @@ -54,7 +69,12 @@ func (r *AWSClusterTemplate) ValidateCreate() (admission.Warnings, error) {
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
func (r *AWSClusterTemplate) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
func (r *AWSClusterTemplate) ValidateUpdate(ctx context.Context, oldRaw runtime.Object, newRaw runtime.Object) (warnings admission.Warnings, err error) {
r, ok := newRaw.(*AWSClusterTemplate)
if !ok {
return nil, fmt.Errorf("expected *AWSClusterTemplate, got %T", newRaw)
}

old := oldRaw.(*AWSClusterTemplate)

if !cmp.Equal(r.Spec, old.Spec) {
Expand All @@ -64,6 +84,6 @@ func (r *AWSClusterTemplate) ValidateUpdate(oldRaw runtime.Object) (admission.Wa
}

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