Skip to content
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

Add support for 'path' field in volumeContext #52

Merged
merged 2 commits into from
Jul 25, 2019
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/kubernetes-sigs/aws-efs-csi-driver

require (
github.com/aws/aws-sdk-go v1.16.5
github.com/container-storage-interface/spec v0.3.0
github.com/container-storage-interface/spec v1.1.0
github.com/golang/mock v1.2.0
github.com/spf13/afero v1.2.1 // indirect
github.com/stretchr/testify v1.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/aws/aws-sdk-go v1.16.5/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/container-storage-interface/spec v0.3.0 h1:ALxSqFjptj8R5rL+cdyAbwbaLHHXDL5pmp1qIh1b+38=
github.com/container-storage-interface/spec v0.3.0/go.mod h1:6URME8mwIBbpVyZV93Ce5St17xBiQJQY67NDsuohiy4=
github.com/container-storage-interface/spec v1.1.0 h1:qPsTqtR1VUPvMPeK0UnCZMtXaKGyyLPG8gj/wG6VqMs=
github.com/container-storage-interface/spec v1.1.0/go.mod h1:6URME8mwIBbpVyZV93Ce5St17xBiQJQY67NDsuohiy4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand Down
13 changes: 1 addition & 12 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"
"net"

csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/cloud"
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/util"
"google.golang.org/grpc"
Expand All @@ -32,17 +32,6 @@ const (
driverName = "efs.csi.aws.com"
)

var (
volumeCaps = []csi.VolumeCapability_AccessMode{
{
Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
},
{
Mode: csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
},
}
)

type Driver struct {
endpoint string
nodeID string
Expand Down
2 changes: 1 addition & 1 deletion pkg/driver/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package driver
import (
"context"

csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/container-storage-interface/spec/lib/go/csi"
)

func (d *Driver) GetPluginInfo(ctx context.Context, req *csi.GetPluginInfoRequest) (*csi.GetPluginInfoResponse, error) {
Expand Down
50 changes: 36 additions & 14 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,21 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog"
)

var (
nodeCaps = []csi.NodeServiceCapability_RPC_Type{}
nodeCaps = []csi.NodeServiceCapability_RPC_Type{}
volumeCapAccessModes = []csi.VolumeCapability_AccessMode_Mode{
csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER,
csi.VolumeCapability_AccessMode_MULTI_NODE_MULTI_WRITER,
}
)

func (d *Driver) NodeStageVolume(ctx context.Context, req *csi.NodeStageVolumeRequest) (*csi.NodeStageVolumeResponse, error) {
Expand All @@ -42,9 +48,6 @@ func (d *Driver) NodeUnstageVolume(ctx context.Context, req *csi.NodeUnstageVolu
func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolumeRequest) (*csi.NodePublishVolumeResponse, error) {
klog.V(4).Infof("NodePublishVolume: called with args %+v", req)

volumeId := req.GetVolumeId()
source := fmt.Sprintf("%s:/", volumeId)

target := req.GetTargetPath()
if len(target) == 0 {
return nil, status.Error(codes.InvalidArgument, "Target path not provided")
Expand All @@ -59,6 +62,24 @@ func (d *Driver) NodePublishVolume(ctx context.Context, req *csi.NodePublishVolu
return nil, status.Error(codes.InvalidArgument, "Volume capability not supported")
}

// TODO when CreateVolume is implemented, it must use the same key names
path := "/"
volContext := req.GetVolumeContext()
for k, v := range volContext {
switch strings.ToLower(k) {
case "path":
if !filepath.IsAbs(v) {
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("Volume context property %q must be an absolute path", k))
}
path = filepath.Join(path, v)
default:
return nil, status.Error(codes.InvalidArgument, fmt.Sprintf("Volume context property %s not supported", k))
}
}

volumeId := req.GetVolumeId()
source := fmt.Sprintf("%s:%s", volumeId, path)

mountOptions := []string{}
if req.GetReadonly() {
mountOptions = append(mountOptions, "ro")
Expand Down Expand Up @@ -110,6 +131,14 @@ func (d *Driver) NodeUnpublishVolume(ctx context.Context, req *csi.NodeUnpublish
return &csi.NodeUnpublishVolumeResponse{}, nil
}

func (d *Driver) NodeGetVolumeStats(ctx context.Context, req *csi.NodeGetVolumeStatsRequest) (*csi.NodeGetVolumeStatsResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}

func (d *Driver) NodeExpandVolume(ctx context.Context, req *csi.NodeExpandVolumeRequest) (*csi.NodeExpandVolumeResponse, error) {
return nil, status.Error(codes.Unimplemented, "")
}

func (d *Driver) NodeGetCapabilities(ctx context.Context, req *csi.NodeGetCapabilitiesRequest) (*csi.NodeGetCapabilitiesResponse, error) {
klog.V(4).Infof("NodeGetCapabilities: called with args %+v", req)
var caps []*csi.NodeServiceCapability
Expand All @@ -134,17 +163,10 @@ func (d *Driver) NodeGetInfo(ctx context.Context, req *csi.NodeGetInfoRequest) (
}, nil
}

func (d *Driver) NodeGetId(ctx context.Context, req *csi.NodeGetIdRequest) (*csi.NodeGetIdResponse, error) {
klog.V(4).Infof("NodeGetId: called with args %+v", req)
return &csi.NodeGetIdResponse{
NodeId: d.nodeID,
}, nil
}

func (d *Driver) isValidVolumeCapabilities(volCaps []*csi.VolumeCapability) bool {
hasSupport := func(cap *csi.VolumeCapability) bool {
for _, c := range volumeCaps {
if c.GetMode() == cap.AccessMode.GetMode() {
for _, m := range volumeCapAccessModes {
if m == cap.AccessMode.GetMode() {
return true
}
}
Expand Down
102 changes: 89 additions & 13 deletions pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"testing"

csi "github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/container-storage-interface/spec/lib/go/csi"
"github.com/golang/mock/gomock"
"github.com/kubernetes-sigs/aws-efs-csi-driver/pkg/driver/mocks"
)
Expand Down Expand Up @@ -62,7 +62,6 @@ func TestNodePublishVolume(t *testing.T) {
ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeAttributes: map[string]string{},
VolumeCapability: stdVolCap,
TargetPath: targetPath,
}
Expand Down Expand Up @@ -92,7 +91,6 @@ func TestNodePublishVolume(t *testing.T) {
ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeAttributes: map[string]string{},
VolumeCapability: stdVolCap,
TargetPath: targetPath,
Readonly: true,
Expand Down Expand Up @@ -122,8 +120,7 @@ func TestNodePublishVolume(t *testing.T) {

ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeAttributes: map[string]string{},
VolumeId: volumeId,
VolumeCapability: &csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{
Expand All @@ -147,6 +144,36 @@ func TestNodePublishVolume(t *testing.T) {
mockCtrl.Finish()
},
},
{
name: "success: normal with path volume context",
testFunc: func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockMounter := mocks.NewMockInterface(mockCtrl)
driver := &Driver{
endpoint: endpoint,
nodeID: nodeID,
mounter: mockMounter,
}
source := volumeId + ":/a/b"

ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeCapability: stdVolCap,
TargetPath: targetPath,
VolumeContext: map[string]string{"path": "/a/b"},
}

mockMounter.EXPECT().MakeDir(gomock.Eq(targetPath)).Return(nil)
mockMounter.EXPECT().Mount(gomock.Eq(source), gomock.Eq(targetPath), gomock.Eq("efs"), gomock.Any()).Return(nil)
_, err := driver.NodePublishVolume(ctx, req)
if err != nil {
t.Fatalf("NodePublishVolume is failed: %v", err)
}

mockCtrl.Finish()
},
},
{
name: "fail: missing target path",
testFunc: func(t *testing.T) {
Expand All @@ -161,7 +188,6 @@ func TestNodePublishVolume(t *testing.T) {
ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeAttributes: map[string]string{},
VolumeCapability: stdVolCap,
}

Expand All @@ -186,9 +212,8 @@ func TestNodePublishVolume(t *testing.T) {

ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeAttributes: map[string]string{},
TargetPath: targetPath,
VolumeId: volumeId,
TargetPath: targetPath,
}

_, err := driver.NodePublishVolume(ctx, req)
Expand All @@ -212,8 +237,7 @@ func TestNodePublishVolume(t *testing.T) {

ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeAttributes: map[string]string{},
VolumeId: volumeId,
VolumeCapability: &csi.VolumeCapability{
AccessType: &csi.VolumeCapability_Mount{
Mount: &csi.VolumeCapability_MountVolume{},
Expand Down Expand Up @@ -247,7 +271,6 @@ func TestNodePublishVolume(t *testing.T) {
ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeAttributes: map[string]string{},
VolumeCapability: stdVolCap,
TargetPath: targetPath,
}
Expand Down Expand Up @@ -277,7 +300,6 @@ func TestNodePublishVolume(t *testing.T) {
ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeAttributes: map[string]string{},
VolumeCapability: stdVolCap,
TargetPath: targetPath,
}
Expand All @@ -292,6 +314,60 @@ func TestNodePublishVolume(t *testing.T) {
t.Fatalf("NodePublishVolume is not failed: %v", err)
}

mockCtrl.Finish()
},
},
{
name: "fail: unsupported volume context",
testFunc: func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockMounter := mocks.NewMockInterface(mockCtrl)
driver := &Driver{
endpoint: endpoint,
nodeID: nodeID,
mounter: mockMounter,
}

ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeCapability: stdVolCap,
TargetPath: targetPath,
VolumeContext: map[string]string{"asdf": "qwer"},
}

_, err := driver.NodePublishVolume(ctx, req)
if err == nil {
t.Fatalf("NodePublishVolume is not failed: %v", err)
}

mockCtrl.Finish()
},
},
{
name: "fail: relative path volume context",
testFunc: func(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockMounter := mocks.NewMockInterface(mockCtrl)
driver := &Driver{
endpoint: endpoint,
nodeID: nodeID,
mounter: mockMounter,
}

ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeCapability: stdVolCap,
TargetPath: targetPath,
VolumeContext: map[string]string{"path": "a/b"},
}

_, err := driver.NodePublishVolume(ctx, req)
if err == nil {
t.Fatalf("NodePublishVolume is not failed: %v", err)
}

mockCtrl.Finish()
},
},
Expand Down