From f9cd031d59a5d73c5019b01cfed5c014c67986ad Mon Sep 17 00:00:00 2001 From: wangjianyu Date: Sun, 28 Apr 2024 15:08:48 +0800 Subject: [PATCH] webhook: optimize webhook patchResponse function (#2025) Signed-off-by: wangjianyu.wjy Co-authored-by: wangjianyu.wjy --- pkg/webhook/node/mutating/mutating_handler.go | 6 +++++- pkg/webhook/pod/mutating/mutating_handler.go | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/webhook/node/mutating/mutating_handler.go b/pkg/webhook/node/mutating/mutating_handler.go index 42e7f81b6..2e40846e6 100644 --- a/pkg/webhook/node/mutating/mutating_handler.go +++ b/pkg/webhook/node/mutating/mutating_handler.go @@ -131,7 +131,11 @@ func (h *NodeMutatingHandler) Handle(ctx context.Context, req admission.Request) klog.Errorf("Failed to marshal mutated Node %s, err: %v", obj.Name, err) return admission.Errored(http.StatusInternalServerError, err) } - return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshaled) + original, err := json.Marshal(clone) + if err != nil { + return admission.Errored(http.StatusInternalServerError, err) + } + return admission.PatchResponseFromRaw(original, marshaled) } var _ inject.Client = &NodeMutatingHandler{} diff --git a/pkg/webhook/pod/mutating/mutating_handler.go b/pkg/webhook/pod/mutating/mutating_handler.go index 1c8874bd2..539c3bd1f 100644 --- a/pkg/webhook/pod/mutating/mutating_handler.go +++ b/pkg/webhook/pod/mutating/mutating_handler.go @@ -94,7 +94,11 @@ func (h *PodMutatingHandler) Handle(ctx context.Context, req admission.Request) klog.Errorf("Failed to marshal mutated Pod %s/%s, err: %v", obj.Namespace, obj.Name, err) return admission.Errored(http.StatusInternalServerError, err) } - return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshaled) + original, err := json.Marshal(clone) + if err != nil { + return admission.Errored(http.StatusInternalServerError, err) + } + return admission.PatchResponseFromRaw(original, marshaled) } func (h *PodMutatingHandler) handleCreate(ctx context.Context, req admission.Request, obj *corev1.Pod) error {