Skip to content

Commit

Permalink
feat(localPV): add Raw Block Volume support (openebs-archive#1536)
Browse files Browse the repository at this point in the history
- Added Builder Function for support & checking Block Device
- Returned true, for support of the Block Device
- Provided the path for the raw Block device, if provided
 - Added containerBuilder function (funcName: WithVolumeDevices Builder)
  - Added pvcBuilder function (funcname: WithVolumeMode Builder)
  - Added 2 BDD TestCases :
    - [+ve] Basic Raw Block Volume Support
    - [-ve] If PVC volumeMode is "Block", but mountPath is provided in Deployment 
      PodSpecTemplate

Signed-off-by: Rahul M Chheda <[email protected]>
  • Loading branch information
rahulchheda authored and shubham14bajpai committed Dec 27, 2019
1 parent d4269f3 commit 6a2e561
Show file tree
Hide file tree
Showing 7 changed files with 392 additions and 15 deletions.
15 changes: 12 additions & 3 deletions cmd/provisioner-localpv/app/helper_blockdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type HelperBlockDeviceOptions struct {
capacity string
// deviceType string
bdcName string
// volumeMode of PVC
volumeMode corev1.PersistentVolumeMode
}

// validate checks that the required fields to create BDC
Expand Down Expand Up @@ -98,7 +100,6 @@ func (p *Provisioner) createBlockDeviceClaim(blkDevOpts *HelperBlockDeviceOption
if err := blkDevOpts.validate(); err != nil {
return err
}

//Create a BDC for this PV (of type device). NDM will
//look for the device matching the capacity and node on which
//pod is being scheduled. Since this BDC is specific to a PV
Expand All @@ -125,6 +126,7 @@ func (p *Provisioner) createBlockDeviceClaim(blkDevOpts *HelperBlockDeviceOption
WithHostName(blkDevOpts.nodeHostname).
WithCapacity(blkDevOpts.capacity).
WithFinalizer(LocalPVFinalizer).
WithBlockVolumeMode(blkDevOpts.volumeMode).
Build()

if err != nil {
Expand Down Expand Up @@ -192,12 +194,19 @@ func (p *Provisioner) getBlockDevicePath(blkDevOpts *HelperBlockDeviceOptions) (
//If the error is about BDC being already present, then return nil
return "", "", errors.Errorf("unable to find BD:%v for BDC:%v associated with PV:%v", bdName, blkDevOpts.bdcName, blkDevOpts.name)
}

path := bd.Spec.FileSystem.Mountpoint

blkPath := bd.Spec.Path
if len(bd.Spec.DevLinks) > 0 {
//TODO : Iterate and get the first path by id.

blkPath = bd.Spec.DevLinks[0].Links[0]

//Iterate and get the first path by id.
for _, v := range bd.Spec.DevLinks {
if v.Kind == "by-id" {
blkPath = v.Links[0]
}
}
}

return path, blkPath, nil
Expand Down
7 changes: 5 additions & 2 deletions cmd/provisioner-localpv/app/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func NewProvisioner(stopCh chan struct{}, kubeClient *clientset.Clientset) (*Pro
}

// SupportsBlock will be used by controller to determine if block mode is
// supported by the host path provisioner. Return false.
// supported by the host path provisioner.
func (p *Provisioner) SupportsBlock() bool {
return false
return true
}

// Provision is invoked by the PVC controller which expect the PV
Expand Down Expand Up @@ -128,6 +128,9 @@ func (p *Provisioner) Provision(opts pvController.VolumeOptions) (*v1.Persistent
if stgType == "device" {
return p.ProvisionBlockDevice(opts, pvCASConfig)
}
if *opts.PVC.Spec.VolumeMode == v1.PersistentVolumeBlock && stgType != "device" {
return nil, fmt.Errorf("PV with BlockMode is not supported with StorageType %v", stgType)
}
alertlog.Logger.Errorw("",
"eventcode", "cstor.local.pv.provision.failure",
"msg", "Failed to provision CStor Local PV",
Expand Down
22 changes: 13 additions & 9 deletions cmd/provisioner-localpv/app/provisioner_blockdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (p *Provisioner) ProvisionBlockDevice(opts pvController.VolumeOptions, volu
nodeHostname: nodeHostname,
name: name,
capacity: capacity.String(),
volumeMode: *opts.PVC.Spec.VolumeMode,
}

path, blkPath, err := p.getBlockDevicePath(blkDevOpts)
Expand All @@ -59,16 +60,13 @@ func (p *Provisioner) ProvisionBlockDevice(opts pvController.VolumeOptions, volu
return nil, err
}
klog.Infof("Creating volume %v on %v at %v(%v)", name, nodeHostname, path, blkPath)

// Over-ride the path, with the blockPath, when path is empty.
if path == "" {
path = blkPath
klog.Infof("Using block device{%v} with fs{%v}", blkPath, fsType)
}

// TODO
// VolumeMode will always be specified as Filesystem for host path volume,
// and the value passed in from the PVC spec will be ignored.
fs := v1.PersistentVolumeFilesystem

// It is possible that the HostPath doesn't already exist on the node.
// Set the Local PV to create it.
//hostPathType := v1.HostPathDirectoryOrCreate
Expand All @@ -84,17 +82,23 @@ func (p *Provisioner) ProvisionBlockDevice(opts pvController.VolumeOptions, volu
//labels[string(v1alpha1.StorageClassKey)] = *className

//TODO Change the following to a builder pattern
pvObj, err := mPV.NewBuilder().
pvObjBuilder := mPV.NewBuilder().
WithName(name).
WithLabels(labels).
WithAnnotations(volAnnotations).
WithReclaimPolicy(opts.PersistentVolumeReclaimPolicy).
WithAccessModes(pvc.Spec.AccessModes).
WithVolumeMode(fs).
WithCapacityQty(pvc.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]).
WithLocalHostPathFormat(path, fsType).
WithNodeAffinity(nodeHostname).
Build()
WithNodeAffinity(nodeHostname)

// If volumeMode set to "Block", then provide the appropriate volumeMode, to pvObj
if *opts.PVC.Spec.VolumeMode == v1.PersistentVolumeBlock {
pvObjBuilder.WithVolumeMode(v1.PersistentVolumeBlock)
}

//Build the pvObject
pvObj, err := pvObjBuilder.Build()

if err != nil {
alertlog.Logger.Errorw("",
Expand Down
16 changes: 16 additions & 0 deletions pkg/blockdeviceclaim/v1alpha1/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,19 @@ func (b *Builder) WithFinalizer(finalizers ...string) *Builder {
b.BDC.Object.Finalizers = append(b.BDC.Object.Finalizers, finalizers...)
return b
}

// WithBlockVolumeMode sets the volumeMode as volumeModeBlock,
// if persistentVolumeMode is set to "Block"
func (b *Builder) WithBlockVolumeMode(mode corev1.PersistentVolumeMode) *Builder {
if len(mode) == 0 {
b.errs = append(
b.errs,
errors.New("failed to build BDC object: missing PersistentVolumeMode"),
)
}
if mode == corev1.PersistentVolumeBlock {
b.BDC.Object.Spec.Details.BlockVolumeMode = ndm.VolumeModeBlock
}

return b
}
22 changes: 22 additions & 0 deletions pkg/kubernetes/container/v1alpha1/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,28 @@ func (b *Builder) WithVolumeMountsNew(volumeMounts []corev1.VolumeMount) *Builde
return b
}

// WithVolumeDevices builds the containers with the appropriate volumeDevices
func (b *Builder) WithVolumeDevices(volumeDevices []corev1.VolumeDevice) *Builder {
if volumeDevices == nil {
b.errors = append(
b.errors,
errors.New("failed to build container object: nil volumedevices"),
)
return b
}
if len(volumeDevices) == 0 {
b.errors = append(
b.errors,
errors.New("failed to build container object: missing volumedevices"),
)
return b
}
newVolumeDevices := []corev1.VolumeDevice{}
newVolumeDevices = append(newVolumeDevices, volumeDevices...)
b.con.VolumeDevices = newVolumeDevices
return b
}

// WithImagePullPolicy sets the image pull policy of the container
func (b *Builder) WithImagePullPolicy(policy corev1.PullPolicy) *Builder {
if len(policy) == 0 {
Expand Down
7 changes: 7 additions & 0 deletions pkg/kubernetes/persistentvolumeclaim/v1alpha1/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ func (b *Builder) WithCapacity(capacity string) *Builder {
return b
}

// WithVolumeMode sets the volumeMode field in PVC with provided arguments
func (b *Builder) WithVolumeMode(vM corev1.PersistentVolumeMode) *Builder {

b.pvc.object.Spec.VolumeMode = &vM
return b
}

// Build returns the PVC API instance
func (b *Builder) Build() (*corev1.PersistentVolumeClaim, error) {
if len(b.errs) > 0 {
Expand Down
Loading

0 comments on commit 6a2e561

Please sign in to comment.