plugin_manager.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package csimigration
  14. import (
  15. "errors"
  16. "fmt"
  17. "k8s.io/api/core/v1"
  18. utilfeature "k8s.io/apiserver/pkg/util/feature"
  19. "k8s.io/component-base/featuregate"
  20. csilibplugins "k8s.io/csi-translation-lib/plugins"
  21. "k8s.io/kubernetes/pkg/features"
  22. "k8s.io/kubernetes/pkg/volume"
  23. )
  24. // PluginNameMapper contains utility methods to retrieve names of plugins
  25. // that support a spec, map intree <=> migrated CSI plugin names, etc
  26. type PluginNameMapper interface {
  27. GetInTreePluginNameFromSpec(pv *v1.PersistentVolume, vol *v1.Volume) (string, error)
  28. GetCSINameFromInTreeName(pluginName string) (string, error)
  29. }
  30. // PluginManager keeps track of migrated state of in-tree plugins
  31. type PluginManager struct {
  32. PluginNameMapper
  33. }
  34. // NewPluginManager returns a new PluginManager instance
  35. func NewPluginManager(m PluginNameMapper) PluginManager {
  36. return PluginManager{
  37. PluginNameMapper: m,
  38. }
  39. }
  40. // IsMigrationCompleteForPlugin indicates whether CSI migration has been completed
  41. // for a particular storage plugin
  42. func (pm PluginManager) IsMigrationCompleteForPlugin(pluginName string) bool {
  43. // CSIMigration feature and plugin specific migration feature flags should
  44. // be enabled for plugin specific migration completion feature flags to be
  45. // take effect
  46. if !pm.IsMigrationEnabledForPlugin(pluginName) {
  47. return false
  48. }
  49. switch pluginName {
  50. case csilibplugins.AWSEBSInTreePluginName:
  51. return utilfeature.DefaultFeatureGate.Enabled(features.CSIMigrationAWSComplete)
  52. case csilibplugins.GCEPDInTreePluginName:
  53. return utilfeature.DefaultFeatureGate.Enabled(features.CSIMigrationGCEComplete)
  54. case csilibplugins.AzureFileInTreePluginName:
  55. return utilfeature.DefaultFeatureGate.Enabled(features.CSIMigrationAzureFileComplete)
  56. case csilibplugins.AzureDiskInTreePluginName:
  57. return utilfeature.DefaultFeatureGate.Enabled(features.CSIMigrationAzureDiskComplete)
  58. case csilibplugins.CinderInTreePluginName:
  59. return utilfeature.DefaultFeatureGate.Enabled(features.CSIMigrationOpenStackComplete)
  60. default:
  61. return false
  62. }
  63. }
  64. // IsMigrationEnabledForPlugin indicates whether CSI migration has been enabled
  65. // for a particular storage plugin
  66. func (pm PluginManager) IsMigrationEnabledForPlugin(pluginName string) bool {
  67. // CSIMigration feature should be enabled along with the plugin-specific one
  68. if !utilfeature.DefaultFeatureGate.Enabled(features.CSIMigration) {
  69. return false
  70. }
  71. switch pluginName {
  72. case csilibplugins.AWSEBSInTreePluginName:
  73. return utilfeature.DefaultFeatureGate.Enabled(features.CSIMigrationAWS)
  74. case csilibplugins.GCEPDInTreePluginName:
  75. return utilfeature.DefaultFeatureGate.Enabled(features.CSIMigrationGCE)
  76. case csilibplugins.AzureFileInTreePluginName:
  77. return utilfeature.DefaultFeatureGate.Enabled(features.CSIMigrationAzureFile)
  78. case csilibplugins.AzureDiskInTreePluginName:
  79. return utilfeature.DefaultFeatureGate.Enabled(features.CSIMigrationAzureDisk)
  80. case csilibplugins.CinderInTreePluginName:
  81. return utilfeature.DefaultFeatureGate.Enabled(features.CSIMigrationOpenStack)
  82. default:
  83. return false
  84. }
  85. }
  86. // IsMigratable indicates whether CSI migration has been enabled for a volume
  87. // plugin that the spec refers to
  88. func (pm PluginManager) IsMigratable(spec *volume.Spec) (bool, error) {
  89. if spec == nil {
  90. return false, fmt.Errorf("could not find if plugin is migratable because volume spec is nil")
  91. }
  92. pluginName, _ := pm.GetInTreePluginNameFromSpec(spec.PersistentVolume, spec.Volume)
  93. if pluginName == "" {
  94. return false, nil
  95. }
  96. // found an in-tree plugin that supports the spec
  97. return pm.IsMigrationEnabledForPlugin(pluginName), nil
  98. }
  99. // InTreeToCSITranslator performs translation of Volume sources for PV and Volume objects
  100. // from references to in-tree plugins to migrated CSI plugins
  101. type InTreeToCSITranslator interface {
  102. TranslateInTreePVToCSI(pv *v1.PersistentVolume) (*v1.PersistentVolume, error)
  103. TranslateInTreeInlineVolumeToCSI(volume *v1.Volume) (*v1.PersistentVolume, error)
  104. }
  105. // TranslateInTreeSpecToCSI translates a volume spec (either PV or inline volume)
  106. // supported by an in-tree plugin to CSI
  107. func TranslateInTreeSpecToCSI(spec *volume.Spec, translator InTreeToCSITranslator) (*volume.Spec, error) {
  108. var csiPV *v1.PersistentVolume
  109. var err error
  110. inlineVolume := false
  111. if spec.PersistentVolume != nil {
  112. csiPV, err = translator.TranslateInTreePVToCSI(spec.PersistentVolume)
  113. } else if spec.Volume != nil {
  114. csiPV, err = translator.TranslateInTreeInlineVolumeToCSI(spec.Volume)
  115. inlineVolume = true
  116. } else {
  117. err = errors.New("not a valid volume spec")
  118. }
  119. if err != nil {
  120. return nil, fmt.Errorf("failed to translate in-tree pv to CSI: %v", err)
  121. }
  122. return &volume.Spec{
  123. PersistentVolume: csiPV,
  124. ReadOnly: spec.ReadOnly,
  125. InlineVolumeSpecForCSIMigration: inlineVolume,
  126. }, nil
  127. }
  128. // CheckMigrationFeatureFlags checks the configuration of feature flags related
  129. // to CSI Migration is valid
  130. func CheckMigrationFeatureFlags(f featuregate.FeatureGate, pluginMigration, pluginMigrationComplete featuregate.Feature) error {
  131. if f.Enabled(pluginMigration) && !f.Enabled(features.CSIMigration) {
  132. return fmt.Errorf("enabling %q requires CSIMigration to be enabled", pluginMigration)
  133. }
  134. if f.Enabled(pluginMigrationComplete) && !f.Enabled(pluginMigration) {
  135. return fmt.Errorf("enabling %q requires %q to be enabled", pluginMigrationComplete, pluginMigration)
  136. }
  137. return nil
  138. }