-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ 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]>
- Loading branch information
Showing
4 changed files
with
258 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package admission | ||
|
||
import "k8s.io/apimachinery/pkg/runtime" | ||
|
||
// For registers a type as an admission handler. | ||
type For interface { | ||
For() runtime.Object | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
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" | ||
"net/http" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// DefaulterFor defines functions for setting defaults on resources. | ||
type DefaulterFor interface { | ||
For | ||
Default(ctx context.Context, obj runtime.Object) | ||
} | ||
|
||
// DefaulterForToWebhook creates a new Webhook for a DefaulterFor interface. | ||
func DefaulterForToWebhook(defaulter DefaulterFor) *Webhook { | ||
return &Webhook{ | ||
Handler: &defaulterForType{handler: defaulter}, | ||
} | ||
} | ||
|
||
type defaulterForType struct { | ||
handler DefaulterFor | ||
decoder *Decoder | ||
scheme *runtime.Scheme | ||
} | ||
|
||
var _ DecoderInjector = &defaulterForType{} | ||
|
||
func (h *defaulterForType) InjectDecoder(d *Decoder) error { | ||
h.decoder = d | ||
return nil | ||
} | ||
|
||
// Handle handles admission requests. | ||
func (h *defaulterForType) Handle(ctx context.Context, req Request) Response { | ||
if h.handler == nil { | ||
panic("defaulter should never be nil") | ||
} | ||
|
||
// Get the object in the request | ||
obj := h.handler.For().DeepCopyObject() | ||
if err := h.decoder.Decode(req, obj); err != nil { | ||
return Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
// Default the object | ||
h.handler.Default(ctx, obj) | ||
marshalled, err := json.Marshal(obj) | ||
if err != nil { | ||
return Errored(http.StatusInternalServerError, err) | ||
} | ||
|
||
// Create the patch | ||
return PatchResponseFromRaw(req.Object.Raw, marshalled) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
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" | ||
goerrors "errors" | ||
"net/http" | ||
|
||
v1 "k8s.io/api/admission/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
// Validator defines functions for validating an operation. | ||
type ValidatorFor interface { | ||
For | ||
ValidateCreate(ctx context.Context, obj runtime.Object) error | ||
ValidateUpdate(ctx context.Context, old runtime.Object, new runtime.Object) error | ||
ValidateDelete(ctx context.Context, obj runtime.Object) error | ||
} | ||
|
||
// ValidatingWebhookFor creates a new Webhook for validating the provided type. | ||
func ValidatorForToWebhook(validator ValidatorFor) *Webhook { | ||
return &Webhook{ | ||
Handler: &validatorForType{handler: validator}, | ||
} | ||
} | ||
|
||
type validatorForType struct { | ||
handler ValidatorFor | ||
decoder *Decoder | ||
} | ||
|
||
var _ DecoderInjector = &validatorForType{} | ||
|
||
// InjectDecoder injects the decoder into a validatingHandler. | ||
func (h *validatorForType) InjectDecoder(d *Decoder) error { | ||
h.decoder = d | ||
return nil | ||
} | ||
|
||
// Handle handles admission requests. | ||
func (h *validatorForType) Handle(ctx context.Context, req Request) Response { | ||
if h.handler == nil { | ||
panic("handler should never be nil") | ||
} | ||
|
||
// Get the object in the request | ||
obj := h.handler.For().DeepCopyObject() | ||
if req.Operation == v1.Create { | ||
err := h.decoder.Decode(req, obj) | ||
if err != nil { | ||
return Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
err = h.handler.ValidateCreate(ctx, obj) | ||
if err != nil { | ||
var apiStatus apierrors.APIStatus | ||
if goerrors.As(err, &apiStatus) { | ||
return validationResponseFromStatus(false, apiStatus.Status()) | ||
} | ||
return Denied(err.Error()) | ||
} | ||
} | ||
|
||
if req.Operation == v1.Update { | ||
oldObj := obj.DeepCopyObject() | ||
|
||
err := h.decoder.DecodeRaw(req.Object, obj) | ||
if err != nil { | ||
return Errored(http.StatusBadRequest, err) | ||
} | ||
err = h.decoder.DecodeRaw(req.OldObject, oldObj) | ||
if err != nil { | ||
return Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
err = h.handler.ValidateUpdate(ctx, oldObj, obj) | ||
if err != nil { | ||
var apiStatus apierrors.APIStatus | ||
if goerrors.As(err, &apiStatus) { | ||
return validationResponseFromStatus(false, apiStatus.Status()) | ||
} | ||
return Denied(err.Error()) | ||
} | ||
} | ||
|
||
if req.Operation == v1.Delete { | ||
// In reference to PR: https://github.com/kubernetes/kubernetes/pull/76346 | ||
// OldObject contains the object being deleted | ||
err := h.decoder.DecodeRaw(req.OldObject, obj) | ||
if err != nil { | ||
return Errored(http.StatusBadRequest, err) | ||
} | ||
|
||
err = h.handler.ValidateDelete(ctx, obj) | ||
if err != nil { | ||
var apiStatus apierrors.APIStatus | ||
if goerrors.As(err, &apiStatus) { | ||
return validationResponseFromStatus(false, apiStatus.Status()) | ||
} | ||
return Denied(err.Error()) | ||
} | ||
} | ||
|
||
return Allowed("") | ||
} |