Merge pull request #57516 from humblec/custom

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Add custom volume name based on SC parameter.

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: Humble Chirammal <hchiramm@redhat.com>
pull/6/head
Kubernetes Submit Queue 2018-01-12 04:51:54 -08:00 committed by GitHub
commit 744a6f0cde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

View File

@ -32,6 +32,7 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/uuid:go_default_library",
"//vendor/k8s.io/client-go/kubernetes:go_default_library",
],
)

View File

@ -36,6 +36,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/uuid"
clientset "k8s.io/client-go/kubernetes"
v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
"k8s.io/kubernetes/pkg/util/mount"
@ -406,17 +407,18 @@ func (plugin *glusterfsPlugin) newProvisionerInternal(options volume.VolumeOptio
}
type provisionerConfig struct {
url string
user string
userKey string
secretNamespace string
secretName string
secretValue string
clusterID string
gidMin int
gidMax int
volumeType gapi.VolumeDurabilityInfo
volumeOptions []string
url string
user string
userKey string
secretNamespace string
secretName string
secretValue string
clusterID string
gidMin int
gidMax int
volumeType gapi.VolumeDurabilityInfo
volumeOptions []string
volumeNamePrefix string
}
type glusterfsVolumeProvisioner struct {
@ -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) {
var clusterIDs []string
customVolumeName := ""
capacity := p.options.PVC.Spec.Resources.Requests[v1.ResourceName(v1.ResourceStorage)]
// Glusterfs creates volumes in units of GiB, but heketi documentation incorrectly reports GBs
sz := int(volume.RoundUpToGiB(capacity))
@ -760,8 +763,13 @@ func (p *glusterfsVolumeProvisioner) CreateVolume(gid int) (r *v1.GlusterfsVolum
clusterIDs = dstrings.Split(p.clusterID, ",")
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)
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)
if err != nil {
glog.Errorf("error creating volume %v ", err)
@ -927,6 +935,7 @@ func parseClassParameters(params map[string]string, kubeClient clientset.Interfa
authEnabled := true
parseVolumeType := ""
parseVolumeOptions := ""
parseVolumeNamePrefix := ""
for k, v := range params {
switch dstrings.ToLower(k) {
@ -977,7 +986,10 @@ func parseClassParameters(params map[string]string, kubeClient clientset.Interfa
if len(v) != 0 {
parseVolumeOptions = v
}
case "volumenameprefix":
if len(v) != 0 {
parseVolumeNamePrefix = v
}
default:
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
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
}