Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 patchHelper: call toUnstructured only if necessary #11665

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading