forked from kubernetes-sigs/controller-runtime
/
webhook.go
259 lines (230 loc) · 8.03 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package admission
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"regexp"
"strings"
"sync"
"github.com/appscode/jsonpatch"
admissionv1beta1 "k8s.io/api/admission/v1beta1"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
atypes "sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
"sigs.k8s.io/controller-runtime/pkg/webhook/types"
)
// Handler can handle an AdmissionRequest.
type Handler interface {
Handle(context.Context, atypes.Request) atypes.Response
}
// HandlerFunc implements Handler interface using a single function.
type HandlerFunc func(context.Context, atypes.Request) atypes.Response
var _ Handler = HandlerFunc(nil)
// Handle process the AdmissionRequest by invoking the underlying function.
func (f HandlerFunc) Handle(ctx context.Context, req atypes.Request) atypes.Response {
return f(ctx, req)
}
// Webhook represents each individual webhook.
type Webhook struct {
// Name is the name of the webhook
Name string
// Type is the webhook type, i.e. mutating, validating
Type types.WebhookType
// Path is the path this webhook will serve.
Path string
// Rules maps to the Rules field in admissionregistrationv1beta1.Webhook
Rules []admissionregistrationv1beta1.RuleWithOperations
// FailurePolicy maps to the FailurePolicy field in admissionregistrationv1beta1.Webhook
// This optional. If not set, will be defaulted to Ignore (fail-open) by the server.
// More details: https://github.com/kubernetes/api/blob/f5c295feaba2cbc946f0bbb8b535fc5f6a0345ee/admissionregistration/v1beta1/types.go#L144-L147
FailurePolicy *admissionregistrationv1beta1.FailurePolicyType
// NamespaceSelector maps to the NamespaceSelector field in admissionregistrationv1beta1.Webhook
// This optional.
NamespaceSelector *metav1.LabelSelector
// Handlers contains a list of handlers. Each handler may only contains the business logic for its own feature.
// For example, feature foo and bar can be in the same webhook if all the other configurations are the same.
// The handler will be invoked sequentially as the order in the list.
// Note: if you are using mutating webhook with multiple handlers, it's your responsibility to
// ensure the handlers are not generating conflicting JSON patches.
Handlers []Handler
once sync.Once
}
func (w *Webhook) setDefaults() {
if len(w.Path) == 0 {
if len(w.Rules) == 0 || len(w.Rules[0].Resources) == 0 {
// can't do defaulting, skip it.
return
}
if w.Type == types.WebhookTypeMutating {
w.Path = "/mutate-" + w.Rules[0].Resources[0]
} else if w.Type == types.WebhookTypeValidating {
w.Path = "/validate-" + w.Rules[0].Resources[0]
}
}
if len(w.Name) == 0 {
reg := regexp.MustCompile("[^a-zA-Z0-9]+")
processedPath := strings.ToLower(reg.ReplaceAllString(w.Path, ""))
w.Name = processedPath + ".example.com"
}
}
// Add adds additional handler(s) in the webhook
func (w *Webhook) Add(handlers ...Handler) {
w.Handlers = append(w.Handlers, handlers...)
}
// Webhook implements Handler interface.
var _ Handler = &Webhook{}
// Handle processes AdmissionRequest.
// If the webhook is mutating type, it delegates the AdmissionRequest to each handler and merge the patches.
// If the webhook is validating type, it delegates the AdmissionRequest to each handler and
// deny the request if anyone denies.
func (w *Webhook) Handle(ctx context.Context, req atypes.Request) atypes.Response {
if req.AdmissionRequest == nil {
return ErrorResponse(http.StatusBadRequest, errors.New("got an empty AdmissionRequest"))
}
var resp atypes.Response
switch w.Type {
case types.WebhookTypeMutating:
resp = w.handleMutating(ctx, req)
case types.WebhookTypeValidating:
resp = w.handleValidating(ctx, req)
default:
return ErrorResponse(http.StatusInternalServerError, errors.New("you must specify your webhook type"))
}
resp.Response.UID = req.AdmissionRequest.UID
return resp
}
func (w *Webhook) handleMutating(ctx context.Context, req atypes.Request) atypes.Response {
patches := []jsonpatch.JsonPatchOperation{}
for _, handler := range w.Handlers {
resp := handler.Handle(ctx, req)
if !resp.Response.Allowed {
setStatusOKInAdmissionResponse(resp.Response)
return resp
}
if resp.Response.PatchType != nil && *resp.Response.PatchType != admissionv1beta1.PatchTypeJSONPatch {
return ErrorResponse(http.StatusInternalServerError,
fmt.Errorf("unexpected patch type returned by the handler: %v, only allow: %v",
resp.Response.PatchType, admissionv1beta1.PatchTypeJSONPatch))
}
patches = append(patches, resp.Patches...)
}
var err error
marshaledPatch, err := json.Marshal(patches)
if err != nil {
return ErrorResponse(http.StatusBadRequest, fmt.Errorf("error when marshaling the patch: %v", err))
}
return atypes.Response{
Response: &admissionv1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
},
Patch: marshaledPatch,
PatchType: func() *admissionv1beta1.PatchType { pt := admissionv1beta1.PatchTypeJSONPatch; return &pt }(),
},
}
}
func (w *Webhook) handleValidating(ctx context.Context, req atypes.Request) atypes.Response {
for _, handler := range w.Handlers {
resp := handler.Handle(ctx, req)
if !resp.Response.Allowed {
setStatusOKInAdmissionResponse(resp.Response)
return resp
}
}
return atypes.Response{
Response: &admissionv1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
},
},
}
}
func setStatusOKInAdmissionResponse(resp *admissionv1beta1.AdmissionResponse) {
if resp == nil {
return
}
if resp.Result == nil {
resp.Result = &metav1.Status{}
}
if resp.Result.Code == 0 {
resp.Result.Code = http.StatusOK
}
}
// GetName returns the name of the webhook.
func (w *Webhook) GetName() string {
w.once.Do(w.setDefaults)
return w.Name
}
// GetPath returns the path that the webhook registered.
func (w *Webhook) GetPath() string {
w.once.Do(w.setDefaults)
return w.Path
}
// GetType returns the type of the webhook.
func (w *Webhook) GetType() types.WebhookType {
w.once.Do(w.setDefaults)
return w.Type
}
// Handler returns a http.Handler for the webhook
func (w *Webhook) Handler() http.Handler {
w.once.Do(w.setDefaults)
return w
}
// Validate validates if the webhook is valid.
func (w *Webhook) Validate() error {
w.once.Do(w.setDefaults)
if len(w.Rules) == 0 {
return errors.New("field Rules should not be empty")
}
if len(w.Name) == 0 {
return errors.New("field Name should not be empty")
}
if w.Type != types.WebhookTypeMutating && w.Type != types.WebhookTypeValidating {
return fmt.Errorf("unsupported Type: %v, only WebhookTypeMutating and WebhookTypeValidating are supported", w.Type)
}
if len(w.Path) == 0 {
return errors.New("field Path should not be empty")
}
if len(w.Handlers) == 0 {
return errors.New("field Handler should not be empty")
}
return nil
}
var _ inject.Client = &Webhook{}
// InjectClient injects the client into the handlers
func (w *Webhook) InjectClient(c client.Client) error {
for _, handler := range w.Handlers {
if _, err := inject.ClientInto(c, handler); err != nil {
return err
}
}
return nil
}
var _ inject.Decoder = &Webhook{}
// InjectDecoder injects the decoder into the handlers
func (w *Webhook) InjectDecoder(d atypes.Decoder) error {
for _, handler := range w.Handlers {
if _, err := inject.DecoderInto(d, handler); err != nil {
return err
}
}
return nil
}