optimize webhook patchResponse function
Signed-off-by: liheng.zms <liheng.zms@alibaba-inc.com>
This commit is contained in:
parent
72e1c0b936
commit
1c7ada9a32
|
|
@ -99,48 +99,58 @@ func (h *WorkloadHandler) Handle(ctx context.Context, req admission.Request) adm
|
||||||
if err := h.Decoder.Decode(req, newObj); err != nil {
|
if err := h.Decoder.Decode(req, newObj); err != nil {
|
||||||
return admission.Errored(http.StatusBadRequest, err)
|
return admission.Errored(http.StatusBadRequest, err)
|
||||||
}
|
}
|
||||||
|
newObjClone := newObj.DeepCopy()
|
||||||
oldObj := &kruiseappsv1alpha1.CloneSet{}
|
oldObj := &kruiseappsv1alpha1.CloneSet{}
|
||||||
if err := h.Decoder.Decode(
|
if err := h.Decoder.Decode(
|
||||||
admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Object: req.AdmissionRequest.OldObject}},
|
admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Object: req.AdmissionRequest.OldObject}},
|
||||||
oldObj); err != nil {
|
oldObj); err != nil {
|
||||||
return admission.Errored(http.StatusBadRequest, err)
|
return admission.Errored(http.StatusBadRequest, err)
|
||||||
}
|
}
|
||||||
changed, err := h.handleCloneSet(newObj, oldObj)
|
changed, err := h.handleCloneSet(newObjClone, oldObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return admission.Errored(http.StatusBadRequest, err)
|
return admission.Errored(http.StatusBadRequest, err)
|
||||||
}
|
}
|
||||||
if !changed {
|
if !changed {
|
||||||
return admission.Allowed("")
|
return admission.Allowed("")
|
||||||
}
|
}
|
||||||
marshalled, err := json.Marshal(newObj)
|
marshalled, err := json.Marshal(newObjClone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return admission.Errored(http.StatusInternalServerError, err)
|
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:
|
case util.ControllerKruiseKindDS.Kind:
|
||||||
// check daemonset
|
// check daemonset
|
||||||
newObj := &kruiseappsv1alpha1.DaemonSet{}
|
newObj := &kruiseappsv1alpha1.DaemonSet{}
|
||||||
if err := h.Decoder.Decode(req, newObj); err != nil {
|
if err := h.Decoder.Decode(req, newObj); err != nil {
|
||||||
return admission.Errored(http.StatusBadRequest, err)
|
return admission.Errored(http.StatusBadRequest, err)
|
||||||
}
|
}
|
||||||
|
newObjClone := newObj.DeepCopy()
|
||||||
oldObj := &kruiseappsv1alpha1.DaemonSet{}
|
oldObj := &kruiseappsv1alpha1.DaemonSet{}
|
||||||
if err := h.Decoder.Decode(
|
if err := h.Decoder.Decode(
|
||||||
admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Object: req.AdmissionRequest.OldObject}},
|
admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Object: req.AdmissionRequest.OldObject}},
|
||||||
oldObj); err != nil {
|
oldObj); err != nil {
|
||||||
return admission.Errored(http.StatusBadRequest, err)
|
return admission.Errored(http.StatusBadRequest, err)
|
||||||
}
|
}
|
||||||
changed, err := h.handleDaemonSet(newObj, oldObj)
|
changed, err := h.handleDaemonSet(newObjClone, oldObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return admission.Errored(http.StatusBadRequest, err)
|
return admission.Errored(http.StatusBadRequest, err)
|
||||||
}
|
}
|
||||||
if !changed {
|
if !changed {
|
||||||
return admission.Allowed("")
|
return admission.Allowed("")
|
||||||
}
|
}
|
||||||
marshalled, err := json.Marshal(newObj)
|
marshalled, err := json.Marshal(newObjClone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return admission.Errored(http.StatusInternalServerError, err)
|
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
|
// native k8s deloyment
|
||||||
|
|
@ -152,24 +162,29 @@ func (h *WorkloadHandler) Handle(ctx context.Context, req admission.Request) adm
|
||||||
if err := h.Decoder.Decode(req, newObj); err != nil {
|
if err := h.Decoder.Decode(req, newObj); err != nil {
|
||||||
return admission.Errored(http.StatusBadRequest, err)
|
return admission.Errored(http.StatusBadRequest, err)
|
||||||
}
|
}
|
||||||
|
newObjClone := newObj.DeepCopy()
|
||||||
oldObj := &apps.Deployment{}
|
oldObj := &apps.Deployment{}
|
||||||
if err := h.Decoder.Decode(
|
if err := h.Decoder.Decode(
|
||||||
admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Object: req.AdmissionRequest.OldObject}},
|
admission.Request{AdmissionRequest: admissionv1.AdmissionRequest{Object: req.AdmissionRequest.OldObject}},
|
||||||
oldObj); err != nil {
|
oldObj); err != nil {
|
||||||
return admission.Errored(http.StatusBadRequest, err)
|
return admission.Errored(http.StatusBadRequest, err)
|
||||||
}
|
}
|
||||||
changed, err := h.handleDeployment(newObj, oldObj)
|
changed, err := h.handleDeployment(newObjClone, oldObj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return admission.Errored(http.StatusBadRequest, err)
|
return admission.Errored(http.StatusBadRequest, err)
|
||||||
}
|
}
|
||||||
if !changed {
|
if !changed {
|
||||||
return admission.Allowed("")
|
return admission.Allowed("")
|
||||||
}
|
}
|
||||||
marshalled, err := json.Marshal(newObj)
|
marshalled, err := json.Marshal(newObjClone)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return admission.Errored(http.StatusInternalServerError, err)
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -131,6 +131,7 @@ var _ = SIGDescribe("Rollout", func() {
|
||||||
}
|
}
|
||||||
// daemon.Spec.Replicas = utilpointer.Int32(*object.Spec.Replicas)
|
// daemon.Spec.Replicas = utilpointer.Int32(*object.Spec.Replicas)
|
||||||
daemon.Spec.Template = *object.Spec.Template.DeepCopy()
|
daemon.Spec.Template = *object.Spec.Template.DeepCopy()
|
||||||
|
daemon.Spec.UpdateStrategy = *object.Spec.UpdateStrategy.DeepCopy()
|
||||||
daemon.Labels = mergeMap(daemon.Labels, object.Labels)
|
daemon.Labels = mergeMap(daemon.Labels, object.Labels)
|
||||||
daemon.Annotations = mergeMap(daemon.Annotations, object.Annotations)
|
daemon.Annotations = mergeMap(daemon.Annotations, object.Annotations)
|
||||||
return k8sClient.Update(context.TODO(), daemon)
|
return k8sClient.Update(context.TODO(), daemon)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue