Skip to content

Commit 1cee290

Browse files
committed
Add --pvc-annotation-mappings arg to pass pvc annotations to parameters
1 parent 3827f80 commit 1cee290

File tree

3 files changed

+75
-49
lines changed

3 files changed

+75
-49
lines changed

cmd/csi-provisioner/csi-provisioner.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ package main
1919
import (
2020
goflag "flag"
2121
"fmt"
22-
flag "github.com/spf13/pflag"
2322
"math/rand"
2423
"os"
2524
"strconv"
2625
"time"
2726

27+
flag "github.com/spf13/pflag"
28+
2829
"github.com/golang/glog"
2930

3031
ctrl "github.com/kubernetes-csi/external-provisioner/pkg/controller"
@@ -34,27 +35,29 @@ import (
3435

3536
"google.golang.org/grpc"
3637

38+
"strings"
39+
3740
"k8s.io/apimachinery/pkg/util/wait"
3841
"k8s.io/client-go/kubernetes"
3942
"k8s.io/client-go/rest"
4043
"k8s.io/client-go/tools/clientcmd"
41-
"strings"
4244

4345
utilfeature "k8s.io/apiserver/pkg/util/feature"
4446
utilflag "k8s.io/apiserver/pkg/util/flag"
4547
)
4648

4749
var (
48-
provisioner = flag.String("provisioner", "", "Name of the provisioner. The provisioner will only provision volumes for claims that request a StorageClass with a provisioner field set equal to this name. If omitted, CSI driver name is used.")
49-
master = flag.String("master", "", "Master URL to build a client config from. Either this or kubeconfig needs to be set if the provisioner is being run out of cluster.")
50-
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Either this or master needs to be set if the provisioner is being run out of cluster.")
51-
csiEndpoint = flag.String("csi-address", "/run/csi/socket", "The gRPC endpoint for Target CSI Volume.")
52-
connectionTimeout = flag.Duration("connection-timeout", 10*time.Second, "Timeout for waiting for CSI driver socket.")
53-
volumeNamePrefix = flag.String("volume-name-prefix", "pvc", "Prefix to apply to the name of a created volume.")
54-
volumeNameUUIDLength = flag.Int("volume-name-uuid-length", -1, "Truncates generated UUID of a created volume to this length. Defaults behavior is to NOT truncate.")
55-
showVersion = flag.Bool("version", false, "Show version.")
56-
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.")
57-
featureGates map[string]bool
50+
provisioner = flag.String("provisioner", "", "Name of the provisioner. The provisioner will only provision volumes for claims that request a StorageClass with a provisioner field set equal to this name. If omitted, CSI driver name is used.")
51+
master = flag.String("master", "", "Master URL to build a client config from. Either this or kubeconfig needs to be set if the provisioner is being run out of cluster.")
52+
kubeconfig = flag.String("kubeconfig", "", "Absolute path to the kubeconfig file. Either this or master needs to be set if the provisioner is being run out of cluster.")
53+
csiEndpoint = flag.String("csi-address", "/run/csi/socket", "The gRPC endpoint for Target CSI Volume.")
54+
connectionTimeout = flag.Duration("connection-timeout", 10*time.Second, "Timeout for waiting for CSI driver socket.")
55+
volumeNamePrefix = flag.String("volume-name-prefix", "pvc", "Prefix to apply to the name of a created volume.")
56+
volumeNameUUIDLength = flag.Int("volume-name-uuid-length", -1, "Truncates generated UUID of a created volume to this length. Defaults behavior is to NOT truncate.")
57+
showVersion = flag.Bool("version", false, "Show version.")
58+
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.")
59+
featureGates map[string]bool
60+
pvcAnnotationMappings map[string]string
5861

5962
provisionController *controller.ProvisionController
6063
version = "unknown"
@@ -66,6 +69,7 @@ func init() {
6669

6770
flag.Var(utilflag.NewMapStringBool(&featureGates), "feature-gates", "A set of key=value pairs that describe feature gates for alpha/experimental features. "+
6871
"Options are:\n"+strings.Join(utilfeature.DefaultFeatureGate.KnownFeatures(), "\n"))
72+
flag.Var(utilflag.NewMapStringString(&pvcAnnotationMappings), "pvc-annotation-mappings", "A set of key=value pairs that describe how pvc annotation should be mapped to parameters that are passed to csi drivers.")
6973

7074
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
7175
flag.Parse()
@@ -148,7 +152,7 @@ func init() {
148152

149153
// Create the provisioner: it implements the Provisioner interface expected by
150154
// the controller
151-
csiProvisioner := ctrl.NewCSIProvisioner(clientset, csiAPIClient, *csiEndpoint, *connectionTimeout, identity, *volumeNamePrefix, *volumeNameUUIDLength, grpcClient, snapClient)
155+
csiProvisioner := ctrl.NewCSIProvisioner(clientset, csiAPIClient, *csiEndpoint, *connectionTimeout, identity, *volumeNamePrefix, *volumeNameUUIDLength, pvcAnnotationMappings, grpcClient, snapClient)
152156
provisionController = controller.NewProvisionController(
153157
clientset,
154158
*provisioner,

pkg/controller/controller.go

+42-20
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,17 @@ const (
8484

8585
// CSIProvisioner struct
8686
type csiProvisioner struct {
87-
client kubernetes.Interface
88-
csiClient csi.ControllerClient
89-
csiAPIClient csiclientset.Interface
90-
grpcClient *grpc.ClientConn
91-
snapshotClient snapclientset.Interface
92-
timeout time.Duration
93-
identity string
94-
volumeNamePrefix string
95-
volumeNameUUIDLength int
96-
config *rest.Config
87+
client kubernetes.Interface
88+
csiClient csi.ControllerClient
89+
csiAPIClient csiclientset.Interface
90+
grpcClient *grpc.ClientConn
91+
snapshotClient snapclientset.Interface
92+
timeout time.Duration
93+
identity string
94+
volumeNamePrefix string
95+
volumeNameUUIDLength int
96+
pvcAnnotationMappings map[string]string
97+
config *rest.Config
9798
}
9899

99100
type driverState struct {
@@ -260,20 +261,22 @@ func NewCSIProvisioner(client kubernetes.Interface,
260261
identity string,
261262
volumeNamePrefix string,
262263
volumeNameUUIDLength int,
264+
pvcAnnotationMappings map[string]string,
263265
grpcClient *grpc.ClientConn,
264266
snapshotClient snapclientset.Interface) controller.Provisioner {
265267

266268
csiClient := csi.NewControllerClient(grpcClient)
267269
provisioner := &csiProvisioner{
268-
client: client,
269-
grpcClient: grpcClient,
270-
csiClient: csiClient,
271-
csiAPIClient: csiAPIClient,
272-
snapshotClient: snapshotClient,
273-
timeout: connectionTimeout,
274-
identity: identity,
275-
volumeNamePrefix: volumeNamePrefix,
276-
volumeNameUUIDLength: volumeNameUUIDLength,
270+
client: client,
271+
grpcClient: grpcClient,
272+
csiClient: csiClient,
273+
csiAPIClient: csiAPIClient,
274+
snapshotClient: snapshotClient,
275+
timeout: connectionTimeout,
276+
identity: identity,
277+
volumeNamePrefix: volumeNamePrefix,
278+
volumeNameUUIDLength: volumeNameUUIDLength,
279+
pvcAnnotationMappings: pvcAnnotationMappings,
277280
}
278281
return provisioner
279282
}
@@ -435,10 +438,11 @@ func (p *csiProvisioner) Provision(options controller.VolumeOptions) (*v1.Persis
435438
volumeCaps = append(volumeCaps, getVolumeCapability(options, pvcAccessMode, fsType))
436439
}
437440

441+
parameters := createVolumeRequestParameters(options, p.pvcAnnotationMappings)
438442
// Create a CSI CreateVolumeRequest and Response
439443
req := csi.CreateVolumeRequest{
440444
Name: pvName,
441-
Parameters: options.Parameters,
445+
Parameters: parameters,
442446
VolumeCapabilities: volumeCaps,
443447
CapacityRange: &csi.CapacityRange{
444448
RequiredBytes: int64(volSizeBytes),
@@ -834,3 +838,21 @@ func bytesToGiQuantity(bytes int64) resource.Quantity {
834838
stringQuantity := fmt.Sprintf("%v%s", num, suffix)
835839
return resource.MustParse(stringQuantity)
836840
}
841+
842+
func createVolumeRequestParameters(options controller.VolumeOptions, mappings map[string]string) map[string]string {
843+
parameters := make(map[string]string)
844+
// Copy all parameters from options.Parameters
845+
for k, v := range options.Parameters {
846+
parameters[k] = v
847+
}
848+
849+
// Copy a parameter from options.PVC.Annotations, if a mapping for the parameter exists.
850+
// Key is replaced with the value that is specified as value of the mapping.
851+
for k, v := range options.PVC.Annotations {
852+
if newKey, ok := mappings[k]; ok {
853+
parameters[newKey] = v
854+
}
855+
}
856+
857+
return parameters
858+
}

pkg/controller/controller_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,14 @@ func TestCreateDriverReturnsInvalidCapacityDuringProvision(t *testing.T) {
430430
defer mockController.Finish()
431431
defer driver.Stop()
432432

433-
csiProvisioner := NewCSIProvisioner(nil, nil, driver.Address(), 5*time.Second, "test-provisioner", "test", 5, csiConn.conn, nil)
433+
csiProvisioner := NewCSIProvisioner(nil, nil, driver.Address(), 5*time.Second, "test-provisioner", "test", 5, nil, csiConn.conn, nil)
434434

435435
// Requested PVC with requestedBytes storage
436436
opts := controller.VolumeOptions{
437437
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
438-
PVName: "test-name",
439-
PVC: createFakePVC(requestedBytes),
440-
Parameters: map[string]string{},
438+
PVName: "test-name",
439+
PVC: createFakePVC(requestedBytes),
440+
Parameters: map[string]string{},
441441
}
442442

443443
// Drivers CreateVolume response with lower capacity bytes than request
@@ -748,8 +748,8 @@ func TestProvision(t *testing.T) {
748748
"normal provision": {
749749
volOpts: controller.VolumeOptions{
750750
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
751-
PVName: "test-name",
752-
PVC: createFakePVC(requestedBytes),
751+
PVName: "test-name",
752+
PVC: createFakePVC(requestedBytes),
753753
Parameters: map[string]string{
754754
"fstype": "ext3",
755755
},
@@ -773,7 +773,7 @@ func TestProvision(t *testing.T) {
773773
"provision with access mode multi node multi writer": {
774774
volOpts: controller.VolumeOptions{
775775
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
776-
PVName: "test-name",
776+
PVName: "test-name",
777777
PVC: &v1.PersistentVolumeClaim{
778778
ObjectMeta: metav1.ObjectMeta{
779779
UID: "testid",
@@ -821,7 +821,7 @@ func TestProvision(t *testing.T) {
821821
"provision with access mode multi node multi readonly": {
822822
volOpts: controller.VolumeOptions{
823823
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
824-
PVName: "test-name",
824+
PVName: "test-name",
825825
PVC: &v1.PersistentVolumeClaim{
826826
ObjectMeta: metav1.ObjectMeta{
827827
UID: "testid",
@@ -869,7 +869,7 @@ func TestProvision(t *testing.T) {
869869
"provision with access mode single writer": {
870870
volOpts: controller.VolumeOptions{
871871
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
872-
PVName: "test-name",
872+
PVName: "test-name",
873873
PVC: &v1.PersistentVolumeClaim{
874874
ObjectMeta: metav1.ObjectMeta{
875875
UID: "testid",
@@ -917,7 +917,7 @@ func TestProvision(t *testing.T) {
917917
"provision with multiple access modes": {
918918
volOpts: controller.VolumeOptions{
919919
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
920-
PVName: "test-name",
920+
PVName: "test-name",
921921
PVC: &v1.PersistentVolumeClaim{
922922
ObjectMeta: metav1.ObjectMeta{
923923
UID: "testid",
@@ -1099,7 +1099,7 @@ func TestProvision(t *testing.T) {
10991099
"provision with mount options": {
11001100
volOpts: controller.VolumeOptions{
11011101
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
1102-
PVName: "test-name",
1102+
PVName: "test-name",
11031103
PVC: &v1.PersistentVolumeClaim{
11041104
ObjectMeta: metav1.ObjectMeta{
11051105
UID: "testid",
@@ -1185,7 +1185,7 @@ func TestProvision(t *testing.T) {
11851185
clientSet = fakeclientset.NewSimpleClientset()
11861186
}
11871187

1188-
csiProvisioner := NewCSIProvisioner(clientSet, nil, driver.Address(), 5*time.Second, "test-provisioner", "test", 5, csiConn.conn, nil)
1188+
csiProvisioner := NewCSIProvisioner(clientSet, nil, driver.Address(), 5*time.Second, "test-provisioner", "test", 5, nil, csiConn.conn, nil)
11891189

11901190
out := &csi.CreateVolumeResponse{
11911191
Volume: &csi.Volume{
@@ -1368,7 +1368,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
13681368
"provision with volume snapshot data source": {
13691369
volOpts: controller.VolumeOptions{
13701370
PersistentVolumeReclaimPolicy: v1.PersistentVolumeReclaimDelete,
1371-
PVName: "test-name",
1371+
PVName: "test-name",
13721372
PVC: &v1.PersistentVolumeClaim{
13731373
ObjectMeta: metav1.ObjectMeta{
13741374
UID: "testid",
@@ -1568,7 +1568,7 @@ func TestProvisionFromSnapshot(t *testing.T) {
15681568
return true, content, nil
15691569
})
15701570

1571-
csiProvisioner := NewCSIProvisioner(clientSet, nil, driver.Address(), 5*time.Second, "test-provisioner", "test", 5, csiConn.conn, client)
1571+
csiProvisioner := NewCSIProvisioner(clientSet, nil, driver.Address(), 5*time.Second, "test-provisioner", "test", 5, nil, csiConn.conn, client)
15721572

15731573
out := &csi.CreateVolumeResponse{
15741574
Volume: &csi.Volume{
@@ -1663,7 +1663,7 @@ func TestProvisionWithTopology(t *testing.T) {
16631663

16641664
clientSet := fakeclientset.NewSimpleClientset()
16651665
csiClientSet := fakecsiclientset.NewSimpleClientset()
1666-
csiProvisioner := NewCSIProvisioner(clientSet, csiClientSet, driver.Address(), 5*time.Second, "test-provisioner", "test", 5, csiConn.conn, nil)
1666+
csiProvisioner := NewCSIProvisioner(clientSet, csiClientSet, driver.Address(), 5*time.Second, "test-provisioner", "test", 5, nil, csiConn.conn, nil)
16671667

16681668
out := &csi.CreateVolumeResponse{
16691669
Volume: &csi.Volume{
@@ -1701,7 +1701,7 @@ func TestProvisionWithMountOptions(t *testing.T) {
17011701

17021702
clientSet := fakeclientset.NewSimpleClientset()
17031703
csiClientSet := fakecsiclientset.NewSimpleClientset()
1704-
csiProvisioner := NewCSIProvisioner(clientSet, csiClientSet, driver.Address(), 5*time.Second, "test-provisioner", "test", 5, csiConn.conn, nil)
1704+
csiProvisioner := NewCSIProvisioner(clientSet, csiClientSet, driver.Address(), 5*time.Second, "test-provisioner", "test", 5, nil, csiConn.conn, nil)
17051705

17061706
out := &csi.CreateVolumeResponse{
17071707
Volume: &csi.Volume{

0 commit comments

Comments
 (0)