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.
This commit is contained in:
David Zhu 2019-05-13 13:20:06 -07:00
parent 0252a32342
commit 196bbaa964
8 changed files with 110 additions and 16 deletions

View File

@ -8,6 +8,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/api/storage/v1:go_default_library",
"//staging/src/k8s.io/csi-translation-lib/plugins:go_default_library",
],
)

View File

@ -13,6 +13,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//staging/src/k8s.io/api/storage/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/cloud-provider/volume:go_default_library",
],
@ -34,6 +35,10 @@ filegroup(
go_test(
name = "go_default_test",
srcs = ["aws_ebs_test.go"],
srcs = [
"aws_ebs_test.go",
"gce_pd_test.go",
],
embed = [":go_default_library"],
deps = ["//staging/src/k8s.io/api/storage/v1:go_default_library"],
)

View File

@ -24,6 +24,7 @@ import (
"strings"
"k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1"
)
const (
@ -44,8 +45,8 @@ func NewAWSElasticBlockStoreCSITranslator() InTreePlugin {
}
// TranslateInTreeStorageClassParametersToCSI translates InTree EBS storage class parameters to CSI storage class
func (t *awsElasticBlockStoreCSITranslator) TranslateInTreeStorageClassParametersToCSI(scParameters map[string]string) (map[string]string, error) {
return scParameters, nil
func (t *awsElasticBlockStoreCSITranslator) TranslateInTreeVolumeOptionsToCSI(sc storage.StorageClass) (storage.StorageClass, error) {
return sc, nil
}
// TranslateInTreePVToCSI takes a PV with AWSElasticBlockStore set from in-tree

View File

@ -22,6 +22,7 @@ import (
"strings"
"k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/util/sets"
cloudvolume "k8s.io/cloud-provider/volume"
)
@ -56,8 +57,21 @@ func NewGCEPersistentDiskCSITranslator() InTreePlugin {
}
// TranslateInTreeStorageClassParametersToCSI translates InTree GCE storage class parameters to CSI storage class
func (g *gcePersistentDiskCSITranslator) TranslateInTreeStorageClassParametersToCSI(scParameters map[string]string) (map[string]string, error) {
return scParameters, nil
func (g *gcePersistentDiskCSITranslator) TranslateInTreeVolumeOptionsToCSI(sc storage.StorageClass) (storage.StorageClass, error) {
np := map[string]string{}
for k, v := range sc.Parameters {
switch strings.ToLower(k) {
case "fstype":
np["csi.storage.k8s.io/fstype"] = v
default:
np[k] = v
}
}
sc.Parameters = np
// TODO(#77235): Translate AccessModes and zone/zones to AccessibleTopologies
return sc, nil
}
// TranslateInTreePVToCSI takes a PV with GCEPersistentDisk set from in-tree

View File

@ -0,0 +1,67 @@
/*
Copyright 2019 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"
storage "k8s.io/api/storage/v1"
)
func NewStorageClass(params map[string]string) storage.StorageClass {
return storage.StorageClass{
Parameters: params,
}
}
func TestTranslatePDInTreeVolumeOptionsToCSI(t *testing.T) {
g := NewGCEPersistentDiskCSITranslator()
tcs := []struct {
name string
options storage.StorageClass
expOptions storage.StorageClass
}{
{
name: "nothing special",
options: NewStorageClass(map[string]string{"foo": "bar"}),
expOptions: NewStorageClass(map[string]string{"foo": "bar"}),
},
{
name: "fstype",
options: NewStorageClass(map[string]string{"fstype": "myfs"}),
expOptions: NewStorageClass(map[string]string{"csi.storage.k8s.io/fstype": "myfs"}),
},
{
name: "empty params",
options: NewStorageClass(map[string]string{}),
expOptions: NewStorageClass(map[string]string{}),
},
}
for _, tc := range tcs {
t.Logf("Testing %v", tc.name)
gotOptions, err := g.TranslateInTreeVolumeOptionsToCSI(tc.options)
if err != nil {
t.Errorf("Did not expect error but got: %v", err)
}
if !reflect.DeepEqual(gotOptions, tc.expOptions) {
t.Errorf("Got parameters: %v, expected :%v", gotOptions, tc.expOptions)
}
}
}

View File

@ -16,14 +16,17 @@ limitations under the License.
package plugins
import "k8s.io/api/core/v1"
import (
"k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1"
)
// InTreePlugin handles translations between CSI and in-tree sources in a PV
type InTreePlugin interface {
// TranslateInTreeStorageClassParametersToCSI takes in-tree storage class
// parameters and translates them to a set of parameters consumable by CSI plugin
TranslateInTreeStorageClassParametersToCSI(scParameters map[string]string) (map[string]string, error)
// TranslateInTreeVolumeOptionsToCSI takes in-tree volume options
// and translates them to a volume options consumable by CSI plugin
TranslateInTreeVolumeOptionsToCSI(sc storage.StorageClass) (storage.StorageClass, error)
// TranslateInTreePVToCSI takes a persistent volume and will translate
// the in-tree source to a CSI Source. The input persistent volume can be modified

View File

@ -18,7 +18,9 @@ package plugins
import (
"fmt"
"k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1"
)
const (
@ -39,8 +41,8 @@ func NewOpenStackCinderCSITranslator() InTreePlugin {
}
// TranslateInTreeStorageClassParametersToCSI translates InTree Cinder storage class parameters to CSI storage class
func (t *osCinderCSITranslator) TranslateInTreeStorageClassParametersToCSI(scParameters map[string]string) (map[string]string, error) {
return scParameters, nil
func (t *osCinderCSITranslator) TranslateInTreeVolumeOptionsToCSI(sc storage.StorageClass) (storage.StorageClass, error) {
return sc, nil
}
// TranslateInTreePVToCSI takes a PV with Cinder set from in-tree

View File

@ -21,6 +21,7 @@ import (
"fmt"
"k8s.io/api/core/v1"
storage "k8s.io/api/storage/v1"
"k8s.io/csi-translation-lib/plugins"
)
@ -32,15 +33,15 @@ var (
}
)
// TranslateInTreeStorageClassParametersToCSI takes in-tree storage class
// parameters and translates them to a set of parameters consumable by CSI plugin
func TranslateInTreeStorageClassParametersToCSI(inTreePluginName string, scParameters map[string]string) (map[string]string, error) {
// TranslateInTreeVolumeOptionsToCSI takes in-tree volume options
// and translates them to a set of parameters consumable by CSI plugin
func TranslateInTreeVolumeOptionsToCSI(inTreePluginName string, sc storage.StorageClass) (storage.StorageClass, error) {
for _, curPlugin := range inTreePlugins {
if inTreePluginName == curPlugin.GetInTreePluginName() {
return curPlugin.TranslateInTreeStorageClassParametersToCSI(scParameters)
return curPlugin.TranslateInTreeVolumeOptionsToCSI(sc)
}
}
return nil, fmt.Errorf("could not find in-tree storage class parameter translation logic for %#v", inTreePluginName)
return storage.StorageClass{}, fmt.Errorf("could not find in-tree storage class parameter translation logic for %#v", inTreePluginName)
}
// TranslateInTreePVToCSI takes a persistent volume and will translate