From e441af7df2fbacbf83d2b9de92a67ff24f1e667d Mon Sep 17 00:00:00 2001 From: Cheng Pan Date: Mon, 15 Oct 2018 10:16:25 -0700 Subject: [PATCH] Implement support for storage class parameter - fsType Fixes: #62 --- pkg/cloud/cloud.go | 1 + pkg/driver/controller.go | 5 +++++ pkg/driver/controller_test.go | 22 ++++++++++++++++++---- pkg/driver/node.go | 13 ++++++++++++- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/pkg/cloud/cloud.go b/pkg/cloud/cloud.go index 08885a8bac..09a9026ff3 100644 --- a/pkg/cloud/cloud.go +++ b/pkg/cloud/cloud.go @@ -87,6 +87,7 @@ type Disk struct { VolumeID string CapacityGiB int64 AvailabilityZone string + FsType string } // DiskOptions represents parameters to create an EBS volume diff --git a/pkg/driver/controller.go b/pkg/driver/controller.go index 22c059aff8..24262386d9 100644 --- a/pkg/driver/controller.go +++ b/pkg/driver/controller.go @@ -84,6 +84,8 @@ func (d *Driver) CreateVolume(ctx context.Context, req *csi.CreateVolumeRequest) if err != nil { return nil, status.Errorf(codes.Internal, "Could not create volume %q: %v", volName, err) } + fsType := req.GetParameters()["fsType"] + disk.FsType = fsType return newCreateVolumeResponse(disk), nil } @@ -279,6 +281,9 @@ func newCreateVolumeResponse(disk *cloud.Disk) *csi.CreateVolumeResponse { Volume: &csi.Volume{ Id: disk.VolumeID, CapacityBytes: util.GiBToBytes(disk.CapacityGiB), + Attributes: map[string]string{ + "fsType": disk.FsType, + }, AccessibleTopology: []*csi.Topology{ &csi.Topology{ Segments: map[string]string{topologyKey: disk.AvailabilityZone}, diff --git a/pkg/driver/controller_test.go b/pkg/driver/controller_test.go index 28480dedae..a43c38ed77 100644 --- a/pkg/driver/controller_test.go +++ b/pkg/driver/controller_test.go @@ -59,7 +59,7 @@ func TestCreateVolume(t *testing.T) { expVol: &csi.Volume{ CapacityBytes: stdVolSize, Id: "vol-test", - Attributes: nil, + Attributes: map[string]string{"fsType": ""}, }, }, { @@ -89,7 +89,7 @@ func TestCreateVolume(t *testing.T) { expVol: &csi.Volume{ CapacityBytes: stdVolSize, Id: "vol-test", - Attributes: nil, + Attributes: map[string]string{"fsType": ""}, }, }, { @@ -118,7 +118,7 @@ func TestCreateVolume(t *testing.T) { expVol: &csi.Volume{ CapacityBytes: cloud.DefaultVolumeSize, Id: "vol-test", - Attributes: nil, + Attributes: map[string]string{"fsType": ""}, }, }, { @@ -132,7 +132,21 @@ func TestCreateVolume(t *testing.T) { expVol: &csi.Volume{ CapacityBytes: 2147483648, // 1 GiB + 1 byte = 2 GiB Id: "vol-test", - Attributes: nil, + Attributes: map[string]string{"fsType": ""}, + }, + }, + { + name: "success with fstype parameter", + req: &csi.CreateVolumeRequest{ + Name: "vol-test", + CapacityRange: stdCapRange, + VolumeCapabilities: stdVolCap, + Parameters: map[string]string{"fsType": defaultFsType}, + }, + expVol: &csi.Volume{ + CapacityBytes: stdVolSize, + Id: "vol-test", + Attributes: map[string]string{"fsType": defaultFsType}, }, }, } diff --git a/pkg/driver/node.go b/pkg/driver/node.go index c02eef3920..55baebb85a 100644 --- a/pkg/driver/node.go +++ b/pkg/driver/node.go @@ -27,6 +27,11 @@ import ( "google.golang.org/grpc/status" ) +const ( + // default file system type to be used when it is not provided + defaultFsType = "ext4" +) + func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) { glog.V(4).Infof("NodeStageVolume: called with args %#v", req) volumeID := req.GetVolumeId() @@ -72,10 +77,16 @@ func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRe msg := fmt.Sprintf("target %q is not a valid mount point", target) return nil, status.Error(codes.InvalidArgument, msg) } + // Get fs type that the volume will be formatted with + attributes := req.GetVolumeAttributes() + fsType, exists := attributes["fsType"] + if !exists || fsType == "" { + fsType = defaultFsType + } // FormatAndMount will format only if needed glog.V(5).Infof("NodeStageVolume: formatting %s and mounting at %s", source, target) - err = d.mounter.FormatAndMount(source, target, "ext4", nil) + err = d.mounter.FormatAndMount(source, target, fsType, nil) if err != nil { msg := fmt.Sprintf("could not format %q and mount it at %q", source, target) return nil, status.Error(codes.Internal, msg)