From c742c31c70d6437c05c72486a2b94fb31d517204 Mon Sep 17 00:00:00 2001 From: Prateek Pandey Date: Tue, 26 Jun 2018 12:28:36 +0530 Subject: [PATCH] Retrieves the namespace from its VolumeSnapshotRef name instead of 'PersistentVolumeRef.Namespace'. (#43) * Retrieves the namespace from its VolumeSnapshotRef name Retrieves the volume namespace and the short name of a snapshot from its 'VolumeSnapshotRef.Name', instead of 'PersistentVolumeRef.Namespace'. exmaple SnapshotRef - "test-ns/snap1" Change required b/c 'PersistentVolumeRef.Namespace contains' nil value * Add unit test for namespace extract logic from unique name Signed-off-by: prateekpandey14 --- snapshot/pkg/volume/openebs/processor.go | 17 ++++++++- snapshot/pkg/volume/openebs/processor_test.go | 37 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 snapshot/pkg/volume/openebs/processor_test.go diff --git a/snapshot/pkg/volume/openebs/processor.go b/snapshot/pkg/volume/openebs/processor.go index 52c82ceb06b..c84df621132 100644 --- a/snapshot/pkg/volume/openebs/processor.go +++ b/snapshot/pkg/volume/openebs/processor.go @@ -19,6 +19,7 @@ package openebs import ( "fmt" "os" + "strings" "time" mApiv1 "github.com/kubernetes-incubator/external-storage/openebs/pkg/v1" @@ -211,11 +212,14 @@ func (h *openEBSPlugin) SnapshotRestore(snapshotData *crdv1.VolumeSnapshotData, // restore snapshot to a PV snapshotID := snapshotData.Spec.OpenEBSSnapshot.SnapshotID pvRefName := snapshotData.Spec.PersistentVolumeRef.Name - pvRefNamespace := snapshotData.Spec.PersistentVolumeRef.Namespace var oldvolume, newvolume mayav1.Volume var openebsVol mApiv1.OpenEBSVolume volumeSpec := mayav1.VolumeSpec{} + pvRefNamespace, _, err := GetNameAndNameSpaceFromSnapshotName(snapshotData.Spec.VolumeSnapshotRef.Name) + if err != nil { + return nil, nil, err + } // Get the source PV storage class name which will be passed // to maya-apiserver to extract volume policy while restoring snapshot as // new volume. @@ -349,3 +353,14 @@ func GetStorageClass(pvName string) (string, error) { glog.Infof("Source Volume is %#v", volume) return GetPersistentVolumeClass(volume), nil } + +// GetNameAndNameSpaceFromSnapshotName retrieves the namespace and +// the short name of a snapshot from its full name, for exmaple +// "test-ns/snap1" +func GetNameAndNameSpaceFromSnapshotName(name string) (string, string, error) { + strs := strings.Split(name, "/") + if len(strs) != 2 { + return "", "", fmt.Errorf("invalid snapshot name") + } + return strs[0], strs[1], nil +} diff --git a/snapshot/pkg/volume/openebs/processor_test.go b/snapshot/pkg/volume/openebs/processor_test.go new file mode 100644 index 00000000000..c11aa4272e9 --- /dev/null +++ b/snapshot/pkg/volume/openebs/processor_test.go @@ -0,0 +1,37 @@ +package openebs + +import ( + "fmt" + "reflect" + "testing" +) + +func TestGetNameAndNameSpaceFromSnapshoName(t *testing.T) { + cases := map[string]struct { + name string + expectErr error + expectNamespace string + expectSnapshotName string + }{ + + "SnapshotName with Namespace": {"percona/fastfurious", nil, "percona", "fastfurious"}, + "SnapshotName without Namespace": {"fastfurious", fmt.Errorf("invalid snapshot name"), "", ""}, + "Invalid Unique SnapshotName": {"k8s/percona/fastfurious", fmt.Errorf("invalid snapshot name"), "", ""}, + "Nil SnapshotName": {"", fmt.Errorf("invalid snapshot name"), "", ""}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + namespace, snapshotName, err := GetNameAndNameSpaceFromSnapshotName(tc.name) + + if !reflect.DeepEqual(err, tc.expectErr) { + t.Errorf("Expected %v, got %v", tc.expectErr, err) + } + if !reflect.DeepEqual(namespace, tc.expectNamespace) { + t.Errorf("Expected %v, got %v", tc.expectNamespace, namespace) + } + if !reflect.DeepEqual(snapshotName, tc.expectSnapshotName) { + t.Errorf("Expected %v, got %v", tc.expectSnapshotName, snapshotName) + } + }) + } +}