Skip to content

Commit 3d4f250

Browse files
committed
patchHelper: call toUnstructured only if necessary
Co-authored-by: fabriziopandini [email protected] Signed-off-by: Stefan Büringer [email protected]
1 parent 199edc1 commit 3d4f250

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

util/patch/patch.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,12 @@ func NewHelper(obj client.Object, crClient client.Client) (*Helper, error) {
6565
return nil, errors.Wrapf(err, "failed to create patch helper for object %s", klog.KObj(obj))
6666
}
6767

68-
// Convert the object to unstructured to compare against our before copy.
69-
unstructuredObj, err := toUnstructured(obj, gvk)
70-
if err != nil {
71-
return nil, errors.Wrapf(err, "failed to create patch helper for %s %s: failed to convert object to Unstructured", gvk.Kind, klog.KObj(obj))
72-
}
73-
7468
// Check if the object satisfies the Cluster API conditions contract.
7569
_, canInterfaceConditions := obj.(conditions.Setter)
7670

7771
return &Helper{
7872
client: crClient,
7973
gvk: gvk,
80-
before: unstructuredObj,
8174
beforeObject: obj.DeepCopyObject().(client.Object),
8275
isConditionsSetter: canInterfaceConditions,
8376
}, nil
@@ -105,10 +98,16 @@ func (h *Helper) Patch(ctx context.Context, obj client.Object, opts ...Option) e
10598
opt.ApplyToHelper(options)
10699
}
107100

108-
// Convert the object to unstructured to compare against our before copy.
101+
// Convert the before object to unstructured.
102+
h.before, err = toUnstructured(h.beforeObject, gvk)
103+
if err != nil {
104+
return errors.Wrapf(err, "failed to patch %s %s: failed to convert before object to Unstructured", h.gvk.Kind, klog.KObj(h.beforeObject))
105+
}
106+
107+
// Convert the after object to unstructured.
109108
h.after, err = toUnstructured(obj, gvk)
110109
if err != nil {
111-
return errors.Wrapf(err, "failed to patch %s %s: failed to convert object to Unstructured", h.gvk.Kind, klog.KObj(h.beforeObject))
110+
return errors.Wrapf(err, "failed to patch %s %s: failed to convert after object to Unstructured", h.gvk.Kind, klog.KObj(h.beforeObject))
112111
}
113112

114113
// Determine if the object has status.

0 commit comments

Comments
 (0)