Skip to content

Commit

Permalink
webhook: optimize webhook patchResponse function (#2025)
Browse files Browse the repository at this point in the history
Signed-off-by: wangjianyu.wjy <wangjianyu.wjy@alibaba-inc.com>
Co-authored-by: wangjianyu.wjy <wangjianyu.wjy@alibaba-inc.com>
  • Loading branch information
ZiMengSheng and wangjianyu.wjy committed Apr 28, 2024
1 parent 5d73698 commit f9cd031
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion pkg/webhook/node/mutating/mutating_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
6 changes: 5 additions & 1 deletion pkg/webhook/pod/mutating/mutating_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f9cd031

Please sign in to comment.