Skip to content

Commit 01bf7cf

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

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

api/v1beta2/ibmpowervsmachine_webhook.go

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,55 +17,62 @@ limitations under the License.
1717
package v1beta2
1818

1919
import (
20+
"context"
21+
"fmt"
2022
apierrors "k8s.io/apimachinery/pkg/api/errors"
2123
"k8s.io/apimachinery/pkg/runtime"
2224
"k8s.io/apimachinery/pkg/runtime/schema"
2325
"k8s.io/apimachinery/pkg/util/validation/field"
2426

2527
ctrl "sigs.k8s.io/controller-runtime"
26-
logf "sigs.k8s.io/controller-runtime/pkg/log"
2728
"sigs.k8s.io/controller-runtime/pkg/webhook"
2829
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2930
)
3031

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

3435
func (r *IBMPowerVSMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
3536
return ctrl.NewWebhookManagedBy(mgr).
36-
For(r).
37+
For(&IBMPowerVSMachine{}).
38+
WithValidator(r).
39+
WithDefaulter(r).
3740
Complete()
3841
}
3942

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

4446
// Default implements webhook.Defaulter so a webhook will be registered for the type.
45-
func (r *IBMPowerVSMachine) Default() {
46-
ibmpowervsmachinelog.Info("default", "name", r.Name)
47-
defaultIBMPowerVSMachineSpec(&r.Spec)
47+
func (r *IBMPowerVSMachine) Default(_ context.Context, obj runtime.Object) error {
48+
objValue, ok := obj.(*IBMPowerVSMachine)
49+
if !ok {
50+
return apierrors.NewBadRequest(fmt.Sprintf("expected a IBMPowerVSMachine but got a %T", obj))
51+
}
52+
defaultIBMPowerVSMachineSpec(&objValue.Spec)
53+
return nil
4854
}
4955

50-
//+kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1beta2-ibmpowervsmachine,mutating=false,failurePolicy=fail,groups=infrastructure.cluster.x-k8s.io,resources=ibmpowervsmachines,versions=v1beta2,name=vibmpowervsmachine.kb.io,sideEffects=None,admissionReviewVersions=v1;v1beta1
51-
52-
var _ webhook.Validator = &IBMPowerVSMachine{}
53-
5456
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
55-
func (r *IBMPowerVSMachine) ValidateCreate() (admission.Warnings, error) {
56-
ibmpowervsmachinelog.Info("validate create", "name", r.Name)
57-
return r.validateIBMPowerVSMachine()
57+
func (r *IBMPowerVSMachine) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
58+
objValue, ok := obj.(*IBMPowerVSMachine)
59+
if !ok {
60+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a IBMPowerVSMachine but got a %T", obj))
61+
}
62+
return objValue.validateIBMPowerVSMachine()
5863
}
5964

6065
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type.
61-
func (r *IBMPowerVSMachine) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
62-
ibmpowervsmachinelog.Info("validate update", "name", r.Name)
63-
return r.validateIBMPowerVSMachine()
66+
func (r *IBMPowerVSMachine) ValidateUpdate(_ context.Context, _, newObj runtime.Object) (warnings admission.Warnings, err error) {
67+
newObjValue, ok := newObj.(*IBMPowerVSMachine)
68+
if !ok {
69+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a IBMPowerVSMachine but got a %T", newObj))
70+
}
71+
return newObjValue.validateIBMPowerVSMachine()
6472
}
6573

6674
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type.
67-
func (r *IBMPowerVSMachine) ValidateDelete() (admission.Warnings, error) {
68-
ibmpowervsmachinelog.Info("validate delete", "name", r.Name)
75+
func (r *IBMPowerVSMachine) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
6976
return nil, nil
7077
}
7178

api/v1beta2/ibmpowervsmachine_webhook_test.go

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

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

2223
. "github.com/onsi/gomega"
2324
corev1 "k8s.io/api/core/v1"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
"k8s.io/apimachinery/pkg/util/intstr"
2627
"k8s.io/utils/ptr"
27-
"sigs.k8s.io/cluster-api/util/defaulting"
2828
)
2929

3030
func TestIBMPowerVSMachine_default(t *testing.T) {
@@ -42,8 +42,7 @@ func TestIBMPowerVSMachine_default(t *testing.T) {
4242
},
4343
},
4444
}
45-
t.Run("Defaults for IBMPowerVSMachine", defaulting.DefaultValidateTest(powervsMachine))
46-
powervsMachine.Default()
45+
g.Expect(powervsMachine.Default(context.Background(), powervsMachine)).ToNot(HaveOccurred())
4746
g.Expect(powervsMachine.Spec.SystemType).To(BeEquivalentTo("s922"))
4847
g.Expect(powervsMachine.Spec.ProcessorType).To(BeEquivalentTo(PowerVSProcessorTypeShared))
4948
}

0 commit comments

Comments
 (0)