Skip to content

Commit bc58c4b

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 0cce21b commit bc58c4b

File tree

4 files changed

+258
-9
lines changed

4 files changed

+258
-9
lines changed

pkg/builder/webhook.go

Lines changed: 54 additions & 9 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"
@@ -33,6 +34,7 @@ import (
3334
// WebhookBuilder builds a Webhook.
3435
type WebhookBuilder struct {
3536
apiType runtime.Object
37+
forType admission.For
3638
gvk schema.GroupVersionKind
3739
mgr manager.Manager
3840
config *rest.Config
@@ -53,6 +55,15 @@ func (blder *WebhookBuilder) For(apiType runtime.Object) *WebhookBuilder {
5355
return blder
5456
}
5557

58+
// HandlerFor takes a admission.admissionFor interface.
59+
//
60+
// If the given object implements the admission.DefaulterFor interface, a MutatingWebhook will be wired for this type.
61+
// If the given object implements the admission.ValidatorFor interface, a ValidatingWebhook will be wired for this type.
62+
func (blder *WebhookBuilder) HandlerFor(forType admission.For) *WebhookBuilder {
63+
blder.forType = forType
64+
return blder
65+
}
66+
5667
// Complete builds the webhook.
5768
func (blder *WebhookBuilder) Complete() error {
5869
// Set the Config
@@ -69,9 +80,13 @@ func (blder *WebhookBuilder) loadRestConfig() {
6980
}
7081

7182
func (blder *WebhookBuilder) registerWebhooks() error {
83+
typ, err := blder.getType()
84+
if err != nil {
85+
return err
86+
}
87+
7288
// Create webhook(s) for each type
73-
var err error
74-
blder.gvk, err = apiutil.GVKForObject(blder.apiType, blder.mgr.GetScheme())
89+
blder.gvk, err = apiutil.GVKForObject(typ, blder.mgr.GetScheme())
7590
if err != nil {
7691
return err
7792
}
@@ -88,12 +103,7 @@ func (blder *WebhookBuilder) registerWebhooks() error {
88103

89104
// registerDefaultingWebhook registers a defaulting webhook if th.
90105
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)
106+
mwh := blder.getDefaultingWebhook()
97107
if mwh != nil {
98108
path := generateMutatePath(blder.gvk)
99109

@@ -108,10 +118,22 @@ func (blder *WebhookBuilder) registerDefaultingWebhook() {
108118
}
109119
}
110120

121+
func (blder *WebhookBuilder) getDefaultingWebhook() *admission.Webhook {
122+
if defaulter, ok := blder.apiType.(admission.Defaulter); ok {
123+
return admission.DefaultingWebhookFor(defaulter)
124+
}
125+
if defaulter, ok := blder.forType.(admission.DefaulterFor); ok {
126+
return admission.DefaulterForToWebhook(defaulter)
127+
}
128+
log.Info(
129+
"skip registering a mutating webhook, admission.Defaulter or admission.DefaulterFor interface is not implemented",
130+
"GVK", blder.gvk)
131+
return nil
132+
}
133+
111134
func (blder *WebhookBuilder) registerValidatingWebhook() {
112135
validator, isValidator := blder.apiType.(admission.Validator)
113136
if !isValidator {
114-
log.Info("skip registering a validating webhook, admission.Validator interface is not implemented", "GVK", blder.gvk)
115137
return
116138
}
117139
vwh := admission.ValidatingWebhookFor(validator)
@@ -129,6 +151,19 @@ func (blder *WebhookBuilder) registerValidatingWebhook() {
129151
}
130152
}
131153

154+
func (blder *WebhookBuilder) getValidatingWebhook() *admission.Webhook {
155+
if validator, ok := blder.apiType.(admission.Validator); ok {
156+
return admission.ValidatingWebhookFor(validator)
157+
}
158+
if validator, ok := blder.forType.(admission.ValidatorFor); ok {
159+
return admission.ValidatorForToWebhook(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,16 @@ 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+
if blder.forType != nil {
188+
return blder.forType.For(), nil
189+
}
190+
return nil, errors.New("one of For() or HandlerFor() should be called")
191+
}
192+
148193
func (blder *WebhookBuilder) isAlreadyHandled(path string) bool {
149194
if blder.mgr.GetWebhookServer().WebhookMux == nil {
150195
return false
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package admission
2+
3+
import "k8s.io/apimachinery/pkg/runtime"
4+
5+
// For registers a type as an admission handler.
6+
type For interface {
7+
For() runtime.Object
8+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
"net/http"
23+
24+
"k8s.io/apimachinery/pkg/runtime"
25+
)
26+
27+
// DefaulterFor defines functions for setting defaults on resources.
28+
type DefaulterFor interface {
29+
For
30+
Default(ctx context.Context, obj runtime.Object)
31+
}
32+
33+
// DefaulterForToWebhook creates a new Webhook for a DefaulterFor interface.
34+
func DefaulterForToWebhook(defaulter DefaulterFor) *Webhook {
35+
return &Webhook{
36+
Handler: &defaulterForType{handler: defaulter},
37+
}
38+
}
39+
40+
type defaulterForType struct {
41+
handler DefaulterFor
42+
decoder *Decoder
43+
scheme *runtime.Scheme
44+
}
45+
46+
var _ DecoderInjector = &defaulterForType{}
47+
48+
func (h *defaulterForType) InjectDecoder(d *Decoder) error {
49+
h.decoder = d
50+
return nil
51+
}
52+
53+
// Handle handles admission requests.
54+
func (h *defaulterForType) Handle(ctx context.Context, req Request) Response {
55+
if h.handler == nil {
56+
panic("defaulter should never be nil")
57+
}
58+
59+
// Get the object in the request
60+
obj := h.handler.For().DeepCopyObject()
61+
if err := h.decoder.Decode(req, obj); err != nil {
62+
return Errored(http.StatusBadRequest, err)
63+
}
64+
65+
// Default the object
66+
h.handler.Default(ctx, obj)
67+
marshalled, err := json.Marshal(obj)
68+
if err != nil {
69+
return Errored(http.StatusInternalServerError, err)
70+
}
71+
72+
// Create the patch
73+
return PatchResponseFromRaw(req.Object.Raw, marshalled)
74+
}
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+
// Validator defines functions for validating an operation.
30+
type ValidatorFor interface {
31+
For
32+
ValidateCreate(ctx context.Context, obj runtime.Object) error
33+
ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) error
34+
ValidateDelete(ctx context.Context, obj runtime.Object) error
35+
}
36+
37+
// ValidatingWebhookFor creates a new Webhook for validating the provided type.
38+
func ValidatorForToWebhook(validator ValidatorFor) *Webhook {
39+
return &Webhook{
40+
Handler: &validatorForType{handler: validator},
41+
}
42+
}
43+
44+
type validatorForType struct {
45+
handler ValidatorFor
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.handler.For().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+
}

0 commit comments

Comments
 (0)