Commit e826a779 authored by Humble Chirammal's avatar Humble Chirammal

Add custom volumename option to GlusterFS dynamic PVs.

At present glusterfs dynamic PVs are created with random names. However an admin would like to have some handle on the volume names created dynamically for various purposes. One example would be having a filter for sorting out PVs created for a particular storage class. This patch enables the functionality by having a custom volume name as a prefix to dynamic PVs. This is an optional parameter in SC and if set, the dynamic volumes are created in below format where `_` is the field seperator/delimiter: customvolumeprefix_PVCname_randomUUID Signed-off-by: 's avatarHumble Chirammal <hchiramm@redhat.com>
parent 8406a547
...@@ -36,6 +36,7 @@ import ( ...@@ -36,6 +36,7 @@ import (
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/uuid"
clientset "k8s.io/client-go/kubernetes" clientset "k8s.io/client-go/kubernetes"
v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper" v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
"k8s.io/kubernetes/pkg/util/mount" "k8s.io/kubernetes/pkg/util/mount"
...@@ -406,17 +407,18 @@ func (plugin *glusterfsPlugin) newProvisionerInternal(options volume.VolumeOptio ...@@ -406,17 +407,18 @@ func (plugin *glusterfsPlugin) newProvisionerInternal(options volume.VolumeOptio
} }
type provisionerConfig struct { type provisionerConfig struct {
url string url string
user string user string
userKey string userKey string
secretNamespace string secretNamespace string
secretName string secretName string
secretValue string secretValue string
clusterID string clusterID string
gidMin int gidMin int
gidMax int gidMax int
volumeType gapi.VolumeDurabilityInfo volumeType gapi.VolumeDurabilityInfo
volumeOptions []string volumeOptions []string
volumeNamePrefix string
} }
type glusterfsVolumeProvisioner struct { type glusterfsVolumeProvisioner struct {
...@@ -743,6 +745,7 @@ func (p *glusterfsVolumeProvisioner) Provision() (*v1.PersistentVolume, error) { ...@@ -743,6 +745,7 @@ func (p *glusterfsVolumeProvisioner) Provision() (*v1.PersistentVolume, error) {
func (p *glusterfsVolumeProvisioner) CreateVolume(gid int) (r *v1.GlusterfsVolumeSource, size int, volID string, err error) { func (p *glusterfsVolumeProvisioner) CreateVolume(gid int) (r *v1.GlusterfsVolumeSource, size int, volID string, err error) {
var clusterIDs []string var clusterIDs []string
customVolumeName := ""
capacity := p.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)] capacity := p.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
// Glusterfs creates volumes in units of GiB, but heketi documentation incorrectly reports GBs // Glusterfs creates volumes in units of GiB, but heketi documentation incorrectly reports GBs
sz := int(volume.RoundUpToGiB(capacity)) sz := int(volume.RoundUpToGiB(capacity))
...@@ -760,8 +763,13 @@ func (p *glusterfsVolumeProvisioner) CreateVolume(gid int) (r *v1.GlusterfsVolum ...@@ -760,8 +763,13 @@ func (p *glusterfsVolumeProvisioner) CreateVolume(gid int) (r *v1.GlusterfsVolum
clusterIDs = dstrings.Split(p.clusterID, ",") clusterIDs = dstrings.Split(p.clusterID, ",")
glog.V(4).Infof("provided clusterIDs: %v", clusterIDs) glog.V(4).Infof("provided clusterIDs: %v", clusterIDs)
} }
if p.provisionerConfig.volumeNamePrefix != "" {
customVolumeName = fmt.Sprintf("%s_%s_%s", p.provisionerConfig.volumeNamePrefix, p.options.PVC.Name, uuid.NewUUID())
}
gid64 := int64(gid) gid64 := int64(gid)
volumeReq := &gapi.VolumeCreateRequest{Size: sz, Clusters: clusterIDs, Gid: gid64, Durability: p.volumeType, GlusterVolumeOptions: p.volumeOptions} volumeReq := &gapi.VolumeCreateRequest{Size: sz, Name: customVolumeName, Clusters: clusterIDs, Gid: gid64, Durability: p.volumeType, GlusterVolumeOptions: p.volumeOptions}
volume, err := cli.VolumeCreate(volumeReq) volume, err := cli.VolumeCreate(volumeReq)
if err != nil { if err != nil {
glog.Errorf("error creating volume %v ", err) glog.Errorf("error creating volume %v ", err)
...@@ -927,6 +935,7 @@ func parseClassParameters(params map[string]string, kubeClient clientset.Interfa ...@@ -927,6 +935,7 @@ func parseClassParameters(params map[string]string, kubeClient clientset.Interfa
authEnabled := true authEnabled := true
parseVolumeType := "" parseVolumeType := ""
parseVolumeOptions := "" parseVolumeOptions := ""
parseVolumeNamePrefix := ""
for k, v := range params { for k, v := range params {
switch dstrings.ToLower(k) { switch dstrings.ToLower(k) {
...@@ -977,7 +986,10 @@ func parseClassParameters(params map[string]string, kubeClient clientset.Interfa ...@@ -977,7 +986,10 @@ func parseClassParameters(params map[string]string, kubeClient clientset.Interfa
if len(v) != 0 { if len(v) != 0 {
parseVolumeOptions = v parseVolumeOptions = v
} }
case "volumenameprefix":
if len(v) != 0 {
parseVolumeNamePrefix = v
}
default: default:
return nil, fmt.Errorf("invalid option %q for volume plugin %s", k, glusterfsPluginName) return nil, fmt.Errorf("invalid option %q for volume plugin %s", k, glusterfsPluginName)
} }
...@@ -1057,6 +1069,13 @@ func parseClassParameters(params map[string]string, kubeClient clientset.Interfa ...@@ -1057,6 +1069,13 @@ func parseClassParameters(params map[string]string, kubeClient clientset.Interfa
cfg.volumeOptions = volOptions cfg.volumeOptions = volOptions
} }
if len(parseVolumeNamePrefix) != 0 {
if dstrings.Contains(parseVolumeNamePrefix, "_") {
return nil, fmt.Errorf("Storageclass parameter 'volumenameprefix' should not contain '_' in its value")
}
cfg.volumeNamePrefix = parseVolumeNamePrefix
}
return &cfg, nil return &cfg, nil
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment