From b54578fc3ea647226d9338a87e2c4f41a8faef5f Mon Sep 17 00:00:00 2001 From: Rex Morgan Date: Wed, 28 Aug 2019 19:05:47 -0500 Subject: [PATCH] Adding ability to specify uid/gid when using goofys. With this change, while setting up your StorageClass, you can specify the uid and gid that goofys should use when mounting your file system. These two new options are goofysuid and goofysgid. --- deploy/kubernetes/storageclass.yaml | 8 ++++++++ pkg/s3/mounter.go | 2 +- pkg/s3/mounter_goofys.go | 24 +++++++++++++++++++++++- pkg/s3/mounter_rclone.go | 2 +- pkg/s3/mounter_s3backer.go | 2 +- pkg/s3/mounter_s3fs.go | 2 +- pkg/s3/nodeserver.go | 2 +- 7 files changed, 36 insertions(+), 6 deletions(-) diff --git a/deploy/kubernetes/storageclass.yaml b/deploy/kubernetes/storageclass.yaml index 1bd02ed..6e6991e 100644 --- a/deploy/kubernetes/storageclass.yaml +++ b/deploy/kubernetes/storageclass.yaml @@ -8,6 +8,14 @@ parameters: # specify which mounter to use # can be set to rclone, s3fs, goofys or s3backer mounter: rclone + + # goofys allows the following additional optional parameters to be passed + + # The uid to mount the volume as. + # goofysuid: "1000" + # The gid to mount the volume as. + # goofysgid: "1000" + csi.storage.k8s.io/provisioner-secret-name: csi-s3-secret csi.storage.k8s.io/provisioner-secret-namespace: kube-system csi.storage.k8s.io/controller-publish-secret-name: csi-s3-secret diff --git a/pkg/s3/mounter.go b/pkg/s3/mounter.go index 0ac2bf9..76b7f49 100644 --- a/pkg/s3/mounter.go +++ b/pkg/s3/mounter.go @@ -14,7 +14,7 @@ import ( type Mounter interface { Stage(stagePath string) error Unstage(stagePath string) error - Mount(source string, target string) error + Mount(source string, target string, attrib map[string]string) error } const ( diff --git a/pkg/s3/mounter_goofys.go b/pkg/s3/mounter_goofys.go index 98a176b..0c40835 100644 --- a/pkg/s3/mounter_goofys.go +++ b/pkg/s3/mounter_goofys.go @@ -3,15 +3,19 @@ package s3 import ( "fmt" "os" + "strconv" "context" + "github.com/golang/glog" goofysApi "github.com/kahing/goofys/api" ) const ( goofysCmd = "goofys" defaultRegion = "us-east-1" + goofysUIDKey = "goofysuid" + goofysGIDKey = "goofysgid" ) // Implements Mounter @@ -46,13 +50,21 @@ func (goofys *goofysMounter) Unstage(stageTarget string) error { return nil } -func (goofys *goofysMounter) Mount(source string, target string) error { +func (goofys *goofysMounter) Mount(source string, target string, attrib map[string]string) error { + uid := uint32FromMap(goofysUIDKey, 0, attrib) + gid := uint32FromMap(goofysGIDKey, 0, attrib) + + glog.V(4).Infof("target %v\nendpoint %v\nregion %v\nuid %v\ngid %v\nattrib %v\n", + target, goofys.endpoint, goofys.region, uid, gid, attrib) + goofysCfg := &goofysApi.Config{ MountPoint: target, Endpoint: goofys.endpoint, Region: goofys.region, DirMode: 0755, FileMode: 0644, + Uid: uid, + Gid: gid, MountOptions: map[string]string{ "allow_other": "", }, @@ -69,3 +81,13 @@ func (goofys *goofysMounter) Mount(source string, target string) error { } return nil } + +func uint32FromMap(key string, defaultValue uint32, attrib map[string]string) uint32 { + value := defaultValue + if valueStr, found := attrib[key]; found { + if i, err := strconv.ParseUint(valueStr, 10, 32); err == nil { + value = uint32(i) + } + } + return value +} diff --git a/pkg/s3/mounter_rclone.go b/pkg/s3/mounter_rclone.go index abc2d32..133a9fe 100644 --- a/pkg/s3/mounter_rclone.go +++ b/pkg/s3/mounter_rclone.go @@ -36,7 +36,7 @@ func (rclone *rcloneMounter) Unstage(stageTarget string) error { return nil } -func (rclone *rcloneMounter) Mount(source string, target string) error { +func (rclone *rcloneMounter) Mount(source string, target string, attrib map[string]string) error { args := []string{ "mount", fmt.Sprintf(":s3:%s/%s", rclone.bucket.Name, rclone.bucket.FSPath), diff --git a/pkg/s3/mounter_s3backer.go b/pkg/s3/mounter_s3backer.go index 118263c..a2495a3 100644 --- a/pkg/s3/mounter_s3backer.go +++ b/pkg/s3/mounter_s3backer.go @@ -81,7 +81,7 @@ func (s3backer *s3backerMounter) Unstage(stageTarget string) error { return fuseUnmount(stageTarget) } -func (s3backer *s3backerMounter) Mount(source string, target string) error { +func (s3backer *s3backerMounter) Mount(source string, target string, attrib map[string]string) error { device := path.Join(source, s3backerDevice) // second mount will mount the 'file' as a filesystem err := mount.New("").Mount(device, target, s3backerFsType, []string{}) diff --git a/pkg/s3/mounter_s3fs.go b/pkg/s3/mounter_s3fs.go index 082e9fc..6156329 100644 --- a/pkg/s3/mounter_s3fs.go +++ b/pkg/s3/mounter_s3fs.go @@ -34,7 +34,7 @@ func (s3fs *s3fsMounter) Unstage(stageTarget string) error { return nil } -func (s3fs *s3fsMounter) Mount(source string, target string) error { +func (s3fs *s3fsMounter) Mount(source string, target string, attrib map[string]string) error { if err := writes3fsPass(s3fs.pwFileContent); err != nil { return err } diff --git a/pkg/s3/nodeserver.go b/pkg/s3/nodeserver.go index 5830e53..ac38249 100644 --- a/pkg/s3/nodeserver.go +++ b/pkg/s3/nodeserver.go @@ -89,7 +89,7 @@ func (ns *nodeServer) NodePublishVolume(ctx context.Context, req *csi.NodePublis if err != nil { return nil, err } - if err := mounter.Mount(stagingTargetPath, targetPath); err != nil { + if err := mounter.Mount(stagingTargetPath, targetPath, attrib); err != nil { return nil, err }