-
Notifications
You must be signed in to change notification settings - Fork 458
/
admission.go
281 lines (241 loc) · 10.9 KB
/
admission.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) 2021 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 extensionresources
import (
"context"
"fmt"
"net/http"
"time"
druidv1alpha1 "github.com/gardener/etcd-druid/api/v1alpha1"
druidvalidation "github.com/gardener/etcd-druid/api/validation"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
"github.com/gardener/gardener/pkg/apis/extensions/validation"
"github.com/go-logr/logr"
admissionv1 "k8s.io/api/admission/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
)
const (
// WebhookPath is the HTTP handler path for this admission webhook handler.
WebhookPath = "/webhooks/validate-extension-resources"
// HandlerName is the name of this admission webhook handler.
HandlerName = "extension_resources"
)
// New creates a new webhook handler validating CREATE and UPDATE requests for extension resources.
func New(logger logr.Logger, allowInvalidExtensionResources bool) *handler {
artifacts := map[metav1.GroupVersionResource]*artifact{
gvr("backupbuckets"): {
newObject: func() client.Object { return new(extensionsv1alpha1.BackupBucket) },
validateCreateResource: func(n, _ client.Object) field.ErrorList {
return validation.ValidateBackupBucket(n.(*extensionsv1alpha1.BackupBucket))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateBackupBucketUpdate(n.(*extensionsv1alpha1.BackupBucket), o.(*extensionsv1alpha1.BackupBucket))
},
},
gvr("backupentries"): {
newObject: func() client.Object { return new(extensionsv1alpha1.BackupEntry) },
validateCreateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateBackupEntry(n.(*extensionsv1alpha1.BackupEntry))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateBackupEntryUpdate(n.(*extensionsv1alpha1.BackupEntry), o.(*extensionsv1alpha1.BackupEntry))
},
},
gvr("bastions"): {
newObject: func() client.Object { return new(extensionsv1alpha1.Bastion) },
validateCreateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateBastion(n.(*extensionsv1alpha1.Bastion))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateBastionUpdate(n.(*extensionsv1alpha1.Bastion), o.(*extensionsv1alpha1.Bastion))
},
},
gvr("containerruntimes"): {
newObject: func() client.Object { return new(extensionsv1alpha1.ContainerRuntime) },
validateCreateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateContainerRuntime(n.(*extensionsv1alpha1.ContainerRuntime))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateContainerRuntimeUpdate(n.(*extensionsv1alpha1.ContainerRuntime), o.(*extensionsv1alpha1.ContainerRuntime))
},
},
gvr("controlplanes"): {
newObject: func() client.Object { return new(extensionsv1alpha1.ControlPlane) },
validateCreateResource: func(n, _ client.Object) field.ErrorList {
return validation.ValidateControlPlane(n.(*extensionsv1alpha1.ControlPlane))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateControlPlaneUpdate(n.(*extensionsv1alpha1.ControlPlane), o.(*extensionsv1alpha1.ControlPlane))
},
},
gvr("dnsrecords"): {
newObject: func() client.Object { return new(extensionsv1alpha1.DNSRecord) },
validateCreateResource: func(n, _ client.Object) field.ErrorList {
return validation.ValidateDNSRecord(n.(*extensionsv1alpha1.DNSRecord))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateDNSRecordUpdate(n.(*extensionsv1alpha1.DNSRecord), o.(*extensionsv1alpha1.DNSRecord))
},
},
gvrDruid("etcds"): {
newObject: func() client.Object { return new(druidv1alpha1.Etcd) },
validateCreateResource: func(n, _ client.Object) field.ErrorList {
return druidvalidation.ValidateEtcd(n.(*druidv1alpha1.Etcd))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return druidvalidation.ValidateEtcdUpdate(n.(*druidv1alpha1.Etcd), o.(*druidv1alpha1.Etcd))
},
},
gvr("extensions"): {
newObject: func() client.Object { return new(extensionsv1alpha1.Extension) },
validateCreateResource: func(n, _ client.Object) field.ErrorList {
return validation.ValidateExtension(n.(*extensionsv1alpha1.Extension))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateExtensionUpdate(n.(*extensionsv1alpha1.Extension), o.(*extensionsv1alpha1.Extension))
},
},
gvr("infrastructures"): {
newObject: func() client.Object { return new(extensionsv1alpha1.Infrastructure) },
validateCreateResource: func(n, _ client.Object) field.ErrorList {
return validation.ValidateInfrastructure(n.(*extensionsv1alpha1.Infrastructure))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateInfrastructureUpdate(n.(*extensionsv1alpha1.Infrastructure), o.(*extensionsv1alpha1.Infrastructure))
},
},
gvr("networks"): {
newObject: func() client.Object { return new(extensionsv1alpha1.Network) },
validateCreateResource: func(n, _ client.Object) field.ErrorList {
return validation.ValidateNetwork(n.(*extensionsv1alpha1.Network))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateNetworkUpdate(n.(*extensionsv1alpha1.Network), o.(*extensionsv1alpha1.Network))
},
},
gvr("operatingsystemconfigs"): {
newObject: func() client.Object { return new(extensionsv1alpha1.OperatingSystemConfig) },
validateCreateResource: func(n, _ client.Object) field.ErrorList {
return validation.ValidateOperatingSystemConfig(n.(*extensionsv1alpha1.OperatingSystemConfig))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateOperatingSystemConfigUpdate(n.(*extensionsv1alpha1.OperatingSystemConfig), o.(*extensionsv1alpha1.OperatingSystemConfig))
},
},
gvr("workers"): {
newObject: func() client.Object { return new(extensionsv1alpha1.Worker) },
validateCreateResource: func(n, _ client.Object) field.ErrorList {
return validation.ValidateWorker(n.(*extensionsv1alpha1.Worker))
},
validateUpdateResource: func(n, o client.Object) field.ErrorList {
return validation.ValidateWorkerUpdate(n.(*extensionsv1alpha1.Worker), o.(*extensionsv1alpha1.Worker))
},
},
}
h := handler{
logger: logger,
artifacts: artifacts,
allowInvalidExtensionResources: allowInvalidExtensionResources,
}
return &h
}
type handler struct {
decoder *admission.Decoder
logger logr.Logger
artifacts map[metav1.GroupVersionResource]*artifact
allowInvalidExtensionResources bool
}
var _ admission.Handler = &handler{}
func (h *handler) InjectDecoder(d *admission.Decoder) error {
h.decoder = d
return nil
}
func (h *handler) Handle(ctx context.Context, request admission.Request) admission.Response {
_, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
artifact, ok := h.artifacts[request.Resource]
if !ok {
return admission.Allowed("validation not found for the given resource")
}
switch request.Operation {
case admissionv1.Create:
return h.handleValidation(request, artifact.newObject, artifact.validateCreateResource)
case admissionv1.Update:
return h.handleValidation(request, artifact.newObject, artifact.validateUpdateResource)
default:
return admission.Allowed("operation is not CREATE or UPDATE")
}
}
type (
newObjectFunc func() client.Object
validateFunc func(new, old client.Object) field.ErrorList
)
// artifact servers as a helper to setup the corresponding function.
type artifact struct {
// newObject is a simple function that creates and returns a new resource.
newObject newObjectFunc
// validateCreateResource is a wrapper function for the different create validation functions.
validateCreateResource validateFunc
// validateUpdateResource is a wrapper function for the different update validation functions.
validateUpdateResource validateFunc
}
func (h handler) handleValidation(request admission.Request, newObject newObjectFunc, validate validateFunc) admission.Response {
obj := newObject()
if err := h.decoder.DecodeRaw(request.Object, obj); err != nil {
h.logger.Error(err, "Could not decode object", "object", request.Object)
return admission.Errored(http.StatusUnprocessableEntity, fmt.Errorf("could not decode object %v: %w", request.Object, err))
}
h.logger.Info("Validating extension resource", "resource", request.Resource, "name", kutil.ObjectName(obj), "operation", request.Operation)
var oldObj client.Object
if len(request.OldObject.Raw) != 0 {
oldObj = newObject()
if err := h.decoder.DecodeRaw(request.OldObject, oldObj); err != nil {
h.logger.Error(err, "Could not decode old object", "oldObject", oldObj)
return admission.Errored(http.StatusBadRequest, fmt.Errorf("could not decode old object %v: %v", oldObj, err))
}
}
errors := validate(obj, oldObj)
if len(errors) != 0 {
err := apierrors.NewInvalid(schema.GroupKind{
Group: request.Kind.Group,
Kind: request.Kind.Kind,
}, kutil.ObjectName(obj), errors)
h.logger.Info("Invalid extension resource detected", "operation", request.Operation, "error", err.Error())
if h.allowInvalidExtensionResources {
return admission.Allowed(err.Error())
}
return admission.Denied(err.Error())
}
return admission.Allowed("validation successful")
}
func gvr(resource string) metav1.GroupVersionResource {
return metav1.GroupVersionResource{
Group: extensionsv1alpha1.SchemeGroupVersion.Group,
Version: extensionsv1alpha1.SchemeGroupVersion.Version,
Resource: resource,
}
}
func gvrDruid(resource string) metav1.GroupVersionResource {
return metav1.GroupVersionResource{
Group: druidv1alpha1.GroupVersion.Group,
Version: druidv1alpha1.GroupVersion.Version,
Resource: resource,
}
}