Skip to content

Commit

Permalink
Added flag to enable readable volume names for kubernetes-csi#67
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Cronce committed Dec 6, 2018
1 parent b52f481 commit 4d159a9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/csi-provisioner/csi-provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
connectionTimeout = flag.Duration("connection-timeout", 10*time.Second, "Timeout for waiting for CSI driver socket.")
volumeNamePrefix = flag.String("volume-name-prefix", "pvc", "Prefix to apply to the name of a created volume.")
volumeNameUUIDLength = flag.Int("volume-name-uuid-length", -1, "Truncates generated UUID of a created volume to this length. Defaults behavior is to NOT truncate.")
volumeNamesReadable = flag.Bool("volume-names-readable", false, "If enabled, includes the PVC namespace and name in VolumeRequests' suggested names. Note that, combined with --volume-name-uuid-length, this can cause naming collisions.")
showVersion = flag.Bool("version", false, "Show version.")
enableLeaderElection = flag.Bool("enable-leader-election", false, "Enables leader election. If leader election is enabled, additional RBAC rules are required. Please refer to the Kubernetes CSI documentation for instructions on setting up these RBAC rules.")
featureGates map[string]bool
Expand Down Expand Up @@ -148,7 +149,7 @@ func init() {

// Create the provisioner: it implements the Provisioner interface expected by
// the controller
csiProvisioner := ctrl.NewCSIProvisioner(clientset, csiAPIClient, *csiEndpoint, *connectionTimeout, identity, *volumeNamePrefix, *volumeNameUUIDLength, grpcClient, snapClient)
csiProvisioner := ctrl.NewCSIProvisioner(clientset, csiAPIClient, *csiEndpoint, *connectionTimeout, identity, *volumeNamePrefix, *volumeNameUUIDLength, *volumeNamesReadable, grpcClient, snapClient)
provisionController = controller.NewProvisionController(
clientset,
*provisioner,
Expand Down
36 changes: 26 additions & 10 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ type csiProvisioner struct {
identity string
volumeNamePrefix string
volumeNameUUIDLength int
volumeNamesReadable bool
config *rest.Config
}

Expand Down Expand Up @@ -325,6 +326,7 @@ func NewCSIProvisioner(client kubernetes.Interface,
identity string,
volumeNamePrefix string,
volumeNameUUIDLength int,
volumeNamesReadable bool,
grpcClient *grpc.ClientConn,
snapshotClient snapclientset.Interface) controller.Provisioner {

Expand All @@ -339,6 +341,7 @@ func NewCSIProvisioner(client kubernetes.Interface,
identity: identity,
volumeNamePrefix: volumeNamePrefix,
volumeNameUUIDLength: volumeNameUUIDLength,
volumeNamesReadable: volumeNamesReadable,
}
return provisioner
}
Expand Down Expand Up @@ -381,22 +384,35 @@ func checkDriverState(grpcClient *grpc.ClientConn, timeout time.Duration, needSn
}, nil
}

func makeVolumeName(prefix, pvcUID string, volumeNameUUIDLength int) (string, error) {
func (p *csiProvisioner) makeVolumeName(pvcNamespace, pvcName, pvcUID string) (string, error) {
// create persistent name based on a volumeNamePrefix and volumeNameUUIDLength
// of PVC's UID
if len(prefix) == 0 {
// of PVC's UID; if enabled, utilize PVC namespace and name as well
if(len(p.volumeNamePrefix) == 0) {
return "", fmt.Errorf("Volume name prefix cannot be of length 0")
}
if len(pvcUID) == 0 {
return "", fmt.Errorf("corrupted PVC object, it is missing UID")
}
if volumeNameUUIDLength == -1 {
// Default behavior is to not truncate or remove dashes
return fmt.Sprintf("%s-%s", prefix, pvcUID), nil
}
// Else we remove all dashes from UUID and truncate to volumeNameUUIDLength
return fmt.Sprintf("%s-%s", prefix, strings.Replace(string(pvcUID), "-", "", -1)[0:volumeNameUUIDLength]), nil
pvcUID = strings.Replace(pvcUID, "-", "", -1)

var fullName string
if(p.volumeNamesReadable) {
if(len(pvcNamespace) == 0) {
return "", fmt.Errorf("corrupted PVC object, it is missing namespace")
}
if(len(pvcName) == 0) {
return "", fmt.Errorf("corrupted PVC object, it is missing name")
}
pvcNamespace = strings.Replace(pvcNamespace, "-", "", -1)
pvcName = strings.Replace(pvcName, "-", "", -1)
fullName = fmt.Sprintf("%s-%s-%s", pvcNamespace, pvcName, pvcUID)
} else {
fullName = pvcUID
}
if(p.volumeNameUUIDLength == -1) {
return fmt.Sprintf("%s-%s", p.volumeNamePrefix, fullName), nil
}
return fmt.Sprintf("%s-%s", p.volumeNamePrefix, fullName[0:p.volumeNameUUIDLength]), nil
}

func getAccessTypeBlock() *csi.VolumeCapability_Block {
Expand Down Expand Up @@ -475,7 +491,7 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
return nil, err
}

pvName, err := makeVolumeName(p.volumeNamePrefix, fmt.Sprintf("%s", options.PVC.ObjectMeta.UID), p.volumeNameUUIDLength)
pvName, err := p.makeVolumeName(options.PVC.Namespace, options.PVC.Name, string(options.PVC.ObjectMeta.UID))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4d159a9

Please sign in to comment.