Skip to content

Commit

Permalink
Fix non-nil restore size problem in CreatePVCFromSnapshot (#5210)
Browse files Browse the repository at this point in the history
This PR will fix the problem with non-nil restore size argument in the function.

* Fix missing dereferencing in CreatePVCFromSnapshot
* Add a test case for the problem
* Fix error checking
* Reduce volume size
  • Loading branch information
Hakan Memisoglu authored and Ilya Kislenko committed Mar 15, 2019
1 parent 0dd7972 commit 2a9ac93
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions pkg/kube/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *SnapshotTestSuite) SetUpSuite(c *C) {
s.snapCli = sc

vscs, err := sc.VolumesnapshotV1alpha1().VolumeSnapshotClasses().List(metav1.ListOptions{})
if !k8errors.IsNotFound(err) {
if err != nil && !k8errors.IsNotFound(err) {
c.Logf("Failed to query VolumeSnapshotClass, skipping test. Error: %v", err)
c.Fail()
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (s *SnapshotTestSuite) TestVolumeSnapshot(c *C) {
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()

size, err := resource.ParseQuantity("200Gi")
size, err := resource.ParseQuantity("1Gi")
c.Assert(err, IsNil)

pvc := &corev1.PersistentVolumeClaim{
Expand Down Expand Up @@ -253,6 +253,19 @@ func (s *SnapshotTestSuite) TestVolumeSnapshot(c *C) {
return pvc.Status.Phase == corev1.ClaimBound, nil
})

// Try with a greater restore size.
sizeNew := 2
volumeCloneName += "-2"
_, 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{})
if err != nil {
return false, err
}
return pvc.Status.Phase == corev1.ClaimBound, nil
})

err = Delete(ctx, s.snapCli, snap.Name, snap.Namespace)
c.Assert(err, IsNil)

Expand Down
2 changes: 1 addition & 1 deletion pkg/kube/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func CreatePVCFromSnapshot(ctx context.Context, kubeCli kubernetes.Interface, sn
}
size := snap.Status.RestoreSize
if restoreSize != nil {
s := resource.MustParse(fmt.Sprintf("%dGi", restoreSize))
s := resource.MustParse(fmt.Sprintf("%dGi", *restoreSize))
size = &s
}
if size == nil {
Expand Down

0 comments on commit 2a9ac93

Please sign in to comment.