Skip to content

Commit

Permalink
Propagate VolumeSnapshotClass DeletionPolicy into static VolumeSnapsh…
Browse files Browse the repository at this point in the history
…otContent (#232)

* Propagate VSC.Deletion policy

* Debug log

* Refactor

* Fix UT

* Add additional check in the unit test
  • Loading branch information
pavannd1 committed Aug 27, 2019
1 parent d1cbd00 commit 67b40f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
15 changes: 14 additions & 1 deletion pkg/kube/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func GetSource(ctx context.Context, snapCli snapshotclient.Interface, snapshotNa
// 'namespace' is the namespace of the snapshot.
// 'waitForReady' blocks the caller until snapshot is ready to use or context is cancelled.
func CreateFromSource(ctx context.Context, snapCli snapshotclient.Interface, source *Source, snapshotName, namespace string, waitForReady bool) error {
deletionPolicy, err := getDeletionPolicyFromClass(snapCli, *source.VolumeSnapshotClassName)
if err != nil {
return errors.Wrap(err, "Failed to get DeletionPolicy from VolumeSnapshotClass")
}
contentName := snapshotName + "-content-" + string(uuid.NewUUID())
content := &snapshot.VolumeSnapshotContent{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -187,6 +191,7 @@ func CreateFromSource(ctx context.Context, snapCli snapshotclient.Interface, sou
Name: snapshotName,
},
VolumeSnapshotClassName: source.VolumeSnapshotClassName,
DeletionPolicy: &deletionPolicy,
},
}
snap := &snapshot.VolumeSnapshot{
Expand All @@ -199,7 +204,7 @@ func CreateFromSource(ctx context.Context, snapCli snapshotclient.Interface, sou
},
}

content, err := snapCli.VolumesnapshotV1alpha1().VolumeSnapshotContents().Create(content)
content, err = snapCli.VolumesnapshotV1alpha1().VolumeSnapshotContents().Create(content)
if err != nil {
return errors.Errorf("Failed to create content, VolumesnapshotContent: %s, Error: %v", content.Name, err)
}
Expand Down Expand Up @@ -233,3 +238,11 @@ func WaitOnReadyToUse(ctx context.Context, snapCli snapshotclient.Interface, sna
func getContent(ctx context.Context, snapCli snapshotclient.Interface, contentName string) (*snapshot.VolumeSnapshotContent, error) {
return snapCli.VolumesnapshotV1alpha1().VolumeSnapshotContents().Get(contentName, metav1.GetOptions{})
}

func getDeletionPolicyFromClass(snapCli snapshotclient.Interface, snapClassName string) (snapshot.DeletionPolicy, error) {
vsc, err := snapCli.VolumesnapshotV1alpha1().VolumeSnapshotClasses().Get(snapClassName, metav1.GetOptions{})
if err != nil {
return "", errors.Wrapf(err, "Failed to find VolumeSnapshotClass: %s", snapClassName)
}
return *vsc.DeletionPolicy, nil
}
16 changes: 14 additions & 2 deletions pkg/kube/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ func (s *SnapshotTestSuite) TestVolumeSnapshotCloneFake(c *C) {
fakeSnapshotName := "snap-1-fake"
fakeContentName := "snapcontent-1-fake"

dp := snapshot.VolumeSnapshotContentDelete
vsc := &snapshot.VolumeSnapshotClass{
ObjectMeta: metav1.ObjectMeta{
Name: fakeClass,
},
Snapshotter: fakeDriver,
DeletionPolicy: &dp,
}

content := &snapshot.VolumeSnapshotContent{
ObjectMeta: metav1.ObjectMeta{
Name: fakeContentName,
Expand All @@ -172,7 +181,7 @@ func (s *SnapshotTestSuite) TestVolumeSnapshotCloneFake(c *C) {
},
}
ctime := metav1.Now()
snapshot := &snapshot.VolumeSnapshot{
snap := &snapshot.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: fakeSnapshotName,
Namespace: defaultNamespace,
Expand All @@ -191,7 +200,9 @@ func (s *SnapshotTestSuite) TestVolumeSnapshotCloneFake(c *C) {
fakeTargetNamespace := "new-ns"
fakeClone := "clone-1"

_, err := snapCli.VolumesnapshotV1alpha1().VolumeSnapshots(defaultNamespace).Create(snapshot)
_, err := snapCli.VolumesnapshotV1alpha1().VolumeSnapshotClasses().Create(vsc)
c.Assert(err, IsNil)
_, err = snapCli.VolumesnapshotV1alpha1().VolumeSnapshots(defaultNamespace).Create(snap)
c.Assert(err, IsNil)
_, err = snapCli.VolumesnapshotV1alpha1().VolumeSnapshotContents().Create(content)
c.Assert(err, IsNil)
Expand All @@ -208,6 +219,7 @@ func (s *SnapshotTestSuite) TestVolumeSnapshotCloneFake(c *C) {
cloneContent, err := snapCli.VolumesnapshotV1alpha1().VolumeSnapshotContents().Get(clone.Spec.SnapshotContentName, metav1.GetOptions{})
c.Assert(err, IsNil)
c.Assert(strings.HasPrefix(cloneContent.Name, fakeClone), Equals, true)
c.Assert(*cloneContent.Spec.DeletionPolicy, Equals, *vsc.DeletionPolicy)
}

func (s *SnapshotTestSuite) TestVolumeSnapshot(c *C) {
Expand Down

0 comments on commit 67b40f4

Please sign in to comment.