Skip to content

Commit

Permalink
Merge pull request #824 from errordeveloper/update-to-unstructured
Browse files Browse the repository at this point in the history
Update `ToUnstructured` to handle already unstructured case
  • Loading branch information
stefanprodan authored Nov 23, 2024
2 parents facd2c2 + cca401b commit 1004b4b
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 1004b4b

Please sign in to comment.