Add GetIntreeNameFromCSIName and SupersedesInTreePlugin APIs and unit tests

Signed-off-by: Deep Debroy <ddebroy@docker.com>
This commit is contained in:
Deep Debroy 2019-03-04 15:42:38 -08:00
parent 79e8a29544
commit 1da91ad39a
7 changed files with 81 additions and 7 deletions

View File

@ -1355,7 +1355,7 @@ func (ctrl *PersistentVolumeController) getCSINameFromIntreeName(pluginName stri
if ctrl.csiNameFromIntreeNameHook != nil {
return ctrl.csiNameFromIntreeNameHook(pluginName)
}
return csitranslation.GetCSINameFromIntreeName(pluginName)
return csitranslation.GetCSINameFromInTreeName(pluginName)
}
// provisionClaimOperation provisions a volume. This method is running in

View File

@ -108,3 +108,8 @@ func (t *awsElasticBlockStoreCSITranslator) CanSupport(pv *v1.PersistentVolume)
func (t *awsElasticBlockStoreCSITranslator) GetInTreePluginName() string {
return AWSEBSInTreePluginName
}
// GetCSIPluginName returns the name of the CSI plugin
func (t *awsElasticBlockStoreCSITranslator) GetCSIPluginName() string {
return AWSEBSDriverName
}

View File

@ -149,6 +149,11 @@ func (g *gcePersistentDiskCSITranslator) GetInTreePluginName() string {
return GCEPDInTreePluginName
}
// GetCSIPluginName returns the name of the CSI plugin
func (g *gcePersistentDiskCSITranslator) GetCSIPluginName() string {
return GCEPDDriverName
}
func pdNameFromVolumeID(id string) (string, error) {
splitID := strings.Split(id, "/")
if len(splitID) != volIDTotalElements {

View File

@ -40,4 +40,7 @@ type InTreePlugin interface {
// GetInTreePluginName returns the in-tree plugin name this migrates
GetInTreePluginName() string
// GetCSIPluginName returns the name of the CSI plugin that supersedes the in-tree plugin
GetCSIPluginName() string
}

View File

@ -96,3 +96,8 @@ func (t *osCinderCSITranslator) CanSupport(pv *v1.PersistentVolume) bool {
func (t *osCinderCSITranslator) GetInTreePluginName() string {
return CinderInTreePluginName
}
// GetCSIPluginName returns the name of the CSI plugin
func (t *osCinderCSITranslator) GetCSIPluginName() string {
return CinderDriverName
}

View File

@ -76,18 +76,27 @@ func TranslateCSIPVToInTree(pv *v1.PersistentVolume) (*v1.PersistentVolume, erro
}
// IsMigratableByName tests whether there is Migration logic for the in-tree plugin
// for the given `pluginName`
func IsMigratableByName(pluginName string) bool {
// whose name matches the given inTreePluginName
func IsMigratableByName(inTreePluginName string) bool {
for _, curPlugin := range inTreePlugins {
if curPlugin.GetInTreePluginName() == pluginName {
if curPlugin.GetInTreePluginName() == inTreePluginName {
return true
}
}
return false
}
// GetCSINameFromIntreeName maps the name of a CSI driver to its in-tree version
func GetCSINameFromIntreeName(pluginName string) (string, error) {
// SupersedesInTreePlugin tests whether there exists an in-tree plugin with logic
// to migrate to the CSI plugin with given name
func SupersedesInTreePlugin(csiPluginName string) bool {
if _, ok := inTreePlugins[csiPluginName]; ok {
return true
}
return false
}
// GetCSINameFromInTreeName returns the name of a CSI driver that supersedes the in-tree plugin with the given name
func GetCSINameFromInTreeName(pluginName string) (string, error) {
for csiDriverName, curPlugin := range inTreePlugins {
if curPlugin.GetInTreePluginName() == pluginName {
return csiDriverName, nil
@ -96,7 +105,15 @@ func GetCSINameFromIntreeName(pluginName string) (string, error) {
return "", fmt.Errorf("Could not find CSI Driver name for plugin %v", pluginName)
}
// IsPVMigratable tests whether there is Migration logic for the given Persistent Volume
// GetInTreeNameFromCSIName returns the name of the in-tree plugin superseded by a CSI driver with the given name
func GetInTreeNameFromCSIName(pluginName string) (string, error) {
if plugin, ok := inTreePlugins[pluginName]; ok {
return plugin.GetInTreePluginName(), nil
}
return "", fmt.Errorf("Could not find In-Tree driver name for CSI plugin %v", pluginName)
}
// IsPVMigratable tests whether there is migration logic for the given Persistent Volume
func IsPVMigratable(pv *v1.PersistentVolume) bool {
for _, curPlugin := range inTreePlugins {
if curPlugin.CanSupport(pv) {

View File

@ -76,4 +76,43 @@ func TestTranslationStability(t *testing.T) {
}
}
func TestPluginNameMappings(t *testing.T) {
testCases := []struct {
name string
inTreePluginName string
csiPluginName string
}{
{
name: "GCE PD plugin name",
inTreePluginName: "kubernetes.io/gce-pd",
csiPluginName: "pd.csi.storage.gke.io",
},
{
name: "AWS EBS plugin name",
inTreePluginName: "kubernetes.io/aws-ebs",
csiPluginName: "ebs.csi.aws.com",
},
}
for _, test := range testCases {
t.Logf("Testing %v", test.name)
csiPluginName, err := GetCSINameFromInTreeName(test.inTreePluginName)
if err != nil {
t.Errorf("Error when mapping In-tree plugin name to CSI plugin name %s", err)
}
if !SupersedesInTreePlugin(csiPluginName) {
t.Errorf("%s expected to supersede an In-tree plugin", csiPluginName)
}
inTreePluginName, err := GetInTreeNameFromCSIName(csiPluginName)
if err != nil {
t.Errorf("Error when mapping CSI plugin name to In-tree plugin name %s", err)
}
if !IsMigratableByName(inTreePluginName) {
t.Errorf("%s expected to be migratable to a CSI name", inTreePluginName)
}
if inTreePluginName != test.inTreePluginName || csiPluginName != test.csiPluginName {
t.Errorf("CSI plugin name and In-tree plugin name do not map to each other: [%s => %s], [%s => %s]", test.csiPluginName, inTreePluginName, test.inTreePluginName, csiPluginName)
}
}
}
// TODO: test for not modifying the original PV.