Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / webhook / admission / doc.go
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 /*
18 Package admission provides implementation for admission webhook and methods to implement admission webhook handlers.
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.ErrorResponse(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.ErrorResponse(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.ErrorResponse(http.StatusBadRequest, err)
74                 }
75
76                 allowed, reason, err := h.validatePodsFn(ctx, pod)
77                 if err != nil {
78                         return admission.ErrorResponse(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         }
94 */
95 package admission
96
97 import (
98         logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
99 )
100
101 var log = logf.KBLog.WithName("admission")