From cca401bfe5d32b5e4681e82d06841b903e0b3ca4 Mon Sep 17 00:00:00 2001 From: Ilya Dmitrichenko Date: Wed, 20 Nov 2024 20:12:04 +0000 Subject: [PATCH] Update `ToUnstructured` to handle already unstructured case This version of the function is more robus, it's what is used in Timoni, updating it hear would alow avoid copies in other places. Signed-off-by: Ilya Dmitrichenko --- ssa/normalize/normalize.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ssa/normalize/normalize.go b/ssa/normalize/normalize.go index ab25a8fe..6cd9dfdd 100644 --- a/ssa/normalize/normalize.go +++ b/ssa/normalize/normalize.go @@ -64,12 +64,18 @@ func FromUnstructuredWithScheme(object *unstructured.Unstructured, scheme *runti // ToUnstructured converts a typed Kubernetes resource into the Unstructured // equivalent. -func ToUnstructured(object metav1.Object) (*unstructured.Unstructured, error) { - u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(object) +func ToUnstructured(obj metav1.Object) (*unstructured.Unstructured, error) { + // If the incoming object is already unstructured, perform a deep copy first + // otherwise DefaultUnstructuredConverter ends up returning the inner map without + // making a copy. + if unstructuredObj, ok := obj.(runtime.Unstructured); ok { + obj = unstructuredObj.DeepCopyObject().(metav1.Object) + } + rawMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj) if err != nil { return nil, err } - return &unstructured.Unstructured{Object: u}, nil + return &unstructured.Unstructured{Object: rawMap}, nil } // UnstructuredList normalizes a list of Unstructured objects by