Skip to content

Commit 1004b4b

Browse files
authored
Merge pull request #824 from errordeveloper/update-to-unstructured
Update `ToUnstructured` to handle already unstructured case
2 parents facd2c2 + cca401b commit 1004b4b

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

ssa/normalize/normalize.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,18 @@ func FromUnstructuredWithScheme(object *unstructured.Unstructured, scheme *runti
6464

6565
// ToUnstructured converts a typed Kubernetes resource into the Unstructured
6666
// equivalent.
67-
func ToUnstructured(object metav1.Object) (*unstructured.Unstructured, error) {
68-
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(object)
67+
func ToUnstructured(obj metav1.Object) (*unstructured.Unstructured, error) {
68+
// If the incoming object is already unstructured, perform a deep copy first
69+
// otherwise DefaultUnstructuredConverter ends up returning the inner map without
70+
// making a copy.
71+
if unstructuredObj, ok := obj.(runtime.Unstructured); ok {
72+
obj = unstructuredObj.DeepCopyObject().(metav1.Object)
73+
}
74+
rawMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
6975
if err != nil {
7076
return nil, err
7177
}
72-
return &unstructured.Unstructured{Object: u}, nil
78+
return &unstructured.Unstructured{Object: rawMap}, nil
7379
}
7480

7581
// UnstructuredList normalizes a list of Unstructured objects by

0 commit comments

Comments
 (0)