From 3a83880d4420699c503bbd1aac6a8421b4748833 Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Fri, 22 Jan 2021 15:32:30 +0100 Subject: [PATCH] Fix translation of Cinder storage classess to CSI In-tree Cinder storage class must be translated to CSI: - sc.params["fsType"] -> sc.params["csi.storage.k8s.io/fstype"] - sc.allowedTopology (with in-tree topology keys) -> sc.allowedTopology (with CSI topology keys) --- .../k8s.io/csi-translation-lib/plugins/BUILD | 1 + .../plugins/openstack_cinder.go | 25 ++++++ .../plugins/openstack_cinder_test.go | 80 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 staging/src/k8s.io/csi-translation-lib/plugins/openstack_cinder_test.go diff --git a/staging/src/k8s.io/csi-translation-lib/plugins/BUILD b/staging/src/k8s.io/csi-translation-lib/plugins/BUILD index b1d49607efc63..1bbed6477101e 100644 --- a/staging/src/k8s.io/csi-translation-lib/plugins/BUILD +++ b/staging/src/k8s.io/csi-translation-lib/plugins/BUILD @@ -46,6 +46,7 @@ go_test( "azure_file_test.go", "gce_pd_test.go", "in_tree_volume_test.go", + "openstack_cinder_test.go", "vsphere_volume_test.go", ], embed = [":go_default_library"], diff --git a/staging/src/k8s.io/csi-translation-lib/plugins/openstack_cinder.go b/staging/src/k8s.io/csi-translation-lib/plugins/openstack_cinder.go index f3e21bc760ac2..3fb3862e568c4 100644 --- a/staging/src/k8s.io/csi-translation-lib/plugins/openstack_cinder.go +++ b/staging/src/k8s.io/csi-translation-lib/plugins/openstack_cinder.go @@ -18,6 +18,7 @@ package plugins import ( "fmt" + "strings" v1 "k8s.io/api/core/v1" storage "k8s.io/api/storage/v1" @@ -45,6 +46,30 @@ func NewOpenStackCinderCSITranslator() InTreePlugin { // TranslateInTreeStorageClassParametersToCSI translates InTree Cinder storage class parameters to CSI storage class func (t *osCinderCSITranslator) TranslateInTreeStorageClassToCSI(sc *storage.StorageClass) (*storage.StorageClass, error) { + var ( + params = map[string]string{} + ) + for k, v := range sc.Parameters { + switch strings.ToLower(k) { + case fsTypeKey: + params[csiFsTypeKey] = v + default: + // All other parameters are supported by the CSI driver. + // This includes also "availability", therefore do not translate it to sc.AllowedTopologies + params[k] = v + } + } + + if len(sc.AllowedTopologies) > 0 { + newTopologies, err := translateAllowedTopologies(sc.AllowedTopologies, CinderTopologyKey) + if err != nil { + return nil, fmt.Errorf("failed translating allowed topologies: %v", err) + } + sc.AllowedTopologies = newTopologies + } + + sc.Parameters = params + return sc, nil } diff --git a/staging/src/k8s.io/csi-translation-lib/plugins/openstack_cinder_test.go b/staging/src/k8s.io/csi-translation-lib/plugins/openstack_cinder_test.go new file mode 100644 index 0000000000000..e4a95da808303 --- /dev/null +++ b/staging/src/k8s.io/csi-translation-lib/plugins/openstack_cinder_test.go @@ -0,0 +1,80 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package plugins + +import ( + "reflect" + "testing" + + v1 "k8s.io/api/core/v1" + storage "k8s.io/api/storage/v1" +) + +func TestTranslateCinderInTreeStorageClassToCSI(t *testing.T) { + translator := NewOpenStackCinderCSITranslator() + + cases := []struct { + name string + sc *storage.StorageClass + expSc *storage.StorageClass + expErr bool + }{ + { + name: "translate normal", + sc: NewStorageClass(map[string]string{"foo": "bar"}, nil), + expSc: NewStorageClass(map[string]string{"foo": "bar"}, nil), + }, + { + name: "translate empty map", + sc: NewStorageClass(map[string]string{}, nil), + expSc: NewStorageClass(map[string]string{}, nil), + }, + + { + name: "translate with fstype", + sc: NewStorageClass(map[string]string{"fstype": "ext3"}, nil), + expSc: NewStorageClass(map[string]string{"csi.storage.k8s.io/fstype": "ext3"}, nil), + }, + { + name: "translate with topology in parameters (no translation expected)", + sc: NewStorageClass(map[string]string{"availability": "nova"}, nil), + expSc: NewStorageClass(map[string]string{"availability": "nova"}, nil), + }, + { + name: "translate with topology", + sc: NewStorageClass(map[string]string{}, generateToplogySelectors(v1.LabelFailureDomainBetaZone, []string{"nova"})), + expSc: NewStorageClass(map[string]string{}, generateToplogySelectors(CinderTopologyKey, []string{"nova"})), + }, + } + + for _, tc := range cases { + t.Logf("Testing %v", tc.name) + got, err := translator.TranslateInTreeStorageClassToCSI(tc.sc) + if err != nil && !tc.expErr { + t.Errorf("Did not expect error but got: %v", err) + } + + if err == nil && tc.expErr { + t.Errorf("Expected error, but did not get one.") + } + + if !reflect.DeepEqual(got, tc.expSc) { + t.Errorf("Got parameters: %v, expected: %v", got, tc.expSc) + } + + } +}