From b89bd369163a6031cf7cbbdc2016f22d27a43862 Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Fri, 10 Jan 2025 13:52:43 +0100 Subject: [PATCH] patchHelper: call toUnstructured only if necessary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-by: Fabrizio Pandini fpandini@vmware.com Signed-off-by: Stefan Büringer buringerst@vmware.com --- util/patch/patch.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/util/patch/patch.go b/util/patch/patch.go index ad8d0e531184..73e60f253d39 100644 --- a/util/patch/patch.go +++ b/util/patch/patch.go @@ -84,16 +84,9 @@ func NewHelper(obj client.Object, crClient client.Client) (*Helper, error) { return nil, errors.Wrapf(err, "failed to identify condition fields for object %s", klog.KObj(obj)) } - // Convert the object to unstructured to compare against our before copy. - unstructuredObj, err := toUnstructured(obj, gvk) - if err != nil { - return nil, errors.Wrapf(err, "failed to create patch helper for %s %s: failed to convert object to Unstructured", gvk.Kind, klog.KObj(obj)) - } - return &Helper{ client: crClient, gvk: gvk, - before: unstructuredObj, beforeObject: obj.DeepCopyObject().(client.Object), metav1ConditionsFieldPath: metav1ConditionsFieldPath, clusterv1ConditionsFieldPath: clusterv1ConditionsFieldPath, @@ -138,10 +131,16 @@ func (h *Helper) Patch(ctx context.Context, obj client.Object, opts ...Option) e h.metav1ConditionsFieldPath = nil } - // Convert the object to unstructured to compare against our before copy. + // Convert the before object to unstructured. + h.before, err = toUnstructured(h.beforeObject, gvk) + if err != nil { + return errors.Wrapf(err, "failed to patch %s %s: failed to convert before object to Unstructured", h.gvk.Kind, klog.KObj(h.beforeObject)) + } + + // Convert the after object to unstructured. h.after, err = toUnstructured(obj, gvk) if err != nil { - return errors.Wrapf(err, "failed to patch %s %s: failed to convert object to Unstructured", h.gvk.Kind, klog.KObj(h.beforeObject)) + return errors.Wrapf(err, "failed to patch %s %s: failed to convert after object to Unstructured", h.gvk.Kind, klog.KObj(h.beforeObject)) } // Determine if the object has status.