Skip to content

Commit

Permalink
patchHelper: call toUnstructured only if necessary
Browse files Browse the repository at this point in the history
Co-authored-by: Fabrizio Pandini fpandini@vmware.com

Signed-off-by: Stefan Büringer buringerst@vmware.com
  • Loading branch information
sbueringer committed Jan 10, 2025
1 parent 7f2a8cd commit 1c5f56d
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions util/patch/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1c5f56d

Please sign in to comment.