Skip to content

Commit

Permalink
Merge pull request #2736 from vincepri/decoder-interface
Browse files Browse the repository at this point in the history
⚠ admission.Decoder is now an interface
  • Loading branch information
k8s-ci-robot committed Mar 27, 2024
2 parents 8afbb50 + 04c37a9 commit 27afc15
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 12 deletions.
25 changes: 19 additions & 6 deletions pkg/webhook/admission/decode.go
Expand Up @@ -26,22 +26,35 @@ import (

// Decoder knows how to decode the contents of an admission
// request into a concrete object.
type Decoder struct {
type Decoder interface {
// Decode decodes the inlined object in the AdmissionRequest into the passed-in runtime.Object.
// If you want decode the OldObject in the AdmissionRequest, use DecodeRaw.
// It errors out if req.Object.Raw is empty i.e. containing 0 raw bytes.
Decode(req Request, into runtime.Object) error

// DecodeRaw decodes a RawExtension object into the passed-in runtime.Object.
// It errors out if rawObj is empty i.e. containing 0 raw bytes.
DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) error
}

// decoder knows how to decode the contents of an admission
// request into a concrete object.
type decoder struct {
codecs serializer.CodecFactory
}

// NewDecoder creates a Decoder given the runtime.Scheme.
func NewDecoder(scheme *runtime.Scheme) *Decoder {
// NewDecoder creates a decoder given the runtime.Scheme.
func NewDecoder(scheme *runtime.Scheme) Decoder {
if scheme == nil {
panic("scheme should never be nil")
}
return &Decoder{codecs: serializer.NewCodecFactory(scheme)}
return &decoder{codecs: serializer.NewCodecFactory(scheme)}
}

// Decode decodes the inlined object in the AdmissionRequest into the passed-in runtime.Object.
// If you want decode the OldObject in the AdmissionRequest, use DecodeRaw.
// It errors out if req.Object.Raw is empty i.e. containing 0 raw bytes.
func (d *Decoder) Decode(req Request, into runtime.Object) error {
func (d *decoder) Decode(req Request, into runtime.Object) error {
// we error out if rawObj is an empty object.
if len(req.Object.Raw) == 0 {
return fmt.Errorf("there is no content to decode")
Expand All @@ -51,7 +64,7 @@ func (d *Decoder) Decode(req Request, into runtime.Object) error {

// DecodeRaw decodes a RawExtension object into the passed-in runtime.Object.
// It errors out if rawObj is empty i.e. containing 0 raw bytes.
func (d *Decoder) DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) error {
func (d *decoder) DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) error {
// NB(directxman12): there's a bug/weird interaction between decoders and
// the API server where the API server doesn't send a GVK on the embedded
// objects, which means the unstructured decoder refuses to decode. It
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/admission/decode_test.go
Expand Up @@ -29,7 +29,7 @@ import (
)

var _ = Describe("Admission Webhook Decoder", func() {
var decoder *Decoder
var decoder Decoder
BeforeEach(func() {
By("creating a new decoder for a scheme")
decoder = NewDecoder(scheme.Scheme)
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/admission/defaulter.go
Expand Up @@ -43,7 +43,7 @@ func DefaultingWebhookFor(scheme *runtime.Scheme, defaulter Defaulter) *Webhook

type mutatingHandler struct {
defaulter Defaulter
decoder *Decoder
decoder Decoder
}

// Handle handles admission requests.
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/admission/defaulter_custom.go
Expand Up @@ -43,7 +43,7 @@ func WithCustomDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter C
type defaulterForType struct {
defaulter CustomDefaulter
object runtime.Object
decoder *Decoder
decoder Decoder
}

// Handle handles admission requests.
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/admission/validator.go
Expand Up @@ -63,7 +63,7 @@ func ValidatingWebhookFor(scheme *runtime.Scheme, validator Validator) *Webhook

type validatingHandler struct {
validator Validator
decoder *Decoder
decoder Decoder
}

// Handle handles admission requests.
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/admission/validator_custom.go
Expand Up @@ -56,7 +56,7 @@ func WithCustomValidator(scheme *runtime.Scheme, obj runtime.Object, validator C
type validatorForType struct {
validator CustomValidator
object runtime.Object
decoder *Decoder
decoder Decoder
}

// Handle handles admission requests.
Expand Down
2 changes: 1 addition & 1 deletion pkg/webhook/webhook_integration_test.go
Expand Up @@ -153,7 +153,7 @@ var _ = Describe("Webhook", func() {
})

type rejectingValidator struct {
d *admission.Decoder
d admission.Decoder
}

func (v *rejectingValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
Expand Down

0 comments on commit 27afc15

Please sign in to comment.