@@ -23,54 +23,38 @@ import (
23
23
24
24
corev1 "k8s.io/api/core/v1"
25
25
"sigs.k8s.io/controller-runtime/pkg/client"
26
- "sigs.k8s.io/controller-runtime/pkg/runtime/inject"
27
26
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
28
- "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
29
27
)
30
28
31
29
// podValidator validates Pods
32
30
type podValidator struct {
33
31
client client.Client
34
- decoder types .Decoder
32
+ decoder * admission .Decoder
35
33
}
36
34
37
- // Implement admission.Handler so the controller can handle admission request.
38
- var _ admission.Handler = & podValidator {}
39
-
40
35
// podValidator admits a pod iff a specific annotation exists.
41
- func (v * podValidator ) Handle (ctx context.Context , req types .Request ) types .Response {
36
+ func (v * podValidator ) Handle (ctx context.Context , req admission .Request ) admission .Response {
42
37
pod := & corev1.Pod {}
43
38
44
39
err := v .decoder .Decode (req , pod )
45
40
if err != nil {
46
- return admission .ErrorResponse (http .StatusBadRequest , err )
41
+ return admission .Errored (http .StatusBadRequest , err )
47
42
}
48
43
49
- allowed , reason , err := v .validatePodsFn (ctx , pod )
50
- if err != nil {
51
- return admission .ErrorResponse (http .StatusInternalServerError , err )
52
- }
53
- return admission .ValidationResponse (allowed , reason )
54
- }
55
-
56
- func (v * podValidator ) validatePodsFn (ctx context.Context , pod * corev1.Pod ) (bool , string , error ) {
57
44
key := "example-mutating-admission-webhook"
58
45
anno , found := pod .Annotations [key ]
59
- switch {
60
- case ! found :
61
- return found , fmt .Sprintf ("failed to find annotation with key: %q" , key ), nil
62
- case found && anno == "foo" :
63
- return found , "" , nil
64
- case found && anno != "foo" :
65
- return false ,
66
- fmt .Sprintf ("the value associate with key %q is expected to be %q, but got %q" , key , "foo" , anno ), nil
46
+ if ! found {
47
+ return admission .Denied (fmt .Sprintf ("missing annotation %s" , key ))
48
+ }
49
+ if anno != "foo" {
50
+ return admission .Denied (fmt .Sprintf ("annotation %s did not have value %q" , key , "foo" ))
67
51
}
68
- return false , "" , nil
52
+
53
+ return admission .Allowed ("" )
69
54
}
70
55
71
56
// podValidator implements inject.Client.
72
57
// A client will be automatically injected.
73
- var _ inject.Client = & podValidator {}
74
58
75
59
// InjectClient injects the client.
76
60
func (v * podValidator ) InjectClient (c client.Client ) error {
@@ -80,10 +64,9 @@ func (v *podValidator) InjectClient(c client.Client) error {
80
64
81
65
// podValidator implements inject.Decoder.
82
66
// A decoder will be automatically injected.
83
- var _ inject.Decoder = & podValidator {}
84
67
85
68
// InjectDecoder injects the decoder.
86
- func (v * podValidator ) InjectDecoder (d types .Decoder ) error {
69
+ func (v * podValidator ) InjectDecoder (d * admission .Decoder ) error {
87
70
v .decoder = d
88
71
return nil
89
72
}
0 commit comments