4
4
package v1
5
5
6
6
import (
7
+ "context"
7
8
"encoding/json"
8
9
"errors"
9
10
"fmt"
@@ -29,46 +30,61 @@ var (
29
30
func (r * Policy ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
30
31
return ctrl .NewWebhookManagedBy (mgr ).
31
32
For (r ).
33
+ WithValidator (& PolicyCustomValidator {}).
32
34
Complete ()
33
35
}
34
36
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
+ // +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
38
+ // +kubebuilder:object:generate=false
39
+ type PolicyCustomValidator struct {}
36
40
37
- var _ webhook.Validator = & Policy {}
41
+ var _ webhook.CustomValidator = & PolicyCustomValidator {}
38
42
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 )
43
+ // ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type
44
+ func (r * PolicyCustomValidator ) ValidateCreate (_ context.Context , obj runtime.Object ) (admission.Warnings , error ) {
45
+ policy , ok := obj .(* Policy )
46
+ if ! ok {
47
+ return nil , fmt .Errorf ("expected a Policy object but got %T" , obj )
48
+ }
49
+
50
+ log := policylog .WithValues ("policyName" , policy .Name , "policyNamespace" , policy .Namespace )
42
51
log .V (1 ).Info ("Validate policy creation request" )
43
52
44
- err := r .validateName ()
53
+ err := policy .validateName ()
45
54
if err != nil {
46
55
return nil , err
47
56
}
48
57
49
- err = r .validateRemediationAction ()
58
+ err = policy .validateRemediationAction ()
50
59
if err != nil {
51
60
return nil , err
52
61
}
53
62
54
63
return nil , nil
55
64
}
56
65
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 )
66
+ // ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type
67
+ func (r * PolicyCustomValidator ) ValidateUpdate (
68
+ _ context.Context , _ , newObj runtime.Object ,
69
+ ) (admission.Warnings , error ) {
70
+ policy , ok := newObj .(* Policy )
71
+ if ! ok {
72
+ return nil , fmt .Errorf ("expected a Policy object but got %T" , newObj )
73
+ }
74
+
75
+ log := policylog .WithValues ("policyName" , policy .Name , "policyNamespace" , policy .Namespace )
60
76
log .V (1 ).Info ("Validate policy update request" )
61
77
62
- err := r .validateRemediationAction ()
78
+ err := policy .validateRemediationAction ()
63
79
if err != nil {
64
80
return nil , err
65
81
}
66
82
67
83
return nil , nil
68
84
}
69
85
70
- // ValidateDelete implements webhook.Validator so a webhook will be registered for the type
71
- func (r * Policy ) ValidateDelete () (admission.Warnings , error ) {
86
+ // ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type
87
+ func (r * PolicyCustomValidator ) ValidateDelete (_ context. Context , _ runtime. Object ) (admission.Warnings , error ) {
72
88
return nil , nil
73
89
}
74
90
0 commit comments