Skip to content

Commit 196bbaa

Browse files
committed
Translate fstype storage class parameter to prefixed stripped parameter
in the gce pd translation library. Change storage class translation library to operate on StorageClass instead of parameters only.
1 parent 0252a32 commit 196bbaa

File tree

8 files changed

+110
-16
lines changed

8 files changed

+110
-16
lines changed

staging/src/k8s.io/csi-translation-lib/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ go_library(
88
visibility = ["//visibility:public"],
99
deps = [
1010
"//staging/src/k8s.io/api/core/v1:go_default_library",
11+
"//staging/src/k8s.io/api/storage/v1:go_default_library",
1112
"//staging/src/k8s.io/csi-translation-lib/plugins:go_default_library",
1213
],
1314
)

staging/src/k8s.io/csi-translation-lib/plugins/BUILD

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ go_library(
1313
visibility = ["//visibility:public"],
1414
deps = [
1515
"//staging/src/k8s.io/api/core/v1:go_default_library",
16+
"//staging/src/k8s.io/api/storage/v1:go_default_library",
1617
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
1718
"//staging/src/k8s.io/cloud-provider/volume:go_default_library",
1819
],
@@ -34,6 +35,10 @@ filegroup(
3435

3536
go_test(
3637
name = "go_default_test",
37-
srcs = ["aws_ebs_test.go"],
38+
srcs = [
39+
"aws_ebs_test.go",
40+
"gce_pd_test.go",
41+
],
3842
embed = [":go_default_library"],
43+
deps = ["//staging/src/k8s.io/api/storage/v1:go_default_library"],
3944
)

staging/src/k8s.io/csi-translation-lib/plugins/aws_ebs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"k8s.io/api/core/v1"
27+
storage "k8s.io/api/storage/v1"
2728
)
2829

2930
const (
@@ -44,8 +45,8 @@ func NewAWSElasticBlockStoreCSITranslator() InTreePlugin {
4445
}
4546

4647
// TranslateInTreeStorageClassParametersToCSI translates InTree EBS storage class parameters to CSI storage class
47-
func (t *awsElasticBlockStoreCSITranslator) TranslateInTreeStorageClassParametersToCSI(scParameters map[string]string) (map[string]string, error) {
48-
return scParameters, nil
48+
func (t *awsElasticBlockStoreCSITranslator) TranslateInTreeVolumeOptionsToCSI(sc storage.StorageClass) (storage.StorageClass, error) {
49+
return sc, nil
4950
}
5051

5152
// TranslateInTreePVToCSI takes a PV with AWSElasticBlockStore set from in-tree

staging/src/k8s.io/csi-translation-lib/plugins/gce_pd.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323

2424
"k8s.io/api/core/v1"
25+
storage "k8s.io/api/storage/v1"
2526
"k8s.io/apimachinery/pkg/util/sets"
2627
cloudvolume "k8s.io/cloud-provider/volume"
2728
)
@@ -56,8 +57,21 @@ func NewGCEPersistentDiskCSITranslator() InTreePlugin {
5657
}
5758

5859
// TranslateInTreeStorageClassParametersToCSI translates InTree GCE storage class parameters to CSI storage class
59-
func (g *gcePersistentDiskCSITranslator) TranslateInTreeStorageClassParametersToCSI(scParameters map[string]string) (map[string]string, error) {
60-
return scParameters, nil
60+
func (g *gcePersistentDiskCSITranslator) TranslateInTreeVolumeOptionsToCSI(sc storage.StorageClass) (storage.StorageClass, error) {
61+
np := map[string]string{}
62+
for k, v := range sc.Parameters {
63+
switch strings.ToLower(k) {
64+
case "fstype":
65+
np["csi.storage.k8s.io/fstype"] = v
66+
default:
67+
np[k] = v
68+
}
69+
}
70+
sc.Parameters = np
71+
72+
// TODO(#77235): Translate AccessModes and zone/zones to AccessibleTopologies
73+
74+
return sc, nil
6175
}
6276

6377
// TranslateInTreePVToCSI takes a PV with GCEPersistentDisk set from in-tree
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package plugins
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
storage "k8s.io/api/storage/v1"
24+
)
25+
26+
func NewStorageClass(params map[string]string) storage.StorageClass {
27+
return storage.StorageClass{
28+
Parameters: params,
29+
}
30+
}
31+
32+
func TestTranslatePDInTreeVolumeOptionsToCSI(t *testing.T) {
33+
g := NewGCEPersistentDiskCSITranslator()
34+
35+
tcs := []struct {
36+
name string
37+
options storage.StorageClass
38+
expOptions storage.StorageClass
39+
}{
40+
{
41+
name: "nothing special",
42+
options: NewStorageClass(map[string]string{"foo": "bar"}),
43+
expOptions: NewStorageClass(map[string]string{"foo": "bar"}),
44+
},
45+
{
46+
name: "fstype",
47+
options: NewStorageClass(map[string]string{"fstype": "myfs"}),
48+
expOptions: NewStorageClass(map[string]string{"csi.storage.k8s.io/fstype": "myfs"}),
49+
},
50+
{
51+
name: "empty params",
52+
options: NewStorageClass(map[string]string{}),
53+
expOptions: NewStorageClass(map[string]string{}),
54+
},
55+
}
56+
57+
for _, tc := range tcs {
58+
t.Logf("Testing %v", tc.name)
59+
gotOptions, err := g.TranslateInTreeVolumeOptionsToCSI(tc.options)
60+
if err != nil {
61+
t.Errorf("Did not expect error but got: %v", err)
62+
}
63+
if !reflect.DeepEqual(gotOptions, tc.expOptions) {
64+
t.Errorf("Got parameters: %v, expected :%v", gotOptions, tc.expOptions)
65+
}
66+
}
67+
}

staging/src/k8s.io/csi-translation-lib/plugins/in_tree_volume.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ limitations under the License.
1616

1717
package plugins
1818

19-
import "k8s.io/api/core/v1"
19+
import (
20+
"k8s.io/api/core/v1"
21+
storage "k8s.io/api/storage/v1"
22+
)
2023

2124
// InTreePlugin handles translations between CSI and in-tree sources in a PV
2225
type InTreePlugin interface {
2326

24-
// TranslateInTreeStorageClassParametersToCSI takes in-tree storage class
25-
// parameters and translates them to a set of parameters consumable by CSI plugin
26-
TranslateInTreeStorageClassParametersToCSI(scParameters map[string]string) (map[string]string, error)
27+
// TranslateInTreeVolumeOptionsToCSI takes in-tree volume options
28+
// and translates them to a volume options consumable by CSI plugin
29+
TranslateInTreeVolumeOptionsToCSI(sc storage.StorageClass) (storage.StorageClass, error)
2730

2831
// TranslateInTreePVToCSI takes a persistent volume and will translate
2932
// the in-tree source to a CSI Source. The input persistent volume can be modified

staging/src/k8s.io/csi-translation-lib/plugins/openstack_cinder.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package plugins
1818

1919
import (
2020
"fmt"
21+
2122
"k8s.io/api/core/v1"
23+
storage "k8s.io/api/storage/v1"
2224
)
2325

2426
const (
@@ -39,8 +41,8 @@ func NewOpenStackCinderCSITranslator() InTreePlugin {
3941
}
4042

4143
// TranslateInTreeStorageClassParametersToCSI translates InTree Cinder storage class parameters to CSI storage class
42-
func (t *osCinderCSITranslator) TranslateInTreeStorageClassParametersToCSI(scParameters map[string]string) (map[string]string, error) {
43-
return scParameters, nil
44+
func (t *osCinderCSITranslator) TranslateInTreeVolumeOptionsToCSI(sc storage.StorageClass) (storage.StorageClass, error) {
45+
return sc, nil
4446
}
4547

4648
// TranslateInTreePVToCSI takes a PV with Cinder set from in-tree

staging/src/k8s.io/csi-translation-lib/translate.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222

2323
"k8s.io/api/core/v1"
24+
storage "k8s.io/api/storage/v1"
2425
"k8s.io/csi-translation-lib/plugins"
2526
)
2627

@@ -32,15 +33,15 @@ var (
3233
}
3334
)
3435

35-
// TranslateInTreeStorageClassParametersToCSI takes in-tree storage class
36-
// parameters and translates them to a set of parameters consumable by CSI plugin
37-
func TranslateInTreeStorageClassParametersToCSI(inTreePluginName string, scParameters map[string]string) (map[string]string, error) {
36+
// TranslateInTreeVolumeOptionsToCSI takes in-tree volume options
37+
// and translates them to a set of parameters consumable by CSI plugin
38+
func TranslateInTreeVolumeOptionsToCSI(inTreePluginName string, sc storage.StorageClass) (storage.StorageClass, error) {
3839
for _, curPlugin := range inTreePlugins {
3940
if inTreePluginName == curPlugin.GetInTreePluginName() {
40-
return curPlugin.TranslateInTreeStorageClassParametersToCSI(scParameters)
41+
return curPlugin.TranslateInTreeVolumeOptionsToCSI(sc)
4142
}
4243
}
43-
return nil, fmt.Errorf("could not find in-tree storage class parameter translation logic for %#v", inTreePluginName)
44+
return storage.StorageClass{}, fmt.Errorf("could not find in-tree storage class parameter translation logic for %#v", inTreePluginName)
4445
}
4546

4647
// TranslateInTreePVToCSI takes a persistent volume and will translate

0 commit comments

Comments
 (0)