Skip to content

Add Block volume support for CSI provisioner #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
100 changes: 63 additions & 37 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

[[constraint]]
name = "k8s.io/client-go"
version = "v6.0.0"
version = "v7.0.0"

[[constraint]]
name = "k8s.io/api"
Expand Down
66 changes: 56 additions & 10 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/golang/glog"

"github.com/kubernetes-incubator/external-storage/lib/controller"
"github.com/kubernetes-incubator/external-storage/lib/util"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -89,9 +90,20 @@ var (
accessMode = &csi.VolumeCapability_AccessMode{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
}
accessType = &csi.VolumeCapability_Mount{
accessTypeMount = &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{},
}
accessTypeBlock = &csi.VolumeCapability_Block{
Block: &csi.VolumeCapability_BlockVolume{},
}
volumeCapabilityMountSingle = &csi.VolumeCapability{
AccessType: accessTypeMount,
AccessMode: accessMode,
}
volumeCapabilityBlockSingle = &csi.VolumeCapability{
AccessType: accessTypeBlock,
AccessMode: accessMode,
}
// Each provisioner have a identify string to distinguish with others. This
// identify string will be added in PV annoations under this key.
provisionerIDKey = "storage.kubernetes.io/csiProvisionerIdentity"
Expand Down Expand Up @@ -295,21 +307,27 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
capacity := options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
volSizeBytes := capacity.Value()

volumeCapabilities := []*csi.VolumeCapability{
volumeCapabilityMountSingle,
}

if util.CheckPersistentVolumeClaimModeBlock(options.PVC) {
volumeCapabilities = []*csi.VolumeCapability{
volumeCapabilityBlockSingle,
}
}

// Create a CSI CreateVolumeRequest and Response
req := csi.CreateVolumeRequest{

Name: pvName,
Parameters: options.Parameters,
VolumeCapabilities: []*csi.VolumeCapability{
{
AccessType: accessType,
AccessMode: accessMode,
},
},
Name: pvName,
Parameters: options.Parameters,
VolumeCapabilities: volumeCapabilities,
CapacityRange: &csi.CapacityRange{
RequiredBytes: int64(volSizeBytes),
},
}

rep := &csi.CreateVolumeResponse{}

// Resolve provision secret credentials.
Expand Down Expand Up @@ -411,7 +429,6 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
CSI: &v1.CSIPersistentVolumeSource{
Driver: driverName,
VolumeHandle: p.volumeIdToHandle(rep.Volume.Id),
FSType: fsType,
VolumeAttributes: volumeAttributes,
ControllerPublishSecretRef: controllerPublishSecretRef,
NodeStageSecretRef: nodeStageSecretRef,
Expand All @@ -421,6 +438,16 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
},
}

// Set VolumeMode to PV if it is passed via PVC spec when Block feature is enabled
if options.PVC.Spec.VolumeMode != nil {
pv.Spec.VolumeMode = options.PVC.Spec.VolumeMode
}

// Set FSType if PV is not Block Volume
if !util.CheckPersistentVolumeClaimModeBlock(options.PVC) {
pv.Spec.PersistentVolumeSource.CSI.FSType = fsType
}

glog.Infof("successfully created PV %+v", pv.Spec.PersistentVolumeSource)

return pv, nil
Expand Down Expand Up @@ -466,6 +493,25 @@ func (p *csiProvisioner) Delete(volume *v1.PersistentVolume) error {
return err
}

func (p *csiProvisioner) SupportsBlock() bool {
ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()

client := csi.NewControllerClient(p.grpcClient)
req := csi.ValidateVolumeCapabilitiesRequest{
VolumeCapabilities: []*csi.VolumeCapability{
volumeCapabilityBlockSingle,
},
}

rsp, err := client.ValidateVolumeCapabilities(ctx, &req)
if err != nil {
return false
}

return rsp.Supported
}

//TODO use a unique volume handle from and to Id
func (p *csiProvisioner) volumeIdToHandle(id string) string {
return id
Expand Down
Loading