Skip to content

Commit

Permalink
optimize webhook patchResponse function (#165)
Browse files Browse the repository at this point in the history
Signed-off-by: liheng.zms <[email protected]>
  • Loading branch information
zmberg committed Jul 18, 2023
1 parent 84c9d86 commit 3c7f689
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
33 changes: 24 additions & 9 deletions pkg/webhook/workload/mutating/workload_update_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,48 +99,58 @@ func (h *WorkloadHandler) Handle(ctx context.Context, req admission.Request) adm
if err := h.Decoder.Decode(req, newObj); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
newObjClone := newObj.DeepCopy()
oldObj := &kruiseappsv1alpha1.CloneSet{}
if err := h.Decoder.Decode(
admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Object: req.AdmissionRequest.OldObject}},
oldObj); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
changed, err := h.handleCloneSet(newObj, oldObj)
changed, err := h.handleCloneSet(newObjClone, oldObj)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
if !changed {
return admission.Allowed("")
}
marshalled, err := json.Marshal(newObj)
marshalled, err := json.Marshal(newObjClone)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshalled)
original, err := json.Marshal(newObj)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.PatchResponseFromRaw(original, marshalled)
case util.ControllerKruiseKindDS.Kind:
// check daemonset
newObj := &kruiseappsv1alpha1.DaemonSet{}
if err := h.Decoder.Decode(req, newObj); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
newObjClone := newObj.DeepCopy()
oldObj := &kruiseappsv1alpha1.DaemonSet{}
if err := h.Decoder.Decode(
admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Object: req.AdmissionRequest.OldObject}},
oldObj); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
changed, err := h.handleDaemonSet(newObj, oldObj)
changed, err := h.handleDaemonSet(newObjClone, oldObj)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
if !changed {
return admission.Allowed("")
}
marshalled, err := json.Marshal(newObj)
marshalled, err := json.Marshal(newObjClone)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshalled)
original, err := json.Marshal(newObj)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.PatchResponseFromRaw(original, marshalled)
}

// native k8s deloyment
Expand All @@ -152,24 +162,29 @@ func (h *WorkloadHandler) Handle(ctx context.Context, req admission.Request) adm
if err := h.Decoder.Decode(req, newObj); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
newObjClone := newObj.DeepCopy()
oldObj := &apps.Deployment{}
if err := h.Decoder.Decode(
admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Object: req.AdmissionRequest.OldObject}},
oldObj); err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
changed, err := h.handleDeployment(newObj, oldObj)
changed, err := h.handleDeployment(newObjClone, oldObj)
if err != nil {
return admission.Errored(http.StatusBadRequest, err)
}
if !changed {
return admission.Allowed("")
}
marshalled, err := json.Marshal(newObj)
marshalled, err := json.Marshal(newObjClone)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
original, err := json.Marshal(newObj)
if err != nil {
return admission.Errored(http.StatusInternalServerError, err)
}
return admission.PatchResponseFromRaw(req.AdmissionRequest.Object.Raw, marshalled)
return admission.PatchResponseFromRaw(original, marshalled)
}
}

Expand Down
1 change: 1 addition & 0 deletions test/e2e/rollout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ var _ = SIGDescribe("Rollout", func() {
}
// daemon.Spec.Replicas = utilpointer.Int32(*object.Spec.Replicas)
daemon.Spec.Template = *object.Spec.Template.DeepCopy()
daemon.Spec.UpdateStrategy = *object.Spec.UpdateStrategy.DeepCopy()
daemon.Labels = mergeMap(daemon.Labels, object.Labels)
daemon.Annotations = mergeMap(daemon.Annotations, object.Annotations)
return k8sClient.Update(context.TODO(), daemon)
Expand Down

0 comments on commit 3c7f689

Please sign in to comment.