Skip to content

Commit 08aca1c

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 08aca1c

File tree

4 files changed

+275
-18
lines changed

4 files changed

+275
-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, object does not implement admission.Defaulter or WithDefaulter wasn't called",
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, object does not implement admission.Validator or WithValidator wasn't called",
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("For() must be called with a valid object")
188+
}
189+
148190
func (blder *WebhookBuilder) isAlreadyHandled(path string) bool {
149191
if blder.mgr.GetWebhookServer().WebhookMux == nil {
150192
return false
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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{object: obj, defaulter: defaulter},
38+
}
39+
}
40+
41+
type defaulterForType struct {
42+
defaulter WithDefaulter
43+
object 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.defaulter == nil {
57+
panic("defaulter should never be nil")
58+
}
59+
if h.object == nil {
60+
panic("object should never be nil")
61+
}
62+
63+
// Get the object in the request
64+
obj := h.object.DeepCopyObject()
65+
if err := h.decoder.Decode(req, obj); err != nil {
66+
return Errored(http.StatusBadRequest, err)
67+
}
68+
69+
// Default the object
70+
if err := h.defaulter.Default(ctx, obj); err != nil {
71+
var apiStatus apierrors.APIStatus
72+
if goerrors.As(err, &apiStatus) {
73+
return validationResponseFromStatus(false, apiStatus.Status())
74+
}
75+
return Denied(err.Error())
76+
}
77+
marshalled, err := json.Marshal(obj)
78+
if err != nil {
79+
return Errored(http.StatusInternalServerError, err)
80+
}
81+
82+
// Create the patch
83+
return PatchResponseFromRaw(req.Object.Raw, marshalled)
84+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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{object: obj, validator: validator},
40+
}
41+
}
42+
43+
type validatorForType struct {
44+
validator WithValidator
45+
object 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.validator == nil {
60+
panic("validator should never be nil")
61+
}
62+
if h.object == nil {
63+
panic("object should never be nil")
64+
}
65+
66+
// Get the object in the request
67+
obj := h.object.DeepCopyObject()
68+
if req.Operation == v1.Create {
69+
err := h.decoder.Decode(req, obj)
70+
if err != nil {
71+
return Errored(http.StatusBadRequest, err)
72+
}
73+
74+
err = h.validator.ValidateCreate(ctx, obj)
75+
if err != nil {
76+
var apiStatus apierrors.APIStatus
77+
if goerrors.As(err, &apiStatus) {
78+
return validationResponseFromStatus(false, apiStatus.Status())
79+
}
80+
return Denied(err.Error())
81+
}
82+
}
83+
84+
if req.Operation == v1.Update {
85+
oldObj := obj.DeepCopyObject()
86+
87+
err := h.decoder.DecodeRaw(req.Object, obj)
88+
if err != nil {
89+
return Errored(http.StatusBadRequest, err)
90+
}
91+
err = h.decoder.DecodeRaw(req.OldObject, oldObj)
92+
if err != nil {
93+
return Errored(http.StatusBadRequest, err)
94+
}
95+
96+
err = h.validator.ValidateUpdate(ctx, oldObj, obj)
97+
if err != nil {
98+
var apiStatus apierrors.APIStatus
99+
if goerrors.As(err, &apiStatus) {
100+
return validationResponseFromStatus(false, apiStatus.Status())
101+
}
102+
return Denied(err.Error())
103+
}
104+
}
105+
106+
if req.Operation == v1.Delete {
107+
// In reference to PR: https://github.com/kubernetes/kubernetes/pull/76346
108+
// OldObject contains the object being deleted
109+
err := h.decoder.DecodeRaw(req.OldObject, obj)
110+
if err != nil {
111+
return Errored(http.StatusBadRequest, err)
112+
}
113+
114+
err = h.validator.ValidateDelete(ctx, obj)
115+
if err != nil {
116+
var apiStatus apierrors.APIStatus
117+
if goerrors.As(err, &apiStatus) {
118+
return validationResponseFromStatus(false, apiStatus.Status())
119+
}
120+
return Denied(err.Error())
121+
}
122+
}
123+
124+
return Allowed("")
125+
}

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)