Skip to content

Commit 053b94e

Browse files
authored
PCP-5293 Upgrade controller-runtime version to v0.20.4 (#981) (#984)
1 parent 0e3a83c commit 053b94e

24 files changed

+587
-336
lines changed

api/v1beta2/awscluster_webhook.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"context"
2021
"fmt"
2122
"net"
2223
"strings"
@@ -44,19 +45,26 @@ var _ = ctrl.Log.WithName("awscluster-resource")
4445
func (r *AWSCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
4546
return ctrl.NewWebhookManagedBy(mgr).
4647
For(r).
48+
WithDefaulter(r). // registers webhook.CustomDefaulter
49+
WithValidator(r). // registers webhook.CustomValidator
4750
Complete()
4851
}
4952

5053
// +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
5154
// +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
5255

5356
var (
54-
_ webhook.Validator = &AWSCluster{}
55-
_ webhook.Defaulter = &AWSCluster{}
57+
_ webhook.CustomValidator = &AWSCluster{}
58+
_ webhook.CustomDefaulter = &AWSCluster{}
5659
)
5760

5861
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
59-
func (r *AWSCluster) ValidateCreate() (admission.Warnings, error) {
62+
func (r *AWSCluster) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
63+
r, ok := obj.(*AWSCluster)
64+
if !ok {
65+
return nil, fmt.Errorf("expected *AWSCluster, got %T", obj)
66+
}
67+
6068
var allErrs field.ErrorList
6169
var allWarnings admission.Warnings
6270

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

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

8593
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
86-
func (r *AWSCluster) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
94+
func (r *AWSCluster) ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) (warnings admission.Warnings, err error) {
95+
r, ok := new.(*AWSCluster)
96+
if !ok {
97+
return nil, fmt.Errorf("expected *AWSCluster, got %T", new)
98+
}
99+
87100
var allErrs field.ErrorList
88101
var allWarnings admission.Warnings
89102

@@ -228,8 +241,13 @@ func (r *AWSCluster) validateControlPlaneLoadBalancerUpdate(oldlb, newlb *AWSLoa
228241
}
229242

230243
// Default satisfies the defaulting webhook interface.
231-
func (r *AWSCluster) Default() {
244+
func (r *AWSCluster) Default(ctx context.Context, obj runtime.Object) error {
245+
r, ok := obj.(*AWSCluster)
246+
if !ok {
247+
return fmt.Errorf("expected *AWSCluster, got %T", obj)
248+
}
232249
SetObjectDefaults_AWSCluster(r)
250+
return nil
233251
}
234252

235253
func (r *AWSCluster) validateGCTasksAnnotation() field.ErrorList {

api/v1beta2/awscluster_webhook_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ import (
3232

3333
"sigs.k8s.io/cluster-api-provider-aws/v2/feature"
3434
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
35-
"sigs.k8s.io/cluster-api/util/defaulting"
35+
// "sigs.k8s.io/cluster-api/util/defaulting"
3636
)
3737

3838
func TestAWSClusterDefault(t *testing.T) {
3939
cluster := &AWSCluster{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}}
40-
t.Run("for AWSCluster", defaultValidateTest(cluster, true))
40+
// t.Run("for AWSCluster", defaultValidateTest(cluster, true))
4141
cluster.Default()
4242
g := NewWithT(t)
4343
g.Expect(cluster.Spec.IdentityRef).NotTo(BeNil())
@@ -1409,6 +1409,7 @@ func TestAWSClusterDefaultAllowedCIDRBlocks(t *testing.T) {
14091409
// update and delete.
14101410
// NOTE: This is a copy of the DefaultValidateTest function in the cluster-api
14111411
// package, but it has been modified to allow warnings to be returned.
1412+
/*
14121413
func defaultValidateTest(object defaulting.DefaultingValidator, allowWarnings bool) func(*testing.T) {
14131414
return func(t *testing.T) {
14141415
t.Helper()
@@ -1448,3 +1449,4 @@ func defaultValidateTest(object defaulting.DefaultingValidator, allowWarnings bo
14481449
})
14491450
}
14501451
}
1452+
*/

api/v1beta2/awsclustercontrolleridentity_webhook.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"context"
2021
"fmt"
2122

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

4245
// +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
4346
// +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
4447

4548
var (
46-
_ webhook.Validator = &AWSClusterControllerIdentity{}
47-
_ webhook.Defaulter = &AWSClusterControllerIdentity{}
49+
_ webhook.CustomValidator = &AWSClusterControllerIdentity{}
50+
_ webhook.CustomDefaulter = &AWSClusterControllerIdentity{}
4851
)
4952

5053
// ValidateCreate will do any extra validation when creating an AWSClusterControllerIdentity.
51-
func (r *AWSClusterControllerIdentity) ValidateCreate() (admission.Warnings, error) {
54+
func (r *AWSClusterControllerIdentity) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
55+
r, ok := obj.(*AWSClusterControllerIdentity)
56+
if !ok {
57+
return nil, fmt.Errorf("expected *AWSClusterControllerIdentity, got %T", obj)
58+
}
59+
5260
// Ensures AWSClusterControllerIdentity being singleton by only allowing "default" as name
5361
if r.Name != AWSClusterControllerIdentityName {
5462
return nil, field.Invalid(field.NewPath("name"),
@@ -67,12 +75,17 @@ func (r *AWSClusterControllerIdentity) ValidateCreate() (admission.Warnings, err
6775
}
6876

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

7482
// ValidateUpdate will do any extra validation when updating an AWSClusterControllerIdentity.
75-
func (r *AWSClusterControllerIdentity) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
83+
func (r *AWSClusterControllerIdentity) ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) (warnings admission.Warnings, err error) {
84+
r, ok := new.(*AWSClusterControllerIdentity)
85+
if !ok {
86+
return nil, fmt.Errorf("expected *AWSClusterControllerIdentity, got %T", new)
87+
}
88+
7689
oldP, ok := old.(*AWSClusterControllerIdentity)
7790
if !ok {
7891
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an AWSClusterControllerIdentity but got a %T", old))
@@ -99,6 +112,11 @@ func (r *AWSClusterControllerIdentity) ValidateUpdate(old runtime.Object) (admis
99112
}
100113

101114
// Default will set default values for the AWSClusterControllerIdentity.
102-
func (r *AWSClusterControllerIdentity) Default() {
115+
func (r *AWSClusterControllerIdentity) Default(ctx context.Context, obj runtime.Object) error {
116+
_, ok := obj.(*AWSClusterControllerIdentity)
117+
if !ok {
118+
return fmt.Errorf("expected *AWSClusterControllerIdentity, got %T", obj)
119+
}
103120
SetDefaults_Labels(&r.ObjectMeta)
121+
return nil
104122
}

api/v1beta2/awsclusterroleidentity_webhook.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"context"
2021
"fmt"
2122

2223
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -41,12 +42,12 @@ func (r *AWSClusterRoleIdentity) SetupWebhookWithManager(mgr ctrl.Manager) error
4142
// +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
4243

4344
var (
44-
_ webhook.Validator = &AWSClusterRoleIdentity{}
45-
_ webhook.Defaulter = &AWSClusterRoleIdentity{}
45+
_ webhook.CustomValidator = &AWSClusterRoleIdentity{}
46+
_ webhook.CustomDefaulter = &AWSClusterRoleIdentity{}
4647
)
4748

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

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

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

9596
// Default will set default values for the AWSClusterRoleIdentity.
96-
func (r *AWSClusterRoleIdentity) Default() {
97+
func (r *AWSClusterRoleIdentity) Default(ctx context.Context, obj runtime.Object) error {
9798
SetDefaults_Labels(&r.ObjectMeta)
99+
return nil
98100
}

api/v1beta2/awsclusterstaticidentity_webhook.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"context"
2021
"fmt"
2122

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

4043
// +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
4144
// +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
4245

4346
var (
44-
_ webhook.Validator = &AWSClusterStaticIdentity{}
45-
_ webhook.Defaulter = &AWSClusterStaticIdentity{}
47+
_ webhook.CustomValidator = &AWSClusterStaticIdentity{}
48+
_ webhook.CustomDefaulter = &AWSClusterStaticIdentity{}
4649
)
4750

4851
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
49-
func (r *AWSClusterStaticIdentity) ValidateCreate() (admission.Warnings, error) {
52+
func (r *AWSClusterStaticIdentity) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
53+
r, ok := obj.(*AWSClusterStaticIdentity)
54+
if !ok {
55+
return nil, fmt.Errorf("expected *AWSClusterStaticIdentity, got %T", obj)
56+
}
57+
5058
// Validate selector parses as Selector
5159
if r.Spec.AllowedNamespaces != nil {
5260
_, err := metav1.LabelSelectorAsSelector(&r.Spec.AllowedNamespaces.Selector)
@@ -59,12 +67,17 @@ func (r *AWSClusterStaticIdentity) ValidateCreate() (admission.Warnings, error)
5967
}
6068

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

6674
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
67-
func (r *AWSClusterStaticIdentity) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
75+
func (r *AWSClusterStaticIdentity) ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) (warnings admission.Warnings, err error) {
76+
r, ok := new.(*AWSClusterStaticIdentity)
77+
if !ok {
78+
return nil, fmt.Errorf("expected *AWSClusterStaticIdentity, got %T", new)
79+
}
80+
6881
oldP, ok := old.(*AWSClusterStaticIdentity)
6982
if !ok {
7083
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an AWSClusterStaticIdentity but got a %T", old))
@@ -87,6 +100,11 @@ func (r *AWSClusterStaticIdentity) ValidateUpdate(old runtime.Object) (admission
87100
}
88101

89102
// Default should return the default AWSClusterStaticIdentity.
90-
func (r *AWSClusterStaticIdentity) Default() {
103+
func (r *AWSClusterStaticIdentity) Default(ctx context.Context, obj runtime.Object) error {
104+
r, ok := obj.(*AWSClusterStaticIdentity)
105+
if !ok {
106+
return fmt.Errorf("expected *AWSClusterStaticIdentity, got %T", obj)
107+
}
91108
SetDefaults_Labels(&r.ObjectMeta)
109+
return nil
92110
}

api/v1beta2/awsclustertemplate_webhook.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"context"
21+
"fmt"
22+
2023
"github.com/google/go-cmp/cmp"
2124
apierrors "k8s.io/apimachinery/pkg/api/errors"
2225
"k8s.io/apimachinery/pkg/runtime"
@@ -29,22 +32,34 @@ import (
2932
func (r *AWSClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
3033
return ctrl.NewWebhookManagedBy(mgr).
3134
For(r).
35+
WithDefaulter(r). // registers webhook.CustomDefaulter
36+
WithValidator(r). // registers webhook.CustomValidator
3237
Complete()
3338
}
3439

3540
// +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
3641
// +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
3742

38-
var _ webhook.Defaulter = &AWSClusterTemplate{}
39-
var _ webhook.Validator = &AWSClusterTemplate{}
43+
var _ webhook.CustomDefaulter = &AWSClusterTemplate{}
44+
var _ webhook.CustomValidator = &AWSClusterTemplate{}
4045

4146
// Default implements webhook.Defaulter so a webhook will be registered for the type.
42-
func (r *AWSClusterTemplate) Default() {
47+
func (r *AWSClusterTemplate) Default(ctx context.Context, obj runtime.Object) error {
48+
r, ok := obj.(*AWSClusterTemplate)
49+
if !ok {
50+
return fmt.Errorf("expected *AWSClusterTemplate, got %T", obj)
51+
}
4352
SetObjectDefaults_AWSClusterTemplate(r)
53+
return nil
4454
}
4555

4656
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
47-
func (r *AWSClusterTemplate) ValidateCreate() (admission.Warnings, error) {
57+
func (r *AWSClusterTemplate) ValidateCreate(ctx context.Context, obj runtime.Object) (warnings admission.Warnings, err error) {
58+
r, ok := obj.(*AWSClusterTemplate)
59+
if !ok {
60+
return nil, fmt.Errorf("expected *AWSClusterTemplate, got %T", obj)
61+
}
62+
4863
var allErrs field.ErrorList
4964

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

5671
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
57-
func (r *AWSClusterTemplate) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
72+
func (r *AWSClusterTemplate) ValidateUpdate(ctx context.Context, oldRaw runtime.Object, newRaw runtime.Object) (warnings admission.Warnings, err error) {
73+
r, ok := newRaw.(*AWSClusterTemplate)
74+
if !ok {
75+
return nil, fmt.Errorf("expected *AWSClusterTemplate, got %T", newRaw)
76+
}
77+
5878
old := oldRaw.(*AWSClusterTemplate)
5979

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

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

0 commit comments

Comments
 (0)