Skip to content

Commit 4f2cc96

Browse files
Fix usage of deprecated webhook.Validator interface
Replace usage of deprecated `webhook.Validator` interface with `admission.CustomValidator`. Signed-off-by: Saeid Askari <[email protected]>
1 parent 1c017aa commit 4f2cc96

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

api/v1/policy_webhook.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package v1
55

66
import (
7+
"context"
78
"encoding/json"
89
"errors"
910
"fmt"
@@ -29,46 +30,60 @@ var (
2930
func (r *Policy) SetupWebhookWithManager(mgr ctrl.Manager) error {
3031
return ctrl.NewWebhookManagedBy(mgr).
3132
For(r).
33+
WithValidator(&PolicyCustomValidator{}).
3234
Complete()
3335
}
3436

35-
//+kubebuilder:webhook:path=/validate-policy-open-cluster-management-io-v1-policy,mutating=false,failurePolicy=Ignore,sideEffects=None,groups=policy.open-cluster-management.io,resources=policies,verbs=create,versions=v1,name=policy.open-cluster-management.io.webhook,admissionReviewVersions=v1
37+
type PolicyCustomValidator struct{}
3638

37-
var _ webhook.Validator = &Policy{}
39+
// +kubebuilder:webhook:path=/validate-policy-open-cluster-management-io-v1-policy,mutating=false,failurePolicy=Ignore,sideEffects=None,groups=policy.open-cluster-management.io,resources=policies,verbs=create,versions=v1,name=policy.open-cluster-management.io.webhook,admissionReviewVersions=v1
40+
var _ webhook.CustomValidator = &PolicyCustomValidator{}
3841

39-
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
40-
func (r *Policy) ValidateCreate() (admission.Warnings, error) {
41-
log := policylog.WithValues("policyName", r.Name, "policyNamespace", r.Namespace)
42+
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
43+
func (r *PolicyCustomValidator) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) {
44+
policy, ok := obj.(*Policy)
45+
log := policylog.WithValues("policyName", policy.Name, "policyNamespace", policy.Namespace)
4246
log.V(1).Info("Validate policy creation request")
4347

44-
err := r.validateName()
48+
if !ok {
49+
return nil, fmt.Errorf("expected a Policy object but got %T", obj)
50+
}
51+
52+
err := policy.validateName()
4553
if err != nil {
4654
return nil, err
4755
}
4856

49-
err = r.validateRemediationAction()
57+
err = policy.validateRemediationAction()
5058
if err != nil {
5159
return nil, err
5260
}
5361

5462
return nil, nil
5563
}
5664

57-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
58-
func (r *Policy) ValidateUpdate(_ runtime.Object) (admission.Warnings, error) {
59-
log := policylog.WithValues("policyName", r.Name, "policyNamespace", r.Namespace)
65+
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
66+
func (r *PolicyCustomValidator) ValidateUpdate(
67+
_ context.Context, _, newObj runtime.Object,
68+
) (admission.Warnings, error) {
69+
policy, ok := newObj.(*Policy)
70+
log := policylog.WithValues("policyName", policy.Name, "policyNamespace", policy.Namespace)
6071
log.V(1).Info("Validate policy update request")
6172

62-
err := r.validateRemediationAction()
73+
if !ok {
74+
return nil, fmt.Errorf("expected a Policy object but got %T", newObj)
75+
}
76+
77+
err := policy.validateRemediationAction()
6378
if err != nil {
6479
return nil, err
6580
}
6681

6782
return nil, nil
6883
}
6984

70-
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
71-
func (r *Policy) ValidateDelete() (admission.Warnings, error) {
85+
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
86+
func (r *PolicyCustomValidator) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
7287
return nil, nil
7388
}
7489

0 commit comments

Comments
 (0)