Skip to content

Commit 9a36e5e

Browse files
committed
Addressed review comments
1 parent 8361874 commit 9a36e5e

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

cmd/csi-provisioner/csi-provisioner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func init() {
8787
if err != nil {
8888
glog.Fatalf("Failed to create client: %v", err)
8989
}
90+
// snapclientset.NewForConfig creates a new Clientset for VolumesnapshotV1alpha1Client
9091
snapClient, err := snapclientset.NewForConfig(config)
9192
if err != nil {
9293
glog.Fatalf("Failed to create snapshot client: %v", err)

pkg/controller/controller.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
343343
return nil, fmt.Errorf("claim Selector is not supported")
344344
}
345345

346-
var needSnapshotSupport bool
346+
var needSnapshotSupport bool = false
347347
if options.PVC.Spec.DataSource != nil {
348348
// PVC.Spec.DataSource.Name is the name of the VolumeSnapshot API object
349349
if options.PVC.Spec.DataSource.Name == "" {
@@ -394,7 +394,7 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
394394
req.VolumeContentSource = volumeContentSource
395395
}
396396

397-
glog.Infof("CreateVolumeRequest %+v", req)
397+
glog.V(5).Infof("CreateVolumeRequest %+v", req)
398398

399399
rep := &csi.CreateVolumeResponse{}
400400

@@ -529,6 +529,10 @@ func (p *csiProvisioner) getVolumeContentSource(options controller.VolumeOptions
529529
}
530530
glog.V(5).Infof("VolumeSnapshotContent %+v", snapContentObj)
531531

532+
if snapContentObj.Spec.VolumeSnapshotSource.CSI == nil || len(snapContentObj.Spec.VolumeSnapshotSource.CSI.SnapshotHandle) == 0 {
533+
return nil, fmt.Errorf("error getting snapshot handle from snapshot:snapshotcontent %s:%s", snapshotObj.Name, snapshotObj.Spec.SnapshotContentName)
534+
}
535+
532536
snapshotSource := csi.VolumeContentSource_Snapshot{
533537
Snapshot: &csi.VolumeContentSource_SnapshotSource{
534538
Id: snapContentObj.Spec.VolumeSnapshotSource.CSI.SnapshotHandle,
@@ -537,7 +541,10 @@ func (p *csiProvisioner) getVolumeContentSource(options controller.VolumeOptions
537541
glog.V(5).Infof("VolumeContentSource_Snapshot %+v", snapshotSource)
538542

539543
if snapshotObj.Status.RestoreSize != nil {
540-
capacity := options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
544+
capacity, exists := options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
545+
if !exists {
546+
return nil, fmt.Errorf("error getting capacity for PVC %s when creating snapshot %s", options.PVC.Name, snapshotObj.Name)
547+
}
541548
volSizeBytes := capacity.Value()
542549
glog.V(5).Infof("Requested volume size is %d and snapshot size is %d for the source snapshot %s", int64(volSizeBytes), int64(snapshotObj.Status.RestoreSize.Value()), snapshotObj.Name)
543550
// When restoring volume from a snapshot, the volume size should

pkg/controller/controller_test.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@ func provisionMockServerSetupExpectations(identityServer *driver.MockIdentitySer
620620
}, nil).Times(1)
621621
}
622622

623+
// provisionFromSnapshotMockServerSetupExpectations mocks plugin and controller capabilities reported
624+
// by a CSI plugin that supports the snapshot feature
623625
func provisionFromSnapshotMockServerSetupExpectations(identityServer *driver.MockIdentityServer, controllerServer *driver.MockControllerServer) {
624626
identityServer.EXPECT().GetPluginCapabilities(gomock.Any(), gomock.Any()).Return(&csi.GetPluginCapabilitiesResponse{
625627
Capabilities: []*csi.PluginCapability{
@@ -1166,7 +1168,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
11661168
volOpts controller.VolumeOptions
11671169
restoredVolSizeSmall bool
11681170
wrongDataSource bool
1169-
validSnapshotStatus bool
1171+
snapshotStatusReady bool
11701172
expectedPVSpec *pvSpec
11711173
expectErr bool
11721174
}{
@@ -1195,7 +1197,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
11951197
},
11961198
Parameters: map[string]string{},
11971199
},
1198-
validSnapshotStatus: true,
1200+
snapshotStatusReady: true,
11991201
expectedPVSpec: &pvSpec{
12001202
Name: "test-testi",
12011203
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
@@ -1238,7 +1240,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
12381240
Parameters: map[string]string{},
12391241
},
12401242
restoredVolSizeSmall: true,
1241-
validSnapshotStatus: true,
1243+
snapshotStatusReady: true,
12421244
expectErr: true,
12431245
},
12441246
"fail empty snapshot name": {
@@ -1346,7 +1348,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
13461348
},
13471349
Parameters: map[string]string{},
13481350
},
1349-
validSnapshotStatus: false,
1351+
snapshotStatusReady: false,
13501352
expectErr: true,
13511353
},
13521354
}
@@ -1364,7 +1366,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
13641366
client := &fake.Clientset{}
13651367

13661368
client.AddReactor("get", "volumesnapshots", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) {
1367-
snap := newSnapshot(snapName, snapClassName, "snapcontent-snapuid", "snapuid", "claim", tc.validSnapshotStatus, nil, metaTimeNowUnix, resource.NewQuantity(requestedBytes, resource.BinarySI))
1369+
snap := newSnapshot(snapName, snapClassName, "snapcontent-snapuid", "snapuid", "claim", tc.snapshotStatusReady, nil, metaTimeNowUnix, resource.NewQuantity(requestedBytes, resource.BinarySI))
13681370
return true, snap, nil
13691371
})
13701372

@@ -1382,11 +1384,19 @@ func TestProvisionFromSnapshot(t *testing.T) {
13821384
},
13831385
}
13841386

1385-
// Setup mock call expectations.
1387+
// Setup mock call expectations. If tc.wrongDataSource is false, DataSource is valid
1388+
// and the controller will proceed to check whether the plugin supports snapshot.
1389+
// So in this case, we need the plugin to report snapshot support capabilities;
1390+
// Otherwise, the controller will fail the operation so it won't check the capabilities.
13861391
if tc.wrongDataSource == false {
13871392
provisionFromSnapshotMockServerSetupExpectations(identityServer, controllerServer)
13881393
}
1389-
if tc.restoredVolSizeSmall == false && tc.wrongDataSource == false && tc.validSnapshotStatus {
1394+
// If tc.restoredVolSizeSmall is true, or tc.wrongDataSource is true, or
1395+
// tc.snapshotStatusReady is false, create volume from snapshot operation will fail
1396+
// early and therefore CreateVolume is not expected to be called.
1397+
// When the following if condition is met, it is a valid create volume from snapshot
1398+
// operation and CreateVolume is expected to be called.
1399+
if tc.restoredVolSizeSmall == false && tc.wrongDataSource == false && tc.snapshotStatusReady {
13901400
controllerServer.EXPECT().CreateVolume(gomock.Any(), gomock.Any()).Return(out, nil).Times(1)
13911401
}
13921402

0 commit comments

Comments
 (0)