From ba26708b9580e5f3f5b6fbfb3ead47caccee8f3c Mon Sep 17 00:00:00 2001 From: Tom Manville Date: Tue, 5 Mar 2024 11:47:14 -0800 Subject: [PATCH] Improve TransformUnstructured; Log GVK, type value (#2717) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- pkg/kube/snapshot/snapshot_alpha.go | 12 ++++++++---- pkg/kube/snapshot/snapshot_test.go | 14 +++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/kube/snapshot/snapshot_alpha.go b/pkg/kube/snapshot/snapshot_alpha.go index 923f79d609..a5630be028 100644 --- a/pkg/kube/snapshot/snapshot_alpha.go +++ b/pkg/kube/snapshot/snapshot_alpha.go @@ -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") } diff --git a/pkg/kube/snapshot/snapshot_test.go b/pkg/kube/snapshot/snapshot_test.go index cb3f9d0369..0675a2a177 100644 --- a/pkg/kube/snapshot/snapshot_test.go +++ b/pkg/kube/snapshot/snapshot_test.go @@ -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 }{ { @@ -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\\)") +}