Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve TransformUnstructured; Log GVK, type value #2717

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\\)")
}