Skip to content
This repository was archived by the owner on Aug 14, 2021. It is now read-only.

Commit c742c31

Browse files
prateekpandey14Kiran Mova
authored andcommitted
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 <[email protected]>
1 parent 87e79f8 commit c742c31

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

snapshot/pkg/volume/openebs/processor.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package openebs
1919
import (
2020
"fmt"
2121
"os"
22+
"strings"
2223
"time"
2324

2425
mApiv1 "github.com/kubernetes-incubator/external-storage/openebs/pkg/v1"
@@ -211,11 +212,14 @@ func (h *openEBSPlugin) SnapshotRestore(snapshotData *crdv1.VolumeSnapshotData,
211212
// restore snapshot to a PV
212213
snapshotID := snapshotData.Spec.OpenEBSSnapshot.SnapshotID
213214
pvRefName := snapshotData.Spec.PersistentVolumeRef.Name
214-
pvRefNamespace := snapshotData.Spec.PersistentVolumeRef.Namespace
215215
var oldvolume, newvolume mayav1.Volume
216216
var openebsVol mApiv1.OpenEBSVolume
217217
volumeSpec := mayav1.VolumeSpec{}
218218

219+
pvRefNamespace, _, err := GetNameAndNameSpaceFromSnapshotName(snapshotData.Spec.VolumeSnapshotRef.Name)
220+
if err != nil {
221+
return nil, nil, err
222+
}
219223
// Get the source PV storage class name which will be passed
220224
// to maya-apiserver to extract volume policy while restoring snapshot as
221225
// new volume.
@@ -349,3 +353,14 @@ func GetStorageClass(pvName string) (string, error) {
349353
glog.Infof("Source Volume is %#v", volume)
350354
return GetPersistentVolumeClass(volume), nil
351355
}
356+
357+
// GetNameAndNameSpaceFromSnapshotName retrieves the namespace and
358+
// the short name of a snapshot from its full name, for exmaple
359+
// "test-ns/snap1"
360+
func GetNameAndNameSpaceFromSnapshotName(name string) (string, string, error) {
361+
strs := strings.Split(name, "/")
362+
if len(strs) != 2 {
363+
return "", "", fmt.Errorf("invalid snapshot name")
364+
}
365+
return strs[0], strs[1], nil
366+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package openebs
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestGetNameAndNameSpaceFromSnapshoName(t *testing.T) {
10+
cases := map[string]struct {
11+
name string
12+
expectErr error
13+
expectNamespace string
14+
expectSnapshotName string
15+
}{
16+
17+
"SnapshotName with Namespace": {"percona/fastfurious", nil, "percona", "fastfurious"},
18+
"SnapshotName without Namespace": {"fastfurious", fmt.Errorf("invalid snapshot name"), "", ""},
19+
"Invalid Unique SnapshotName": {"k8s/percona/fastfurious", fmt.Errorf("invalid snapshot name"), "", ""},
20+
"Nil SnapshotName": {"", fmt.Errorf("invalid snapshot name"), "", ""},
21+
}
22+
for name, tc := range cases {
23+
t.Run(name, func(t *testing.T) {
24+
namespace, snapshotName, err := GetNameAndNameSpaceFromSnapshotName(tc.name)
25+
26+
if !reflect.DeepEqual(err, tc.expectErr) {
27+
t.Errorf("Expected %v, got %v", tc.expectErr, err)
28+
}
29+
if !reflect.DeepEqual(namespace, tc.expectNamespace) {
30+
t.Errorf("Expected %v, got %v", tc.expectNamespace, namespace)
31+
}
32+
if !reflect.DeepEqual(snapshotName, tc.expectSnapshotName) {
33+
t.Errorf("Expected %v, got %v", tc.expectSnapshotName, snapshotName)
34+
}
35+
})
36+
}
37+
}

0 commit comments

Comments
 (0)