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

Commit

Permalink
Retrieves the namespace from its VolumeSnapshotRef name instead of 'P…
Browse files Browse the repository at this point in the history
…ersistentVolumeRef.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]>
  • Loading branch information
prateekpandey14 authored and kmova committed Jun 26, 2018
1 parent 87e79f8 commit c742c31
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
17 changes: 16 additions & 1 deletion snapshot/pkg/volume/openebs/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package openebs
import (
"fmt"
"os"
"strings"
"time"

mApiv1 "github.com/kubernetes-incubator/external-storage/openebs/pkg/v1"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
37 changes: 37 additions & 0 deletions snapshot/pkg/volume/openebs/processor_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit c742c31

Please sign in to comment.