Skip to content

Commit e66d1ce

Browse files
author
Mengqi Yu
committed
⚠️ introduce PatchResponseFromRaw and drop PatchResponse
1 parent 0dd6edb commit e66d1ce

File tree

5 files changed

+17
-88
lines changed

5 files changed

+17
-88
lines changed

example/mutatingwebhook.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package main
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"net/http"
2223

2324
corev1 "k8s.io/api/core/v1"
@@ -44,13 +45,18 @@ func (a *podAnnotator) Handle(ctx context.Context, req types.Request) types.Resp
4445
if err != nil {
4546
return admission.ErrorResponse(http.StatusBadRequest, err)
4647
}
47-
copy := pod.DeepCopy()
4848

49-
err = a.mutatePodsFn(ctx, copy)
49+
err = a.mutatePodsFn(ctx, pod)
5050
if err != nil {
5151
return admission.ErrorResponse(http.StatusInternalServerError, err)
5252
}
53-
return admission.PatchResponse(pod, copy)
53+
54+
marshaledPod, err := json.Marshal(pod)
55+
if err != nil {
56+
return admission.ErrorResponse(http.StatusInternalServerError, err)
57+
}
58+
59+
return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshaledPod)
5460
}
5561

5662
// mutatePodsFn add an annotation to the given pod

pkg/patch/doc.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

pkg/patch/patch.go

Lines changed: 0 additions & 45 deletions
This file was deleted.

pkg/webhook/admission/response.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ package admission
1919
import (
2020
"net/http"
2121

22+
"github.com/appscode/jsonpatch"
23+
2224
admissionv1beta1 "k8s.io/api/admission/v1beta1"
2325
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24-
"k8s.io/apimachinery/pkg/runtime"
25-
"sigs.k8s.io/controller-runtime/pkg/patch"
2626
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
2727
)
2828

@@ -54,9 +54,11 @@ func ValidationResponse(allowed bool, reason string) types.Response {
5454
return resp
5555
}
5656

57-
// PatchResponse returns a new response with json patch.
58-
func PatchResponse(original, current runtime.Object) types.Response {
59-
patches, err := patch.NewJSONPatch(original, current)
57+
// PatchResponseFromRaw takes 2 byte arrays and returns a new response with json patch.
58+
// The original object should be passed in as raw bytes to avoid the roundtripping problem
59+
// described in https://github.com/kubernetes-sigs/kubebuilder/issues/510.
60+
func PatchResponseFromRaw(original, current []byte) types.Response {
61+
patches, err := jsonpatch.CreatePatch(original, current)
6062
if err != nil {
6163
return ErrorResponse(http.StatusInternalServerError, err)
6264
}

pkg/webhook/admission/response_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
. "github.com/onsi/gomega"
2626

2727
admissionv1beta1 "k8s.io/api/admission/v1beta1"
28-
corev1 "k8s.io/api/core/v1"
2928
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3029
"sigs.k8s.io/controller-runtime/pkg/webhook/admission/types"
3130
)
@@ -72,7 +71,7 @@ var _ = Describe("admission webhook response", func() {
7271
PatchType: func() *admissionv1beta1.PatchType { pt := admissionv1beta1.PatchTypeJSONPatch; return &pt }(),
7372
},
7473
}
75-
resp := PatchResponse(&corev1.Pod{}, &corev1.Pod{})
74+
resp := PatchResponseFromRaw([]byte(`{}`), []byte(`{}`))
7675
Expect(resp).To(Equal(expected))
7776
})
7877
})

0 commit comments

Comments
 (0)