Skip to content

Commit

Permalink
Update ToUnstructured to handle already unstructured case
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
errordeveloper committed Nov 20, 2024
1 parent facd2c2 commit cca401b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions ssa/normalize/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cca401b

Please sign in to comment.