From 1dc67d59efd9bb5f60756b52f2c67a435d609060 Mon Sep 17 00:00:00 2001 From: Fabian Ruff Date: Thu, 29 Apr 2021 06:20:50 +0200 Subject: [PATCH] [cinder-csi-plugin] Tag volume with optional pvc/pv metadata (#1492) * [cinder-csi-plugin] Tag volume with optional metadata The external CSI provisioner optionally injects metadata in the CreateVolume that we can pass onto the volume as additional tags. See https://github.com/kubernetes-csi/external-provisioner/pull/399 Signed-off-by: Fabian Ruff * Add --extra-create-metadata to csi-provisoner manifests Signed-off-by: Fabian Ruff * Add unit test for extra metadata Signed-off-by: Fabian Ruff * Bump chart again. Signed-off-by: Fabian Ruff --- charts/cinder-csi-plugin/Chart.yaml | 2 +- .../controllerplugin-statefulset.yaml | 1 + .../cinder-csi-controllerplugin.yaml | 1 + pkg/csi/cinder/controllerserver.go | 6 +++ pkg/csi/cinder/controllerserver_test.go | 47 +++++++++++++++++++ pkg/csi/cinder/fake.go | 3 ++ 6 files changed, 59 insertions(+), 1 deletion(-) diff --git a/charts/cinder-csi-plugin/Chart.yaml b/charts/cinder-csi-plugin/Chart.yaml index eef3d758f5..5def1ef6a5 100644 --- a/charts/cinder-csi-plugin/Chart.yaml +++ b/charts/cinder-csi-plugin/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v1 appVersion: latest description: Cinder CSI Chart for OpenStack name: openstack-cinder-csi -version: 1.3.6 +version: 1.3.7 home: https://github.com/kubernetes/cloud-provider-openstack icon: https://github.com/kubernetes/kubernetes/blob/master/logo/logo.png maintainers: diff --git a/charts/cinder-csi-plugin/templates/controllerplugin-statefulset.yaml b/charts/cinder-csi-plugin/templates/controllerplugin-statefulset.yaml index a8e4cb422d..1439da3dea 100644 --- a/charts/cinder-csi-plugin/templates/controllerplugin-statefulset.yaml +++ b/charts/cinder-csi-plugin/templates/controllerplugin-statefulset.yaml @@ -46,6 +46,7 @@ spec: - "--timeout={{ .Values.timeout }}" - "--default-fstype=ext4" - "--feature-gates=Topology={{ .Values.csi.provisioner.topology }}" + - "--extra-create-metadata" env: - name: ADDRESS value: /var/lib/csi/sockets/pluginproxy/csi.sock diff --git a/manifests/cinder-csi-plugin/cinder-csi-controllerplugin.yaml b/manifests/cinder-csi-plugin/cinder-csi-controllerplugin.yaml index 4c5addbfbf..4fe41dd44e 100644 --- a/manifests/cinder-csi-plugin/cinder-csi-controllerplugin.yaml +++ b/manifests/cinder-csi-plugin/cinder-csi-controllerplugin.yaml @@ -54,6 +54,7 @@ spec: - "--timeout=3m" - "--default-fstype=ext4" - "--feature-gates=Topology=true" + - "--extra-create-metadata" env: - name: ADDRESS value: /var/lib/csi/sockets/pluginproxy/csi.sock diff --git a/pkg/csi/cinder/controllerserver.go b/pkg/csi/cinder/controllerserver.go index 1c8e7dbd5f..09320bd0c4 100644 --- a/pkg/csi/cinder/controllerserver.go +++ b/pkg/csi/cinder/controllerserver.go @@ -103,6 +103,12 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol // Volume Create properties := map[string]string{"cinder.csi.openstack.org/cluster": cs.Driver.cluster} + //Tag volume with metadata if present: https://github.com/kubernetes-csi/external-provisioner/pull/399 + for _, mKey := range []string{"csi.storage.k8s.io/pvc/name", "csi.storage.k8s.io/pvc/namespace", "csi.storage.k8s.io/pv/name"} { + if v, ok := req.Parameters[mKey]; ok { + properties[mKey] = v + } + } content := req.GetVolumeContentSource() var snapshotID string var sourcevolID string diff --git a/pkg/csi/cinder/controllerserver_test.go b/pkg/csi/cinder/controllerserver_test.go index 65ffbb0d0b..6673feaad5 100644 --- a/pkg/csi/cinder/controllerserver_test.go +++ b/pkg/csi/cinder/controllerserver_test.go @@ -87,6 +87,53 @@ func TestCreateVolume(t *testing.T) { } +func TestCreateVolumeWithExtraMetadata(t *testing.T) { + + // mock OpenStack + properties := map[string]string{ + "cinder.csi.openstack.org/cluster": FakeCluster, + "csi.storage.k8s.io/pv/name": FakePVName, + "csi.storage.k8s.io/pvc/name": FakePVCName, + "csi.storage.k8s.io/pvc/namespace": FakePVCNamespace, + } + // CreateVolume(name string, size int, vtype, availability string, snapshotID string, tags *map[string]string) (string, string, int, error) + osmock.On("CreateVolume", FakeVolName, mock.AnythingOfType("int"), FakeVolType, FakeAvailability, "", "", &properties).Return(&FakeVol, nil) + + osmock.On("GetVolumesByName", FakeVolName).Return(FakeVolListEmpty, nil) + + // Fake request + fakeReq := &csi.CreateVolumeRequest{ + Name: FakeVolName, + Parameters: map[string]string{ + "csi.storage.k8s.io/pv/name": FakePVName, + "csi.storage.k8s.io/pvc/name": FakePVCName, + "csi.storage.k8s.io/pvc/namespace": FakePVCNamespace, + }, + VolumeCapabilities: []*csi.VolumeCapability{ + { + AccessMode: &csi.VolumeCapability_AccessMode{ + Mode: csi.VolumeCapability_AccessMode_SINGLE_NODE_WRITER, + }, + }, + }, + + AccessibilityRequirements: &csi.TopologyRequirement{ + Requisite: []*csi.Topology{ + { + Segments: map[string]string{"topology.cinder.csi.openstack.org/zone": FakeAvailability}, + }, + }, + }, + } + + // Invoke CreateVolume + _, err := fakeCs.CreateVolume(FakeCtx, fakeReq) + if err != nil { + t.Errorf("failed to CreateVolume: %v", err) + } + +} + func TestCreateVolumeFromSnapshot(t *testing.T) { properties := map[string]string{"cinder.csi.openstack.org/cluster": FakeCluster} diff --git a/pkg/csi/cinder/fake.go b/pkg/csi/cinder/fake.go index 09d3c466c3..5a6a8f4433 100644 --- a/pkg/csi/cinder/fake.go +++ b/pkg/csi/cinder/fake.go @@ -38,6 +38,9 @@ var FakeAvailability = "nova" var FakeDevicePath = "/dev/xxx" var FakeTargetPath = "/mnt/cinder" var FakeStagingTargetPath = "/mnt/globalmount" +var FakePVName = "fakepv-1" +var FakePVCName = "fakepvc-1" +var FakePVCNamespace = "fakepvc-ns" var FakeAttachment = volumes.Attachment{ ServerID: FakeNodeID, }