Skip to content

Commit 27afc15

Browse files
authored
Merge pull request #2736 from vincepri/decoder-interface
⚠ admission.Decoder is now an interface
2 parents 8afbb50 + 04c37a9 commit 27afc15

File tree

7 files changed

+25
-12
lines changed

7 files changed

+25
-12
lines changed

pkg/webhook/admission/decode.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,35 @@ import (
2626

2727
// Decoder knows how to decode the contents of an admission
2828
// request into a concrete object.
29-
type Decoder struct {
29+
type Decoder interface {
30+
// Decode decodes the inlined object in the AdmissionRequest into the passed-in runtime.Object.
31+
// If you want decode the OldObject in the AdmissionRequest, use DecodeRaw.
32+
// It errors out if req.Object.Raw is empty i.e. containing 0 raw bytes.
33+
Decode(req Request, into runtime.Object) error
34+
35+
// DecodeRaw decodes a RawExtension object into the passed-in runtime.Object.
36+
// It errors out if rawObj is empty i.e. containing 0 raw bytes.
37+
DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) error
38+
}
39+
40+
// decoder knows how to decode the contents of an admission
41+
// request into a concrete object.
42+
type decoder struct {
3043
codecs serializer.CodecFactory
3144
}
3245

33-
// NewDecoder creates a Decoder given the runtime.Scheme.
34-
func NewDecoder(scheme *runtime.Scheme) *Decoder {
46+
// NewDecoder creates a decoder given the runtime.Scheme.
47+
func NewDecoder(scheme *runtime.Scheme) Decoder {
3548
if scheme == nil {
3649
panic("scheme should never be nil")
3750
}
38-
return &Decoder{codecs: serializer.NewCodecFactory(scheme)}
51+
return &decoder{codecs: serializer.NewCodecFactory(scheme)}
3952
}
4053

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

5265
// DecodeRaw decodes a RawExtension object into the passed-in runtime.Object.
5366
// It errors out if rawObj is empty i.e. containing 0 raw bytes.
54-
func (d *Decoder) DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) error {
67+
func (d *decoder) DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) error {
5568
// NB(directxman12): there's a bug/weird interaction between decoders and
5669
// the API server where the API server doesn't send a GVK on the embedded
5770
// objects, which means the unstructured decoder refuses to decode. It

pkg/webhook/admission/decode_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
var _ = Describe("Admission Webhook Decoder", func() {
32-
var decoder *Decoder
32+
var decoder Decoder
3333
BeforeEach(func() {
3434
By("creating a new decoder for a scheme")
3535
decoder = NewDecoder(scheme.Scheme)

pkg/webhook/admission/defaulter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func DefaultingWebhookFor(scheme *runtime.Scheme, defaulter Defaulter) *Webhook
4343

4444
type mutatingHandler struct {
4545
defaulter Defaulter
46-
decoder *Decoder
46+
decoder Decoder
4747
}
4848

4949
// Handle handles admission requests.

pkg/webhook/admission/defaulter_custom.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func WithCustomDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter C
4343
type defaulterForType struct {
4444
defaulter CustomDefaulter
4545
object runtime.Object
46-
decoder *Decoder
46+
decoder Decoder
4747
}
4848

4949
// Handle handles admission requests.

pkg/webhook/admission/validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func ValidatingWebhookFor(scheme *runtime.Scheme, validator Validator) *Webhook
6363

6464
type validatingHandler struct {
6565
validator Validator
66-
decoder *Decoder
66+
decoder Decoder
6767
}
6868

6969
// Handle handles admission requests.

pkg/webhook/admission/validator_custom.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func WithCustomValidator(scheme *runtime.Scheme, obj runtime.Object, validator C
5656
type validatorForType struct {
5757
validator CustomValidator
5858
object runtime.Object
59-
decoder *Decoder
59+
decoder Decoder
6060
}
6161

6262
// Handle handles admission requests.

pkg/webhook/webhook_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ var _ = Describe("Webhook", func() {
153153
})
154154

155155
type rejectingValidator struct {
156-
d *admission.Decoder
156+
d admission.Decoder
157157
}
158158

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

0 commit comments

Comments
 (0)