Skip to content

Commit fc8168b

Browse files
committed
Remove usage of deprecated webhook.Validator and webhook.Defaulter interfaces
1 parent 6cbfe19 commit fc8168b

23 files changed

+255
-252
lines changed

api/v1beta2/groupversion_info.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,18 @@ limitations under the License.
2020
package v1beta2
2121

2222
import (
23-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
"k8s.io/apimachinery/pkg/runtime"
2523
"k8s.io/apimachinery/pkg/runtime/schema"
24+
25+
"sigs.k8s.io/controller-runtime/pkg/scheme"
2626
)
2727

2828
var (
2929
// GroupVersion is group version used to register these objects.
3030
GroupVersion = schema.GroupVersion{Group: "infrastructure.cluster.x-k8s.io", Version: "v1beta2"}
3131

32-
// schemeBuilder is used to add go types to the GroupVersionKind scheme.
33-
schemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
32+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
33+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
3434

3535
// AddToScheme adds the types in this group-version to the given scheme.
36-
AddToScheme = schemeBuilder.AddToScheme
37-
38-
objectTypes = []runtime.Object{}
36+
AddToScheme = SchemeBuilder.AddToScheme
3937
)
40-
41-
func addKnownTypes(scheme *runtime.Scheme) error {
42-
scheme.AddKnownTypes(GroupVersion, objectTypes...)
43-
metav1.AddToGroupVersion(scheme, GroupVersion)
44-
return nil
45-
}

api/v1beta2/ibmpowervscluster_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,5 +338,5 @@ func (rf *ResourceReference) Set(resource ResourceReference) {
338338
}
339339

340340
func init() {
341-
objectTypes = append(objectTypes, &IBMPowerVSCluster{}, &IBMPowerVSClusterList{})
341+
SchemeBuilder.Register(&IBMPowerVSCluster{}, &IBMPowerVSClusterList{})
342342
}

api/v1beta2/ibmpowervscluster_webhook.go

Lines changed: 27 additions & 26 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
"strconv"
2223

@@ -28,51 +29,51 @@ import (
2829
"k8s.io/apimachinery/pkg/util/validation/field"
2930

3031
ctrl "sigs.k8s.io/controller-runtime"
31-
logf "sigs.k8s.io/controller-runtime/pkg/log"
3232
"sigs.k8s.io/controller-runtime/pkg/webhook"
3333
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3434

3535
genUtil "sigs.k8s.io/cluster-api-provider-ibmcloud/util"
3636
)
3737

38-
// log is for logging in this package.
39-
var ibmpowervsclusterlog = logf.Log.WithName("ibmpowervscluster-resource")
38+
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervscluster,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclusters,verbs=create;update,versions=v1beta2,name=mibmpowervscluster.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
39+
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervscluster,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclusters,versions=v1beta2,name=vibmpowervscluster.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
4040

4141
func (r *IBMPowerVSCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
4242
return ctrl.NewWebhookManagedBy(mgr).
43-
For(r).
43+
For(&IBMPowerVSCluster{}).
44+
WithValidator(r).
45+
WithDefaulter(r).
4446
Complete()
4547
}
4648

47-
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervscluster,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclusters,verbs=create;update,versions=v1beta2,name=mibmpowervscluster.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
49+
var _ webhook.CustomDefaulter = &IBMPowerVSCluster{}
50+
var _ webhook.CustomValidator = &IBMPowerVSCluster{}
4851

49-
var _ webhook.Defaulter = &IBMPowerVSCluster{}
50-
51-
// Default implements webhook.Defaulter so a webhook will be registered for the type.
52-
func (r *IBMPowerVSCluster) Default() {
53-
ibmpowervsclusterlog.Info("default", "name", r.Name)
52+
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type.
53+
func (r *IBMPowerVSCluster) Default(_ context.Context, _ runtime.Object) error {
54+
return nil
5455
}
5556

56-
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
57-
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervscluster,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclusters,versions=v1beta2,name=vibmpowervscluster.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
58-
59-
var _ webhook.Validator = &IBMPowerVSCluster{}
60-
61-
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
62-
func (r *IBMPowerVSCluster) ValidateCreate() (admission.Warnings, error) {
63-
ibmpowervsclusterlog.Info("validate create", "name", r.Name)
64-
return r.validateIBMPowerVSCluster()
57+
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
58+
func (r *IBMPowerVSCluster) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
59+
objValue, ok := obj.(*IBMPowerVSCluster)
60+
if !ok {
61+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a IBMPowerVSCluster but got a %T", obj))
62+
}
63+
return objValue.validateIBMPowerVSCluster()
6564
}
6665

67-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
68-
func (r *IBMPowerVSCluster) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
69-
ibmpowervsclusterlog.Info("validate update", "name", r.Name)
70-
return r.validateIBMPowerVSCluster()
66+
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
67+
func (r *IBMPowerVSCluster) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (warnings admission.Warnings, err error) {
68+
objValue, ok := newObj.(*IBMPowerVSCluster)
69+
if !ok {
70+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a IBMPowerVSCluster but got a %T", newObj))
71+
}
72+
return objValue.validateIBMPowerVSCluster()
7173
}
7274

73-
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
74-
func (r *IBMPowerVSCluster) ValidateDelete() (admission.Warnings, error) {
75-
ibmpowervsclusterlog.Info("validate delete", "name", r.Name)
75+
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
76+
func (r *IBMPowerVSCluster) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
7677
return nil, nil
7778
}
7879

api/v1beta2/ibmpowervsclustertemplate_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ type IBMPowerVSClusterTemplateResource struct {
5959
}
6060

6161
func init() {
62-
objectTypes = append(objectTypes, &IBMPowerVSClusterTemplate{}, &IBMPowerVSClusterTemplateList{})
62+
SchemeBuilder.Register(&IBMPowerVSClusterTemplate{}, &IBMPowerVSClusterTemplateList{})
6363
}

api/v1beta2/ibmpowervsclustertemplate_webhook.go

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

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

2324
apierrors "k8s.io/apimachinery/pkg/api/errors"
2425
"k8s.io/apimachinery/pkg/runtime"
2526

2627
ctrl "sigs.k8s.io/controller-runtime"
27-
logf "sigs.k8s.io/controller-runtime/pkg/log"
2828
"sigs.k8s.io/controller-runtime/pkg/webhook"
2929
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
3030
)
3131

32-
// log is for logging in this package.
33-
var ibmpowervsclustertemplatelog = logf.Log.WithName("ibmpowervsclustertemplate-resource")
32+
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclustertemplates,verbs=create;update,versions=v1beta2,name=mibmpowervsclustertemplate.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
33+
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclustertemplates,versions=v1beta2,name=vibmpowervsclustertemplate.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3434

3535
func (r *IBMPowerVSClusterTemplate) SetupWebhookWithManager(mgr ctrl.Manager) error {
3636
return ctrl.NewWebhookManagedBy(mgr).
37-
For(r).
37+
For(&IBMPowerVSClusterTemplate{}).
38+
WithValidator(r).
39+
WithDefaulter(r).
3840
Complete()
3941
}
4042

41-
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclustertemplates,verbs=create;update,versions=v1beta2,name=mibmpowervsclustertemplate.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
42-
43-
var _ webhook.Defaulter = &IBMPowerVSClusterTemplate{}
43+
var _ webhook.CustomDefaulter = &IBMPowerVSClusterTemplate{}
44+
var _ webhook.CustomValidator = &IBMPowerVSClusterTemplate{}
4445

45-
// Default implements webhook.Defaulter so a webhook will be registered for the type.
46-
func (r *IBMPowerVSClusterTemplate) Default() {
47-
ibmpowervsclustertemplatelog.Info("default", "name", r.Name)
46+
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type.
47+
func (r *IBMPowerVSClusterTemplate) Default(_ context.Context, _ runtime.Object) error {
48+
return nil
4849
}
4950

50-
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsclustertemplate,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsclustertemplates,versions=v1beta2,name=vibmpowervsclustertemplate.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
51-
52-
var _ webhook.Validator = &IBMPowerVSClusterTemplate{}
53-
54-
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
55-
func (r *IBMPowerVSClusterTemplate) ValidateCreate() (admission.Warnings, error) {
56-
ibmpowervsclustertemplatelog.Info("validate create", "name", r.Name)
57-
51+
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
52+
func (r *IBMPowerVSClusterTemplate) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
5853
return nil, nil
5954
}
6055

61-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
62-
func (r *IBMPowerVSClusterTemplate) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings, error) {
63-
ibmpowervsclustertemplatelog.Info("validate update", "name", r.Name)
64-
old, ok := oldRaw.(*IBMPowerVSClusterTemplate)
56+
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
57+
func (r *IBMPowerVSClusterTemplate) ValidateUpdate(_ context.Context, oldObj, newObj runtime.Object) (warnings admission.Warnings, err error) {
58+
oldObjValue, ok := oldObj.(*IBMPowerVSClusterTemplate)
6559
if !ok {
66-
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an IBMPowerVSClusterTemplate but got a %T", oldRaw))
60+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an IBMPowerVSClusterTemplate but got a %T", oldObj))
6761
}
68-
if !reflect.DeepEqual(r.Spec, old.Spec) {
62+
newObjValue, ok := newObj.(*IBMPowerVSClusterTemplate)
63+
if !ok {
64+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected an IBMPowerVSClusterTemplate but got a %T", newObj))
65+
}
66+
if !reflect.DeepEqual(newObjValue.Spec, oldObjValue.Spec) {
6967
return nil, apierrors.NewBadRequest("IBMPowerVSClusterTemplate.Spec is immutable")
7068
}
7169
return nil, nil
7270
}
7371

74-
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
75-
func (r *IBMPowerVSClusterTemplate) ValidateDelete() (admission.Warnings, error) {
76-
ibmpowervsclustertemplatelog.Info("validate delete", "name", r.Name)
77-
72+
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
73+
func (r *IBMPowerVSClusterTemplate) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
7874
return nil, nil
7975
}

api/v1beta2/ibmpowervsclustertemplate_webhook_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func TestIBMPowerVSClusterTemplate_ValidateUpdate(t *testing.T) {
7878
}
7979
for _, test := range tests {
8080
t.Run(test.name, func(_ *testing.T) {
81-
_, err := test.newTemplate.ValidateUpdate(test.oldTemplate)
81+
_, err := test.newTemplate.ValidateUpdate(ctx, test.oldTemplate, test.newTemplate)
8282
if test.wantErr {
8383
g.Expect(err).To(HaveOccurred())
8484
} else {

api/v1beta2/ibmpowervsimage_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,5 @@ type IBMPowerVSImageList struct {
131131
}
132132

133133
func init() {
134-
objectTypes = append(objectTypes, &IBMPowerVSImage{}, &IBMPowerVSImageList{})
134+
SchemeBuilder.Register(&IBMPowerVSImage{}, &IBMPowerVSImageList{})
135135
}

api/v1beta2/ibmpowervsimage_webhook.go

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

1919
import (
20+
"context"
21+
2022
"k8s.io/apimachinery/pkg/runtime"
2123

2224
ctrl "sigs.k8s.io/controller-runtime"
23-
logf "sigs.k8s.io/controller-runtime/pkg/log"
2425
"sigs.k8s.io/controller-runtime/pkg/webhook"
2526
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2627
)
2728

28-
// log is for logging in this package.
29-
var ibmpowervsimagelog = logf.Log.WithName("ibmpowervsimage-resource")
29+
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsimage,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsimages,verbs=create;update,versions=v1beta2,name=mibmpowervsimage.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
30+
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsimage,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsimages,versions=v1beta2,name=vibmpowervsimage.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
3031

3132
func (r *IBMPowerVSImage) SetupWebhookWithManager(mgr ctrl.Manager) error {
3233
return ctrl.NewWebhookManagedBy(mgr).
33-
For(r).
34+
For(&IBMPowerVSImage{}).
35+
WithValidator(r).
36+
WithDefaulter(r).
3437
Complete()
3538
}
3639

37-
//+kubebuilder:webhook:path=/mutate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsimage,mutating=true,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsimages,verbs=create;update,versions=v1beta2,name=mibmpowervsimage.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
38-
39-
var _ webhook.Defaulter = &IBMPowerVSImage{}
40+
var _ webhook.CustomDefaulter = &IBMPowerVSImage{}
41+
var _ webhook.CustomValidator = &IBMPowerVSImage{}
4042

41-
// Default implements webhook.Defaulter so a webhook will be registered for the type.
42-
func (r *IBMPowerVSImage) Default() {
43-
ibmpowervsimagelog.Info("default", "name", r.Name)
43+
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type.
44+
func (r *IBMPowerVSImage) Default(_ context.Context, _ runtime.Object) error {
45+
return nil
4446
}
4547

46-
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
47-
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsimage,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsimages,versions=v1beta2,name=vibmpowervsimage.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
48-
49-
var _ webhook.Validator = &IBMPowerVSImage{}
50-
51-
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
52-
func (r *IBMPowerVSImage) ValidateCreate() (admission.Warnings, error) {
53-
ibmpowervsimagelog.Info("validate create", "name", r.Name)
48+
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type.
49+
func (r *IBMPowerVSImage) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
5450
return nil, nil
5551
}
5652

57-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
58-
func (r *IBMPowerVSImage) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
59-
ibmpowervsimagelog.Info("validate update", "name", r.Name)
53+
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type.
54+
func (r *IBMPowerVSImage) ValidateUpdate(_ context.Context, _, _ runtime.Object) (warnings admission.Warnings, err error) {
6055
return nil, nil
6156
}
6257

63-
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
64-
func (r *IBMPowerVSImage) ValidateDelete() (admission.Warnings, error) {
65-
ibmpowervsimagelog.Info("validate delete", "name", r.Name)
58+
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type.
59+
func (r *IBMPowerVSImage) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
6660
return nil, nil
6761
}

api/v1beta2/ibmpowervsmachine_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,5 +271,5 @@ type IBMPowerVSMachineList struct {
271271
}
272272

273273
func init() {
274-
objectTypes = append(objectTypes, &IBMPowerVSMachine{}, &IBMPowerVSMachineList{})
274+
SchemeBuilder.Register(&IBMPowerVSMachine{}, &IBMPowerVSMachineList{})
275275
}

0 commit comments

Comments
 (0)