Skip to content

Commit cd7d01a

Browse files
committed
✨ Allow webhooks to register custom validators/defaulter types
This changeset allows our webhook builder to take in a handler any other struct other than a runtime.Object. Today having an object as the primary source of truth for both Defaulting and Validators makes API types carry a lot of information and business logic alongside their definitions. Moreover, lots of folks in the past have asked for ways to have an external type to handle these operations and use a controller runtime client for validations. This change brings a new way to register webhooks, which admission.For handler any type (struct) can be a defaulting or validating handler for a runtime Object. Signed-off-by: Vince Prignano <[email protected]>
1 parent 76b74e8 commit cd7d01a

File tree

4 files changed

+269
-18
lines changed

4 files changed

+269
-18
lines changed

pkg/builder/webhook.go

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package builder
1818

1919
import (
20+
"errors"
2021
"net/http"
2122
"net/url"
2223
"strings"
@@ -32,10 +33,12 @@ import (
3233

3334
// WebhookBuilder builds a Webhook.
3435
type WebhookBuilder struct {
35-
apiType runtime.Object
36-
gvk schema.GroupVersionKind
37-
mgr manager.Manager
38-
config *rest.Config
36+
apiType runtime.Object
37+
withDefaulter admission.WithDefaulter
38+
withValidator admission.WithValidator
39+
gvk schema.GroupVersionKind
40+
mgr manager.Manager
41+
config *rest.Config
3942
}
4043

4144
// WebhookManagedBy allows inform its manager.Manager.
@@ -53,6 +56,18 @@ func (blder *WebhookBuilder) For(apiType runtime.Object) *WebhookBuilder {
5356
return blder
5457
}
5558

59+
// WithDefaulter takes a admission.WithDefaulter interface, a MutatingWebhook will be wired for this type.
60+
func (blder *WebhookBuilder) WithDefaulter(defaulter admission.WithDefaulter) *WebhookBuilder {
61+
blder.withDefaulter = defaulter
62+
return blder
63+
}
64+
65+
// WithValidator takes a admission.WithValidator interface, a ValidatingWebhook will be wired for this type.
66+
func (blder *WebhookBuilder) WithValidator(validator admission.WithValidator) *WebhookBuilder {
67+
blder.withValidator = validator
68+
return blder
69+
}
70+
5671
// Complete builds the webhook.
5772
func (blder *WebhookBuilder) Complete() error {
5873
// Set the Config
@@ -69,9 +84,13 @@ func (blder *WebhookBuilder) loadRestConfig() {
6984
}
7085

7186
func (blder *WebhookBuilder) registerWebhooks() error {
87+
typ, err := blder.getType()
88+
if err != nil {
89+
return err
90+
}
91+
7292
// Create webhook(s) for each type
73-
var err error
74-
blder.gvk, err = apiutil.GVKForObject(blder.apiType, blder.mgr.GetScheme())
93+
blder.gvk, err = apiutil.GVKForObject(typ, blder.mgr.GetScheme())
7594
if err != nil {
7695
return err
7796
}
@@ -88,12 +107,7 @@ func (blder *WebhookBuilder) registerWebhooks() error {
88107

89108
// registerDefaultingWebhook registers a defaulting webhook if th.
90109
func (blder *WebhookBuilder) registerDefaultingWebhook() {
91-
defaulter, isDefaulter := blder.apiType.(admission.Defaulter)
92-
if !isDefaulter {
93-
log.Info("skip registering a mutating webhook, admission.Defaulter interface is not implemented", "GVK", blder.gvk)
94-
return
95-
}
96-
mwh := admission.DefaultingWebhookFor(defaulter)
110+
mwh := blder.getDefaultingWebhook()
97111
if mwh != nil {
98112
path := generateMutatePath(blder.gvk)
99113

@@ -108,13 +122,21 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() {
108122
}
109123
}
110124

111-
func (blder *WebhookBuilder) registerValidatingWebhook() {
112-
validator, isValidator := blder.apiType.(admission.Validator)
113-
if !isValidator {
114-
log.Info("skip registering a validating webhook, admission.Validator interface is not implemented", "GVK", blder.gvk)
115-
return
125+
func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook {
126+
if defaulter := blder.withDefaulter; defaulter != nil {
127+
return admission.WithCustomDefaulter(blder.apiType, defaulter)
128+
}
129+
if defaulter, ok := blder.apiType.(admission.Defaulter); ok {
130+
return admission.DefaultingWebhookFor(defaulter)
116131
}
117-
vwh := admission.ValidatingWebhookFor(validator)
132+
log.Info(
133+
"skip registering a mutating webhook, admission.Defaulter or admission.DefaulterFor interface is not implemented",
134+
"GVK", blder.gvk)
135+
return nil
136+
}
137+
138+
func (blder *WebhookBuilder) registerValidatingWebhook() {
139+
vwh := blder.getValidatingWebhook()
118140
if vwh != nil {
119141
path := generateValidatePath(blder.gvk)
120142

@@ -129,6 +151,19 @@ func (blder *WebhookBuilder) registerValidatingWebhook() {
129151
}
130152
}
131153

154+
func (blder *WebhookBuilder) getValidatingWebhook() *admission.Webhook {
155+
if validator := blder.withValidator; validator != nil {
156+
return admission.WithCustomValidator(blder.apiType, validator)
157+
}
158+
if validator, ok := blder.apiType.(admission.Validator); ok {
159+
return admission.ValidatingWebhookFor(validator)
160+
}
161+
log.Info(
162+
"skip registering a validating webhook, admission.Validator or admission.ValidatorFor interface is not implemented",
163+
"GVK", blder.gvk)
164+
return nil
165+
}
166+
132167
func (blder *WebhookBuilder) registerConversionWebhook() error {
133168
ok, err := conversion.IsConvertible(blder.mgr.GetScheme(), blder.apiType)
134169
if err != nil {
@@ -145,6 +180,13 @@ func (blder *WebhookBuilder) registerConversionWebhook() error {
145180
return nil
146181
}
147182

183+
func (blder *WebhookBuilder) getType() (runtime.Object, error) {
184+
if blder.apiType != nil {
185+
return blder.apiType, nil
186+
}
187+
return nil, errors.New("one of For() or HandlerFor() should be called")
188+
}
189+
148190
func (blder *WebhookBuilder) isAlreadyHandled(path string) bool {
149191
if blder.mgr.GetWebhookServer().WebhookMux == nil {
150192
return false
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
package admission
18+
19+
import (
20+
"context"
21+
"encoding/json"
22+
goerrors "errors"
23+
"net/http"
24+
25+
apierrors "k8s.io/apimachinery/pkg/api/errors"
26+
"k8s.io/apimachinery/pkg/runtime"
27+
)
28+
29+
// WithDefaulter defines functions for setting defaults on resources.
30+
type WithDefaulter interface {
31+
Default(ctx context.Context, obj runtime.Object) error
32+
}
33+
34+
// WithCustomDefaulter creates a new Webhook for a WithDefaulter interface.
35+
func WithCustomDefaulter(obj runtime.Object, defaulter WithDefaulter) *Webhook {
36+
return &Webhook{
37+
Handler: &defaulterForType{handler: defaulter},
38+
}
39+
}
40+
41+
type defaulterForType struct {
42+
handler WithDefaulter
43+
obj runtime.Object
44+
decoder *Decoder
45+
}
46+
47+
var _ DecoderInjector = &defaulterForType{}
48+
49+
func (h *defaulterForType) InjectDecoder(d *Decoder) error {
50+
h.decoder = d
51+
return nil
52+
}
53+
54+
// Handle handles admission requests.
55+
func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
56+
if h.handler == nil {
57+
panic("defaulter should never be nil")
58+
}
59+
60+
// Get the object in the request
61+
obj := h.obj.DeepCopyObject()
62+
if err := h.decoder.Decode(req, obj); err != nil {
63+
return Errored(http.StatusBadRequest, err)
64+
}
65+
66+
// Default the object
67+
if err := h.handler.Default(ctx, obj); err != nil {
68+
var apiStatus apierrors.APIStatus
69+
if goerrors.As(err, &apiStatus) {
70+
return validationResponseFromStatus(false, apiStatus.Status())
71+
}
72+
return Denied(err.Error())
73+
}
74+
marshalled, err := json.Marshal(obj)
75+
if err != nil {
76+
return Errored(http.StatusInternalServerError, err)
77+
}
78+
79+
// Create the patch
80+
return PatchResponseFromRaw(req.Object.Raw, marshalled)
81+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
package admission
18+
19+
import (
20+
"context"
21+
goerrors "errors"
22+
"net/http"
23+
24+
v1 "k8s.io/api/admission/v1"
25+
apierrors "k8s.io/apimachinery/pkg/api/errors"
26+
"k8s.io/apimachinery/pkg/runtime"
27+
)
28+
29+
// WithValidator defines functions for validating an operation.
30+
type WithValidator interface {
31+
ValidateCreate(ctx context.Context, obj runtime.Object) error
32+
ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) error
33+
ValidateDelete(ctx context.Context, obj runtime.Object) error
34+
}
35+
36+
// WithCustomValidator creates a new Webhook for validating the provided type.
37+
func WithCustomValidator(obj runtime.Object, validator WithValidator) *Webhook {
38+
return &Webhook{
39+
Handler: &validatorForType{obj: obj, handler: validator},
40+
}
41+
}
42+
43+
type validatorForType struct {
44+
handler WithValidator
45+
obj runtime.Object
46+
decoder *Decoder
47+
}
48+
49+
var _ DecoderInjector = &validatorForType{}
50+
51+
// InjectDecoder injects the decoder into a validatingHandler.
52+
func (h *validatorForType) InjectDecoder(d *Decoder) error {
53+
h.decoder = d
54+
return nil
55+
}
56+
57+
// Handle handles admission requests.
58+
func (h *validatorForType) Handle(ctx context.Context, req Request) Response {
59+
if h.handler == nil {
60+
panic("handler should never be nil")
61+
}
62+
63+
// Get the object in the request
64+
obj := h.obj.DeepCopyObject()
65+
if req.Operation == v1.Create {
66+
err := h.decoder.Decode(req, obj)
67+
if err != nil {
68+
return Errored(http.StatusBadRequest, err)
69+
}
70+
71+
err = h.handler.ValidateCreate(ctx, obj)
72+
if err != nil {
73+
var apiStatus apierrors.APIStatus
74+
if goerrors.As(err, &apiStatus) {
75+
return validationResponseFromStatus(false, apiStatus.Status())
76+
}
77+
return Denied(err.Error())
78+
}
79+
}
80+
81+
if req.Operation == v1.Update {
82+
oldObj := obj.DeepCopyObject()
83+
84+
err := h.decoder.DecodeRaw(req.Object, obj)
85+
if err != nil {
86+
return Errored(http.StatusBadRequest, err)
87+
}
88+
err = h.decoder.DecodeRaw(req.OldObject, oldObj)
89+
if err != nil {
90+
return Errored(http.StatusBadRequest, err)
91+
}
92+
93+
err = h.handler.ValidateUpdate(ctx, oldObj, obj)
94+
if err != nil {
95+
var apiStatus apierrors.APIStatus
96+
if goerrors.As(err, &apiStatus) {
97+
return validationResponseFromStatus(false, apiStatus.Status())
98+
}
99+
return Denied(err.Error())
100+
}
101+
}
102+
103+
if req.Operation == v1.Delete {
104+
// In reference to PR: https://github.com/kubernetes/kubernetes/pull/76346
105+
// OldObject contains the object being deleted
106+
err := h.decoder.DecodeRaw(req.OldObject, obj)
107+
if err != nil {
108+
return Errored(http.StatusBadRequest, err)
109+
}
110+
111+
err = h.handler.ValidateDelete(ctx, obj)
112+
if err != nil {
113+
var apiStatus apierrors.APIStatus
114+
if goerrors.As(err, &apiStatus) {
115+
return validationResponseFromStatus(false, apiStatus.Status())
116+
}
117+
return Denied(err.Error())
118+
}
119+
}
120+
121+
return Allowed("")
122+
}

pkg/webhook/alias.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ type Defaulter = admission.Defaulter
2929
// Validator defines functions for validating an operation.
3030
type Validator = admission.Validator
3131

32+
// WithDefaulter defines functions for setting defaults on resources.
33+
type WithDefaulter = admission.WithDefaulter
34+
35+
// WithValidator defines functions for validating an operation.
36+
type WithValidator = admission.WithValidator
37+
3238
// AdmissionRequest defines the input for an admission handler.
3339
// It contains information to identify the object in
3440
// question (group, version, kind, resource, subresource,

0 commit comments

Comments
 (0)