Skip to content

Commit

Permalink
Improve TransformUnstructured; Log GVK, type value (#2717)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
tdmanv and mergify[bot] committed Mar 5, 2024
1 parent 15dff38 commit ba26708
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 8 additions & 4 deletions pkg/kube/snapshot/snapshot_alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,17 @@ func UnstructuredVolumeSnapshotClassAlpha(name, driver, deletionPolicy string, p
}
}

// TransformUnstructured maps Unstructured object to object pointed by value
func TransformUnstructured(u *unstructured.Unstructured, value interface{}) error {
// TransformUnstructured maps Unstructured object to object pointed by obj
func TransformUnstructured(u *unstructured.Unstructured, obj metav1.Object) error {
if u == nil {
return errors.Errorf("Cannot deserialize nil unstructured")
}
b, err := json.Marshal(u.Object)
if err != nil {
return errors.Wrapf(err, "Failed to Marshal unstructured object")
gvk := u.GetObjectKind().GroupVersionKind()
return errors.Wrapf(err, "Failed to Marshal unstructured object GroupVersionKind: %v", gvk)
}
err = json.Unmarshal(b, value)
err = json.Unmarshal(b, obj)
return errors.Wrapf(err, "Failed to Unmarshal unstructured object")
}

Expand Down
14 changes: 13 additions & 1 deletion pkg/kube/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ func (s *SnapshotTestSuite) TestVolumeSnapshotCloneFake(c *C) {
contentGVR schema.GroupVersionResource
snapSpec *unstructured.Unstructured
snapGVR schema.GroupVersionResource
snapContentObject interface{}
snapContentObject metav1.Object
fakeSs snapshot.Snapshotter
}{
{
Expand Down Expand Up @@ -1467,3 +1467,15 @@ func fakePVC(name, namespace string) *corev1.PersistentVolumeClaim {
},
}
}

type SnapshotTransformUnstructuredTestSuite struct{}

var _ = Suite(&SnapshotTransformUnstructuredTestSuite{})

func (s *SnapshotTransformUnstructuredTestSuite) TestNilUnstructured(c *C) {
err := snapshot.TransformUnstructured(nil, nil)
c.Check(err, ErrorMatches, "Cannot deserialize nil unstructured")
u := &unstructured.Unstructured{}
err = snapshot.TransformUnstructured(u, nil)
c.Check(err, ErrorMatches, "Failed to Unmarshal unstructured object: json: Unmarshal\\(nil\\)")
}

0 comments on commit ba26708

Please sign in to comment.