Skip to content

Commit

Permalink
Merge pull request #52 from wongma7/volume-context-path
Browse files Browse the repository at this point in the history
Add support for 'path' field in volumeContext
  • Loading branch information
k8s-ci-robot committed Jul 25, 2019
2 parents 9e92ae8 + 5b0caec commit f342f2e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
23 changes: 20 additions & 3 deletions pkg/driver/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/container-storage-interface/spec/lib/go/csi"
"google.golang.org/grpc/codes"
Expand All @@ -46,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 @@ -63,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
84 changes: 84 additions & 0 deletions pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,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 Down Expand Up @@ -284,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

0 comments on commit f342f2e

Please sign in to comment.