From 5d53fb68e5769c0f6439ff05a35a48928b33b8b9 Mon Sep 17 00:00:00 2001 From: Vincent Date: Thu, 27 Oct 2022 08:21:40 +0000 Subject: [PATCH 1/2] Add PVC Annotations to req --- pkg/controller/controller.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 04c9e2913c..e05204aa94 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "os" + "sort" "strconv" "strings" "time" @@ -113,9 +114,10 @@ const ( nodePublishSecretNamespaceKey = "csiNodePublishSecretNamespace" // PV and PVC metadata, used for sending to drivers in the create requests, added as parameters, optional. - pvcNameKey = "csi.storage.k8s.io/pvc/name" - pvcNamespaceKey = "csi.storage.k8s.io/pvc/namespace" - pvNameKey = "csi.storage.k8s.io/pv/name" + pvcNameKey = "csi.storage.k8s.io/pvc/name" + pvcNamespaceKey = "csi.storage.k8s.io/pvc/namespace" + pvcNamespaceAnnotationsKey = "csi.storage.k8s.io/pvc/namespace/annotations" + pvNameKey = "csi.storage.k8s.io/pv/name" snapshotKind = "VolumeSnapshot" snapshotAPIGroup = snapapi.GroupName // "snapshot.storage.k8s.io" @@ -716,9 +718,19 @@ func (p *csiProvisioner) prepareProvision(ctx context.Context, claim *v1.Persist } if p.extraCreateMetadata { + pvcAnnotationMap := claim.GetAnnotations() + + pvcAnnotationPairs := []string{} + for k, v := range pvcAnnotationMap { + pvcAnnotationPairs = append(pvcAnnotationPairs, fmt.Sprintf("%s=%s", k, v)) + } + sort.Strings(pvcAnnotationPairs) + pvcAnnotation := strings.Join(pvcAnnotationPairs, ",") + // add pvc and pv metadata to request for use by the plugin req.Parameters[pvcNameKey] = claim.GetName() req.Parameters[pvcNamespaceKey] = claim.GetNamespace() + req.Parameters[pvcNamespaceAnnotationsKey] = pvcAnnotation req.Parameters[pvNameKey] = pvName } deletionAnnSecrets := new(deletionSecretParams) From 24206ef7ea0a4343bd755a48e4dd362aec8c349f Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 8 Nov 2022 12:33:24 +0000 Subject: [PATCH 2/2] Add test case --- pkg/controller/controller_test.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/controller/controller_test.go b/pkg/controller/controller_test.go index 4d642d5ce5..3c235ef1ff 100644 --- a/pkg/controller/controller_test.go +++ b/pkg/controller/controller_test.go @@ -1107,7 +1107,14 @@ func provisionTestcases() (int64, map[string]provisioningTestcase) { }, }, PVName: "test-name", - PVC: createFakePVC(requestedBytes), + PVC: createFakeNamedPVC( + requestedBytes, + "fake-pvc", + map[string]string{ + "userAnnotation1": "value1", + "userAnnotation2": "value2", + }, + ), }, withExtraMetadata: true, expectedPVSpec: &pvSpec{ @@ -1128,10 +1135,11 @@ func provisionTestcases() (int64, map[string]provisioningTestcase) { expectCreateVolDo: func(t *testing.T, ctx context.Context, req *csi.CreateVolumeRequest) { pvc := createFakePVC(requestedBytes) expectedParams := map[string]string{ - pvcNameKey: pvc.GetName(), - pvcNamespaceKey: pvc.GetNamespace(), - pvNameKey: "test-testi", - "fstype": "ext3", + pvcNameKey: pvc.GetName(), + pvcNamespaceKey: pvc.GetNamespace(), + pvNameKey: "test-testi", + "fstype": "ext3", + pvcNamespaceAnnotationsKey: "userAnnotation1=value1,userAnnotation2=value2,volume.beta.kubernetes.io/storage-provisioner=test-driver", } if fmt.Sprintf("%v", req.Parameters) != fmt.Sprintf("%v", expectedParams) { // only pvc name/namespace left t.Errorf("Unexpected parameters: %v", req.Parameters)