Skip to content

Commit

Permalink
Merge pull request kubernetes#98608 from ialidzhikov/automated-cherry…
Browse files Browse the repository at this point in the history
…-pick-of-#98311-upstream-release-1.20

Automated cherry pick of kubernetes#98311: Fix translation of Cinder storage classess to CSI
  • Loading branch information
k8s-ci-robot committed Feb 4, 2021
2 parents f89e356 + 3a83880 commit 542316f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions staging/src/k8s.io/csi-translation-lib/plugins/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
25 changes: 25 additions & 0 deletions staging/src/k8s.io/csi-translation-lib/plugins/openstack_cinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package plugins

import (
"fmt"
"strings"

v1 "k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1"
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}

}
}

0 comments on commit 542316f

Please sign in to comment.