Skip to content

Commit 0847c9f

Browse files
committed
[WIP] Add Block volume support for CSI provisioner
In CSI provisioner, below three logics need to be implemented, to add Block volume support to CSI provisioner: 1. Add SupportsBlock that properly returns whether Storage Provider's plugin supports block (this is checked by using ValidateVolumeCapabilities), 2. Pass BlockVolume instead of MountVolume to CreateVolume if volumeMode is set to be Block on Provision, 3. Set volumeMode to PV returned by Provision. vendor/github.com/kubernetes-incubator/external-storage/* should be updated from upstream, but manually changed for testing purpose, in this commit. Fixes kubernetes-csi#110
1 parent a0bc91b commit 0847c9f

File tree

4 files changed

+93
-9
lines changed

4 files changed

+93
-9
lines changed

pkg/controller/controller.go

+50-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/golang/glog"
3232

3333
"github.com/kubernetes-incubator/external-storage/lib/controller"
34+
"github.com/kubernetes-incubator/external-storage/lib/util"
3435

3536
"k8s.io/api/core/v1"
3637
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -89,9 +90,20 @@ var (
8990
accessMode = &csi.VolumeCapability_AccessMode{
9091
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
9192
}
92-
accessType = &csi.VolumeCapability_Mount{
93+
accessTypeMount = &csi.VolumeCapability_Mount{
9394
Mount: &csi.VolumeCapability_MountVolume{},
9495
}
96+
accessTypeBlock = &csi.VolumeCapability_Block{
97+
Block: &csi.VolumeCapability_BlockVolume{},
98+
}
99+
volumeCapabilityMountSingle = &csi.VolumeCapability{
100+
AccessType: accessTypeMount,
101+
AccessMode: accessMode,
102+
}
103+
volumeCapabilityBlockSingle = &csi.VolumeCapability{
104+
AccessType: accessTypeBlock,
105+
AccessMode: accessMode,
106+
}
95107
// Each provisioner have a identify string to distinguish with others. This
96108
// identify string will be added in PV annoations under this key.
97109
provisionerIDKey = "storage.kubernetes.io/csiProvisionerIdentity"
@@ -295,21 +307,27 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
295307
capacity := options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
296308
volSizeBytes := capacity.Value()
297309

310+
volumeCapabilities := []*csi.VolumeCapability{
311+
volumeCapabilityMountSingle,
312+
}
313+
314+
if util.CheckPersistentVolumeClaimModeBlock(options.PVC) {
315+
volumeCapabilities = []*csi.VolumeCapability{
316+
volumeCapabilityBlockSingle,
317+
}
318+
}
319+
298320
// Create a CSI CreateVolumeRequest and Response
299321
req := csi.CreateVolumeRequest{
300322

301-
Name: pvName,
302-
Parameters: options.Parameters,
303-
VolumeCapabilities: []*csi.VolumeCapability{
304-
{
305-
AccessType: accessType,
306-
AccessMode: accessMode,
307-
},
308-
},
323+
Name: pvName,
324+
Parameters: options.Parameters,
325+
VolumeCapabilities: volumeCapabilities,
309326
CapacityRange: &csi.CapacityRange{
310327
RequiredBytes: int64(volSizeBytes),
311328
},
312329
}
330+
313331
rep := &csi.CreateVolumeResponse{}
314332

315333
// Resolve provision secret credentials.
@@ -421,6 +439,10 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
421439
},
422440
}
423441

442+
if options.PVC.Spec.VolumeMode != nil {
443+
pv.Spec.VolumeMode = options.PVC.Spec.VolumeMode
444+
}
445+
424446
glog.Infof("successfully created PV %+v", pv.Spec.PersistentVolumeSource)
425447

426448
return pv, nil
@@ -466,6 +488,25 @@ func (p *csiProvisioner) Delete(volume *v1.PersistentVolume) error {
466488
return err
467489
}
468490

491+
func (p *csiProvisioner) SupportsBlock() bool {
492+
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
493+
defer cancel()
494+
495+
client := csi.NewControllerClient(p.grpcClient)
496+
req := csi.ValidateVolumeCapabilitiesRequest{
497+
VolumeCapabilities: []*csi.VolumeCapability{
498+
volumeCapabilityBlockSingle,
499+
},
500+
}
501+
502+
rsp, err := client.ValidateVolumeCapabilities(ctx, &req)
503+
if err != nil {
504+
return false
505+
}
506+
507+
return rsp.Supported
508+
}
509+
469510
//TODO use a unique volume handle from and to Id
470511
func (p *csiProvisioner) volumeIdToHandle(id string) string {
471512
return id

vendor/github.com/kubernetes-incubator/external-storage/lib/controller/controller.go

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/kubernetes-incubator/external-storage/lib/controller/volume.go

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/kubernetes-incubator/external-storage/lib/util/util.go

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)