Skip to content

Commit 41ba537

Browse files
author
Mengqi Yu
committed
✨ support decoding the old object in an AdmissionRequest
1 parent 1dafd94 commit 41ba537

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

example/mutatingwebhook.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type podAnnotator struct {
3636
func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admission.Response {
3737
pod := &corev1.Pod{}
3838

39-
err := a.decoder.Decode(req, pod)
39+
err := a.decoder.Decode(req.Object, pod)
4040
if err != nil {
4141
return admission.Errored(http.StatusBadRequest, err)
4242
}
@@ -51,7 +51,7 @@ func (a *podAnnotator) Handle(ctx context.Context, req admission.Request) admiss
5151
return admission.Errored(http.StatusInternalServerError, err)
5252
}
5353

54-
return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshaledPod)
54+
return admission.PatchResponseFromRaw(req.Object.Raw, marshaledPod)
5555
}
5656

5757
// podAnnotator implements inject.Client.
@@ -63,7 +63,7 @@ func (a *podAnnotator) InjectClient(c client.Client) error {
6363
return nil
6464
}
6565

66-
// podAnnotator implements inject.Decoder.
66+
// podAnnotator implements admission.DecoderInjector.
6767
// A decoder will be automatically injected.
6868

6969
// InjectDecoder injects the decoder.

example/validatingwebhook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type podValidator struct {
3636
func (v *podValidator) Handle(ctx context.Context, req admission.Request) admission.Response {
3737
pod := &corev1.Pod{}
3838

39-
err := v.decoder.Decode(req, pod)
39+
err := v.decoder.Decode(req.Object, pod)
4040
if err != nil {
4141
return admission.Errored(http.StatusBadRequest, err)
4242
}
@@ -62,7 +62,7 @@ func (v *podValidator) InjectClient(c client.Client) error {
6262
return nil
6363
}
6464

65-
// podValidator implements inject.Decoder.
65+
// podValidator implements admission.DecoderInjector.
6666
// A decoder will be automatically injected.
6767

6868
// InjectDecoder injects the decoder.

pkg/webhook/admission/decode.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ func NewDecoder(scheme *runtime.Scheme) (*Decoder, error) {
3535
}
3636

3737
// Decode decodes the inlined object in the AdmissionRequest into the passed-in runtime.Object.
38+
// If you want decode the OldObject in the AdmissionRequest, use DecodeRaw.
3839
func (d *Decoder) Decode(req Request, into runtime.Object) error {
40+
return d.DecodeRaw(req.Object, into)
41+
}
42+
43+
// DecodeRaw decodes a RawExtension object into the passed-in runtime.Object.
44+
func (d *Decoder) DecodeRaw(rawObj runtime.RawExtension, into runtime.Object) error {
3945
// NB(directxman12): there's a bug/weird interaction between decoders and
4046
// the API server where the API server doesn't send a GVK on the embedded
4147
// objects, which means the unstructured decoder refuses to decode. It
@@ -45,13 +51,13 @@ func (d *Decoder) Decode(req Request, into runtime.Object) error {
4551
// See kubernetes/kubernetes#74373.
4652
if unstructuredInto, isUnstructured := into.(*unstructured.Unstructured); isUnstructured {
4753
// unmarshal into unstructured's underlying object to avoid calling the decoder
48-
if err := json.Unmarshal(req.Object.Raw, &unstructuredInto.Object); err != nil {
54+
if err := json.Unmarshal(rawObj.Raw, &unstructuredInto.Object); err != nil {
4955
return err
5056
}
5157

5258
return nil
5359
}
5460

5561
deserializer := d.codecs.UniversalDeserializer()
56-
return runtime.DecodeInto(deserializer, req.AdmissionRequest.Object.Raw, into)
62+
return runtime.DecodeInto(deserializer, rawObj.Raw, into)
5763
}

0 commit comments

Comments
 (0)