Skip to content

Commit 0272899

Browse files
Merge pull request #8 from DataDog/agaillard/custom-params
[local] Add annotations defined params to volume creation request
2 parents 5ca5e26 + c762d7f commit 0272899

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

pkg/controller/controller.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,15 @@ func (p *csiProvisioner) prepareProvision(ctx context.Context, claim *v1.Persist
614614
}
615615
}
616616

617-
if vacName != "" {
617+
// Get mutable parameters from claim annotations
618+
mutableParameters := make(map[string]string)
619+
for k, v := range claim.Annotations {
620+
if param, ok := strings.CutPrefix(k, p.driverName+"/"); ok {
621+
mutableParameters[param] = v
622+
}
623+
}
624+
625+
if vacName != "" || len(mutableParameters) > 0 {
618626
rc.modifyVolume = true
619627
}
620628

@@ -665,6 +673,7 @@ func (p *csiProvisioner) prepareProvision(ctx context.Context, claim *v1.Persist
665673
CapacityRange: &csi.CapacityRange{
666674
RequiredBytes: int64(volSizeBytes),
667675
},
676+
MutableParameters: mutableParameters,
668677
}
669678

670679
if dataSource != nil && (rc.clone || rc.snapshot) {
@@ -769,6 +778,7 @@ func (p *csiProvisioner) prepareProvision(ctx context.Context, claim *v1.Persist
769778
return nil, controller.ProvisioningFinished, fmt.Errorf("VAC %s referenced in PVC is for driver %s which does not match driver name %s", vacName, vac.DriverName, p.driverName)
770779
}
771780

781+
// Override any mutable parameters set through annotation on the claim
772782
req.MutableParameters = vac.Parameters
773783
}
774784

@@ -903,12 +913,21 @@ func (p *csiProvisioner) Provision(ctx context.Context, options controller.Provi
903913
pvReadOnly = true
904914
}
905915

916+
// Add mutable parameters as annotations so that volume modifier skips them.
917+
annotations := map[string]string{}
918+
for k, v := range options.PVC.Annotations {
919+
if strings.HasPrefix(k, p.driverName+"/") {
920+
annotations[k] = v
921+
}
922+
}
923+
906924
result.csiPVSource.VolumeHandle = p.volumeIdToHandle(rep.Volume.VolumeId)
907925
result.csiPVSource.VolumeAttributes = volumeAttributes
908926
result.csiPVSource.ReadOnly = pvReadOnly
909927
pv := &v1.PersistentVolume{
910928
ObjectMeta: metav1.ObjectMeta{
911-
Name: pvName,
929+
Name: pvName,
930+
Annotations: annotations,
912931
},
913932
Spec: v1.PersistentVolumeSpec{
914933
AccessModes: options.PVC.Spec.AccessModes,

pkg/controller/controller_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,12 @@ func createFakePVCWithVAC(requestBytes int64, vacName string) *v1.PersistentVolu
564564
return claim
565565
}
566566

567+
// createFakePVCWithVAC returns PVC with Annotations
568+
func createFakePVCWithAnnotations(requestBytes int64, annotations map[string]string) *v1.PersistentVolumeClaim {
569+
claim := createFakeNamedPVC(requestBytes, "fake-pvc", annotations)
570+
return claim
571+
}
572+
567573
// fakeClaim returns a valid PVC with the requested settings
568574
func fakeClaim(name, namespace, claimUID string, capacity int64, boundToVolume string, phase v1.PersistentVolumeClaimPhase, class *string, mode string) *v1.PersistentVolumeClaim {
569575
claim := v1.PersistentVolumeClaim{
@@ -2376,6 +2382,46 @@ func provisionTestcases() (int64, map[string]provisioningTestcase) {
23762382
},
23772383
expectState: controller.ProvisioningFinished,
23782384
},
2385+
"normal provision with custom params in annotations": {
2386+
pluginCapabilities: provisionWithVACCapabilities,
2387+
expectCreateVolDo: func(t *testing.T, ctx context.Context, req *csi.CreateVolumeRequest) {
2388+
if !reflect.DeepEqual(req.MutableParameters, map[string]string{"iops": "10000"}) {
2389+
t.Errorf("Missing or incorrect mutable parameters")
2390+
}
2391+
},
2392+
volOpts: controller.ProvisionOptions{
2393+
StorageClass: &storagev1.StorageClass{
2394+
ReclaimPolicy: &deletePolicy,
2395+
Parameters: map[string]string{
2396+
"fstype": "ext3",
2397+
"test-param": "from-sc",
2398+
},
2399+
},
2400+
PVName: "test-name",
2401+
PVC: createFakePVCWithAnnotations(requestedBytes, map[string]string{driverName + "/iops": "10000"}),
2402+
},
2403+
expectedPVSpec: &pvSpec{
2404+
Name: "test-testi",
2405+
Annotations: map[string]string{
2406+
annDeletionProvisionerSecretRefName: "",
2407+
annDeletionProvisionerSecretRefNamespace: "",
2408+
driverName + "/iops": "10000",
2409+
},
2410+
ReclaimPolicy: v1.PersistentVolumeReclaimDelete,
2411+
Capacity: v1.ResourceList{
2412+
v1.ResourceName(v1.ResourceStorage): bytesToQuantity(requestedBytes),
2413+
},
2414+
CSIPVS: &v1.CSIPersistentVolumeSource{
2415+
Driver: "test-driver",
2416+
VolumeHandle: "test-volume-id",
2417+
FSType: "ext3",
2418+
VolumeAttributes: map[string]string{
2419+
"storage.kubernetes.io/csiProvisionerIdentity": "test-provisioner",
2420+
},
2421+
},
2422+
},
2423+
expectState: controller.ProvisioningFinished,
2424+
},
23792425
"normal provision with VolumeAttributesClass but feature gate is disabled": {
23802426
featureGates: map[featuregate.Feature]bool{
23812427
features.VolumeAttributesClass: false,

0 commit comments

Comments
 (0)