Skip to content

Commit

Permalink
create subdir
Browse files Browse the repository at this point in the history
  • Loading branch information
hervenicol committed Apr 2, 2021
1 parent 15aa2cc commit 7c5aaef
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v0.6.0

* BREAKING: now creates a directory at the root of the volumes - eases giving specific access rights to a volume, and makes it compatible with rexray volumes. Use `"volumeSubDir": ""` in config for previous behaviour.

## v0.5.0

* Support for "type" volume option (defaults to "classic")
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ Provide configuration for the plugin:
"applicationCredentialSecret": "",
"region": "",
"mountDir": "/var/lib/cinder/mounts",
"filesystem": "",
"defaultsize": "",
"defaulttype": ""
"filesystem": "xfs",
"defaultsize": "1",
"defaulttype": "high-speed",
"volumeSubDir": "data"
}
```

Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type tConfig struct {
Filesystem string `json:"filesystem,omitempty"`
DefaultSize string `json:"defaultsize,omitempty"`
DefaultType string `json:"defaulttype,omitempty"`
VolumeSubDir string `json:"volumeSubDir,omitempty"`
}

func init() {
Expand All @@ -51,9 +52,10 @@ func main() {
flag.BoolVar(&config.Quiet, "quiet", false, "Only report errors")
flag.StringVar(&configFile, "config", "", "")
flag.StringVar(&config.MountDir, "mountDir", "", "")
flag.StringVar(&config.Filesystem, "filesystem", "ext4", "New volumes filesystem")
flag.StringVar(&config.DefaultSize, "defaultsize", "10", "New volumes default size")
flag.StringVar(&config.DefaultType, "defaulttype", "classic", "New volumes default type")
flag.StringVar(&config.Filesystem, "filesystem", "ext4", "New volumes filesystem (ext4)")
flag.StringVar(&config.DefaultSize, "defaultsize", "10", "New volumes default size (10)")
flag.StringVar(&config.DefaultType, "defaulttype", "classic", "New volumes default type (classic)")
flag.StringVar(&config.VolumeSubDir, "volumeSubDir", "data", "Volumes subdirectory (data)")
flag.Parse()

if len(configFile) == 0 {
Expand Down
30 changes: 26 additions & 4 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ func (d plugin) Get(r *volume.GetRequest) (*volume.GetResponse, error) {
vol, err := d.getByName(r.Name)

if err != nil {
logger.WithError(err).Errorf("Error retriving volume: %s", err.Error())
logger.WithError(err).Errorf("Error retrieving volume: %s", err.Error())
return nil, err
}

response := &volume.GetResponse{
Volume: &volume.Volume{
Name: r.Name,
CreatedAt: vol.CreatedAt.Format(time.RFC3339),
Mountpoint: filepath.Join(d.config.MountDir, r.Name),
Mountpoint: filepath.Join(d.config.MountDir, r.Name, d.config.VolumeSubDir),
},
}

Expand Down Expand Up @@ -272,8 +272,10 @@ func (d plugin) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
return nil, err
}

newVolumeFlag := false
if fsType == "" {
logger.Debug("Volume is empty, formatting")
newVolumeFlag = true
if out, err := formatFilesystem(dev, r.Name, d.config.Filesystem); err != nil {
logger.WithFields(log.Fields{
"output": out,
Expand All @@ -300,8 +302,28 @@ func (d plugin) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
return nil, errors.New(string(out))
}

if newVolumeFlag {

// new volume settings
var perm = 0700
var uid = 0
var gid = 0
path := filepath.Join(d.config.MountDir, r.Name, d.config.VolumeSubDir)

logger.Debugf("New volume, creating VolumeSubDir %s, uid %d / gid %d / perm %o", d.config.VolumeSubDir, uid, gid, perm)

if err = os.MkdirAll(path, os.FileMode(perm)); err != nil {
logger.WithError(err).Error("Error creating VolumeSubDir")
return nil, err
}
if err = os.Chown(path, uid, gid); err != nil {
logger.WithError(err).Error("Error creating VolumeSubDir")
return nil, err
}
}

resp := volume.MountResponse{
Mountpoint: path,
Mountpoint: filepath.Join(path, d.config.VolumeSubDir),
}

logger.Debug("Volume successfully mounted")
Expand All @@ -314,7 +336,7 @@ func (d plugin) Path(r *volume.PathRequest) (*volume.PathResponse, error) {
logger.Debugf("Path: %+v", r)

resp := volume.PathResponse{
Mountpoint: filepath.Join(d.config.MountDir, r.Name),
Mountpoint: filepath.Join(d.config.MountDir, r.Name, d.config.VolumeSubDir),
}

return &resp, nil
Expand Down

0 comments on commit 7c5aaef

Please sign in to comment.