@@ -17,80 +17,7 @@ limitations under the License.
17
17
/*
18
18
Package admission provides implementation for admission webhook and methods to implement admission webhook handlers.
19
19
20
- The following snippet is an example implementation of mutating handler.
21
-
22
- type Mutator struct {
23
- client client.Client
24
- decoder types.Decoder
25
- }
26
-
27
- func (m *Mutator) mutatePodsFn(ctx context.Context, pod *corev1.Pod) error {
28
- // your logic to mutate the passed-in pod.
29
- }
30
-
31
- func (m *Mutator) Handle(ctx context.Context, req types.Request) types.Response {
32
- pod := &corev1.Pod{}
33
- err := m.decoder.Decode(req, pod)
34
- if err != nil {
35
- return admission.Errored(http.StatusBadRequest, err)
36
- }
37
- // Do deepcopy before actually mutate the object.
38
- copy := pod.DeepCopy()
39
- err = m.mutatePodsFn(ctx, copy)
40
- if err != nil {
41
- return admission.Errored(http.StatusInternalServerError, err)
42
- }
43
- return admission.PatchResponse(pod, copy)
44
- }
45
-
46
- // InjectClient is called by the Manager and provides a client.Client to the Mutator instance.
47
- func (m *Mutator) InjectClient(c client.Client) error {
48
- h.client = c
49
- return nil
50
- }
51
-
52
- // InjectDecoder is called by the Manager and provides a types.Decoder to the Mutator instance.
53
- func (m *Mutator) InjectDecoder(d types.Decoder) error {
54
- h.decoder = d
55
- return nil
56
- }
57
-
58
- The following snippet is an example implementation of validating handler.
59
-
60
- type Handler struct {
61
- client client.Client
62
- decoder types.Decoder
63
- }
64
-
65
- func (v *Validator) validatePodsFn(ctx context.Context, pod *corev1.Pod) (bool, string, error) {
66
- // your business logic
67
- }
68
-
69
- func (v *Validator) Handle(ctx context.Context, req types.Request) types.Response {
70
- pod := &corev1.Pod{}
71
- err := h.decoder.Decode(req, pod)
72
- if err != nil {
73
- return admission.Errored(http.StatusBadRequest, err)
74
- }
75
-
76
- allowed, reason, err := h.validatePodsFn(ctx, pod)
77
- if err != nil {
78
- return admission.Errored(http.StatusInternalServerError, err)
79
- }
80
- return admission.ValidationResponse(allowed, reason)
81
- }
82
-
83
- // InjectClient is called by the Manager and provides a client.Client to the Validator instance.
84
- func (v *Validator) InjectClient(c client.Client) error {
85
- h.client = c
86
- return nil
87
- }
88
-
89
- // InjectDecoder is called by the Manager and provides a types.Decoder to the Validator instance.
90
- func (v *Validator) InjectDecoder(d types.Decoder) error {
91
- h.decoder = d
92
- return nil
93
- }
20
+ See examples/mutatingwebhook.go and examples/validatingwebhook.go for examples of admission webhooks.
94
21
*/
95
22
package admission
96
23
0 commit comments