From 846a129b7802f7af1fb540da34e895c1e1b392d7 Mon Sep 17 00:00:00 2001 From: Pavan Navarathna Devaraj Date: Tue, 13 Aug 2019 13:47:15 -0700 Subject: [PATCH 1/2] Add storage class to the PVC --- pkg/kube/volume/volume.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/kube/volume/volume.go b/pkg/kube/volume/volume.go index 411b77f94e..5bf800a8bd 100644 --- a/pkg/kube/volume/volume.go +++ b/pkg/kube/volume/volume.go @@ -84,10 +84,11 @@ func CreatePVC(ctx context.Context, kubeCli kubernetes.Interface, ns string, nam // PersistentVolumeClaim and any error that happened in the process. // // 'volumeName' is the name of the PVC that will be restored from the snapshot. +// 'storageClassName' is the name of the storage class used to create the PVC. // 'snapshotName' is the name of the VolumeSnapshot that will be used for restoring. // 'namespace' is the namespace of the VolumeSnapshot. The PVC will be restored to the same namepsace. // 'restoreSize' will override existing restore size from snapshot content if provided. -func CreatePVCFromSnapshot(ctx context.Context, kubeCli kubernetes.Interface, snapCli snapshotclient.Interface, namespace, volumeName, snapshotName string, restoreSize *int) (string, error) { +func CreatePVCFromSnapshot(ctx context.Context, kubeCli kubernetes.Interface, snapCli snapshotclient.Interface, namespace, volumeName, storageClassName, snapshotName string, restoreSize *int) (string, error) { snap, err := snapCli.VolumesnapshotV1alpha1().VolumeSnapshots(namespace).Get(snapshotName, metav1.GetOptions{}) if err != nil { return "", err @@ -123,6 +124,9 @@ func CreatePVCFromSnapshot(ctx context.Context, kubeCli kubernetes.Interface, sn } else { pvc.ObjectMeta.GenerateName = pvcGenerateName } + if storageClassName != "" { + pvc.Spec.StorageClassName = &storageClassName + } pvc, err = kubeCli.CoreV1().PersistentVolumeClaims(namespace).Create(pvc) if err != nil { From 497c168c0edd4ac56680fd5e07db692ee46846c2 Mon Sep 17 00:00:00 2001 From: Pavan Navarathna Devaraj Date: Tue, 13 Aug 2019 14:53:32 -0700 Subject: [PATCH 2/2] Fix compilation error --- pkg/kube/snapshot/snapshot_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/kube/snapshot/snapshot_test.go b/pkg/kube/snapshot/snapshot_test.go index 0a24343a19..631a5d1239 100644 --- a/pkg/kube/snapshot/snapshot_test.go +++ b/pkg/kube/snapshot/snapshot_test.go @@ -253,7 +253,7 @@ func (s *SnapshotTestSuite) TestVolumeSnapshot(c *C) { err = Clone(ctx, s.snapCli, snapshotName, s.sourceNamespace, snapshotCloneName, s.targetNamespace, wait) c.Assert(err, IsNil) - _, err = volume.CreatePVCFromSnapshot(ctx, s.cli, s.snapCli, s.targetNamespace, volumeCloneName, snapshotCloneName, nil) + _, err = volume.CreatePVCFromSnapshot(ctx, s.cli, s.snapCli, s.targetNamespace, volumeCloneName, "", snapshotCloneName, nil) c.Assert(err, IsNil) poll.Wait(ctx, func(ctx context.Context) (bool, error) { pvc, err = s.cli.CoreV1().PersistentVolumeClaims(s.targetNamespace).Get(volumeCloneName, metav1.GetOptions{}) @@ -266,7 +266,7 @@ func (s *SnapshotTestSuite) TestVolumeSnapshot(c *C) { // Try with a greater restore size. sizeNew := 2 volumeCloneName += "-2" - _, err = volume.CreatePVCFromSnapshot(ctx, s.cli, s.snapCli, s.targetNamespace, volumeCloneName, snapshotCloneName, &sizeNew) + _, err = volume.CreatePVCFromSnapshot(ctx, s.cli, s.snapCli, s.targetNamespace, volumeCloneName, "", snapshotCloneName, &sizeNew) c.Assert(err, IsNil) poll.Wait(ctx, func(ctx context.Context) (bool, error) { pvc, err = s.cli.CoreV1().PersistentVolumeClaims(s.targetNamespace).Get(volumeCloneName, metav1.GetOptions{})