Skip to content

Commit

Permalink
Add support for 'path' field in volumeContext
Browse files Browse the repository at this point in the history
  • Loading branch information
wongma7 committed Jul 18, 2019
1 parent 6147e39 commit 3074db5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 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
49 changes: 48 additions & 1 deletion pkg/driver/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package driver
import (
"context"
"fmt"
"path/filepath"
"testing"

"github.com/container-storage-interface/spec/lib/go/csi"
Expand Down Expand Up @@ -48,6 +49,7 @@ func TestNodePublishVolume(t *testing.T) {
readOnly bool
volCap *csi.VolumeCapability
targetPath string
volContext map[string]string
expectMakeDir bool
makeDirErr error
expectMount bool
Expand All @@ -59,6 +61,7 @@ func TestNodePublishVolume(t *testing.T) {
readOnly: false,
volCap: stdVolCap,
targetPath: targetPath,
volContext: map[string]string{},
expectMakeDir: true,
makeDirErr: nil,
expectMount: true,
Expand All @@ -70,6 +73,7 @@ func TestNodePublishVolume(t *testing.T) {
readOnly: true,
volCap: stdVolCap,
targetPath: targetPath,
volContext: map[string]string{},
expectMakeDir: true,
makeDirErr: nil,
expectMount: true,
Expand All @@ -90,6 +94,19 @@ func TestNodePublishVolume(t *testing.T) {
},
},
targetPath: targetPath,
volContext: map[string]string{},
expectMakeDir: true,
makeDirErr: nil,
expectMount: true,
mountErr: nil,
expectFail: false,
},
{
name: "success: normal with path volume context",
readOnly: false,
volCap: stdVolCap,
targetPath: targetPath,
volContext: map[string]string{"path": "/a/b"},
expectMakeDir: true,
makeDirErr: nil,
expectMount: true,
Expand All @@ -101,6 +118,7 @@ func TestNodePublishVolume(t *testing.T) {
readOnly: false,
volCap: stdVolCap,
targetPath: "",
volContext: map[string]string{},
expectMakeDir: false,
makeDirErr: nil,
expectMount: false,
Expand All @@ -112,6 +130,7 @@ func TestNodePublishVolume(t *testing.T) {
readOnly: false,
volCap: nil,
targetPath: targetPath,
volContext: map[string]string{},
expectMakeDir: false,
makeDirErr: nil,
expectMount: false,
Expand All @@ -130,6 +149,7 @@ func TestNodePublishVolume(t *testing.T) {
},
},
targetPath: targetPath,
volContext: map[string]string{},
expectMakeDir: false,
makeDirErr: nil,
expectMount: false,
Expand All @@ -141,6 +161,7 @@ func TestNodePublishVolume(t *testing.T) {
readOnly: false,
volCap: stdVolCap,
targetPath: targetPath,
volContext: map[string]string{},
expectMakeDir: true,
makeDirErr: fmt.Errorf("failed to MakeDir"),
expectMount: false,
Expand All @@ -152,12 +173,37 @@ func TestNodePublishVolume(t *testing.T) {
readOnly: false,
volCap: stdVolCap,
targetPath: targetPath,
volContext: map[string]string{},
expectMakeDir: true,
makeDirErr: nil,
expectMount: true,
mountErr: fmt.Errorf("failed to Mount"),
expectFail: true,
},
{
name: "fail: unsupported volume context",
readOnly: false,
volCap: stdVolCap,
targetPath: targetPath,
volContext: map[string]string{"asdf": "qwer"},
expectMakeDir: false,
makeDirErr: nil,
expectMount: false,
mountErr: nil,
expectFail: true,
},
{
name: "fail: relative path volume context",
readOnly: false,
volCap: stdVolCap,
targetPath: targetPath,
volContext: map[string]string{"path": "../a/b"},
expectMakeDir: false,
makeDirErr: nil,
expectMount: false,
mountErr: nil,
expectFail: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -168,14 +214,15 @@ func TestNodePublishVolume(t *testing.T) {
nodeID: nodeID,
mounter: mockMounter,
}
source := volumeId + ":/"
source := volumeId + ":" + filepath.Join("/", tc.volContext["path"])

ctx := context.Background()
req := &csi.NodePublishVolumeRequest{
VolumeId: volumeId,
VolumeCapability: tc.volCap,
TargetPath: tc.targetPath,
Readonly: tc.readOnly,
VolumeContext: tc.volContext,
}

if tc.expectMakeDir {
Expand Down

0 comments on commit 3074db5

Please sign in to comment.