Skip to content

Commit

Permalink
fixing shared volume creation
Browse files Browse the repository at this point in the history
  • Loading branch information
vbouchaud committed Sep 13, 2021
1 parent dbda46f commit 8ca2212
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import (
)

const (
provisionerNameKey = "PROVISIONER_NAME"
namespacedAnnotationKey = "nfs-provisioner.legion.bouchaud.org/shared-with-key"
provisionerNameKey = "PROVISIONER_NAME"
sharedAnnotationKey = "nfs-provisioner.legion.bouchaud.org/shared-with-key"
)

type nfsProvisioner struct {
Expand All @@ -59,24 +59,35 @@ func (p *nfsProvisioner) Provision(_ context.Context, options controller.Provisi
}
log.Info().Msgf("nfs provisioner: VolumeOptions %v", options)

var pvName string
var dirName string
var namespace string
var shared = false

namespace = options.PVC.Namespace
if _, ok := options.PVC.Annotations[namespacedAnnotationKey]; ok {
namespace = options.PVC.Annotations[namespacedAnnotationKey]

if _, ok := options.PVC.Annotations[sharedAnnotationKey]; ok {
shared = true
}

if shared {
dirName = strings.Join([]string{"shared", options.PVC.Annotations[sharedAnnotationKey]}, "-")
} else {
dirName = strings.Join([]string{namespace, options.PVC.Name, options.PVName}, "-")
}

pvName = strings.Join([]string{namespace, options.PVC.Name, options.PVName}, "-")
if *options.StorageClass.ReclaimPolicy != core.PersistentVolumeReclaimRetain && shared {
return nil, controller.ProvisioningFinished, errors.New("not allowed to create shared volatile pv.")
}

fullPath := filepath.Join(mountPath, pvName)
fullPath := filepath.Join(mountPath, dirName)
log.Info().Msgf("creating path %s", fullPath)

if err := os.MkdirAll(fullPath, 0777); err != nil {
return nil, controller.ProvisioningFinished, errors.New("unable to create directory to provision new pv: " + err.Error())
}
os.Chmod(fullPath, 0777)

path := filepath.Join(p.path, pvName)
// if path is already a directory, still chown
os.Chmod(fullPath, 0777)

pv := &core.PersistentVolume{
ObjectMeta: meta.ObjectMeta{
Expand All @@ -92,7 +103,7 @@ func (p *nfsProvisioner) Provision(_ context.Context, options controller.Provisi
PersistentVolumeSource: core.PersistentVolumeSource{
NFS: &core.NFSVolumeSource{
Server: p.server,
Path: path,
Path: filepath.Join(p.path, dirName),
ReadOnly: false,
},
},
Expand Down

0 comments on commit 8ca2212

Please sign in to comment.