plugins.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright 2014 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 app
  14. import (
  15. // This file exists to force the desired plugin implementations to be linked.
  16. // This should probably be part of some configuration fed into the build for a
  17. // given binary target.
  18. "fmt"
  19. // Cloud providers
  20. "k8s.io/kubernetes/pkg/apis/componentconfig"
  21. _ "k8s.io/kubernetes/pkg/cloudprovider/providers"
  22. // Volume plugins
  23. "github.com/golang/glog"
  24. "k8s.io/kubernetes/pkg/cloudprovider"
  25. "k8s.io/kubernetes/pkg/cloudprovider/providers/aws"
  26. "k8s.io/kubernetes/pkg/cloudprovider/providers/gce"
  27. "k8s.io/kubernetes/pkg/cloudprovider/providers/openstack"
  28. "k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere"
  29. utilconfig "k8s.io/kubernetes/pkg/util/config"
  30. "k8s.io/kubernetes/pkg/util/io"
  31. "k8s.io/kubernetes/pkg/volume"
  32. "k8s.io/kubernetes/pkg/volume/aws_ebs"
  33. "k8s.io/kubernetes/pkg/volume/azure_dd"
  34. "k8s.io/kubernetes/pkg/volume/cinder"
  35. "k8s.io/kubernetes/pkg/volume/flexvolume"
  36. "k8s.io/kubernetes/pkg/volume/gce_pd"
  37. "k8s.io/kubernetes/pkg/volume/glusterfs"
  38. "k8s.io/kubernetes/pkg/volume/host_path"
  39. "k8s.io/kubernetes/pkg/volume/nfs"
  40. "k8s.io/kubernetes/pkg/volume/vsphere_volume"
  41. )
  42. // ProbeAttachableVolumePlugins collects all volume plugins for the attach/
  43. // detach controller. VolumeConfiguration is used ot get FlexVolumePluginDir
  44. // which specifies the directory to search for additional third party volume
  45. // plugins.
  46. // The list of plugins is manually compiled. This code and the plugin
  47. // initialization code for kubelet really, really need a through refactor.
  48. func ProbeAttachableVolumePlugins(config componentconfig.VolumeConfiguration) []volume.VolumePlugin {
  49. allPlugins := []volume.VolumePlugin{}
  50. allPlugins = append(allPlugins, aws_ebs.ProbeVolumePlugins()...)
  51. allPlugins = append(allPlugins, gce_pd.ProbeVolumePlugins()...)
  52. allPlugins = append(allPlugins, cinder.ProbeVolumePlugins()...)
  53. allPlugins = append(allPlugins, flexvolume.ProbeVolumePlugins(config.FlexVolumePluginDir)...)
  54. allPlugins = append(allPlugins, vsphere_volume.ProbeVolumePlugins()...)
  55. allPlugins = append(allPlugins, azure_dd.ProbeVolumePlugins()...)
  56. return allPlugins
  57. }
  58. // ProbeControllerVolumePlugins collects all persistent volume plugins into an
  59. // easy to use list. Only volume plugins that implement any of
  60. // provisioner/recycler/deleter interface should be returned.
  61. func ProbeControllerVolumePlugins(cloud cloudprovider.Interface, config componentconfig.VolumeConfiguration) []volume.VolumePlugin {
  62. allPlugins := []volume.VolumePlugin{}
  63. // The list of plugins to probe is decided by this binary, not
  64. // by dynamic linking or other "magic". Plugins will be analyzed and
  65. // initialized later.
  66. // Each plugin can make use of VolumeConfig. The single arg to this func contains *all* enumerated
  67. // options meant to configure volume plugins. From that single config, create an instance of volume.VolumeConfig
  68. // for a specific plugin and pass that instance to the plugin's ProbeVolumePlugins(config) func.
  69. // HostPath recycling is for testing and development purposes only!
  70. hostPathConfig := volume.VolumeConfig{
  71. RecyclerMinimumTimeout: int(config.PersistentVolumeRecyclerConfiguration.MinimumTimeoutHostPath),
  72. RecyclerTimeoutIncrement: int(config.PersistentVolumeRecyclerConfiguration.IncrementTimeoutHostPath),
  73. RecyclerPodTemplate: volume.NewPersistentVolumeRecyclerPodTemplate(),
  74. ProvisioningEnabled: config.EnableHostPathProvisioning,
  75. }
  76. if err := AttemptToLoadRecycler(config.PersistentVolumeRecyclerConfiguration.PodTemplateFilePathHostPath, &hostPathConfig); err != nil {
  77. glog.Fatalf("Could not create hostpath recycler pod from file %s: %+v", config.PersistentVolumeRecyclerConfiguration.PodTemplateFilePathHostPath, err)
  78. }
  79. allPlugins = append(allPlugins, host_path.ProbeVolumePlugins(hostPathConfig)...)
  80. nfsConfig := volume.VolumeConfig{
  81. RecyclerMinimumTimeout: int(config.PersistentVolumeRecyclerConfiguration.MinimumTimeoutNFS),
  82. RecyclerTimeoutIncrement: int(config.PersistentVolumeRecyclerConfiguration.IncrementTimeoutNFS),
  83. RecyclerPodTemplate: volume.NewPersistentVolumeRecyclerPodTemplate(),
  84. }
  85. if err := AttemptToLoadRecycler(config.PersistentVolumeRecyclerConfiguration.PodTemplateFilePathNFS, &nfsConfig); err != nil {
  86. glog.Fatalf("Could not create NFS recycler pod from file %s: %+v", config.PersistentVolumeRecyclerConfiguration.PodTemplateFilePathNFS, err)
  87. }
  88. allPlugins = append(allPlugins, nfs.ProbeVolumePlugins(nfsConfig)...)
  89. allPlugins = append(allPlugins, glusterfs.ProbeVolumePlugins()...)
  90. if cloud != nil {
  91. switch {
  92. case aws.ProviderName == cloud.ProviderName():
  93. allPlugins = append(allPlugins, aws_ebs.ProbeVolumePlugins()...)
  94. case gce.ProviderName == cloud.ProviderName():
  95. allPlugins = append(allPlugins, gce_pd.ProbeVolumePlugins()...)
  96. case openstack.ProviderName == cloud.ProviderName():
  97. allPlugins = append(allPlugins, cinder.ProbeVolumePlugins()...)
  98. case vsphere.ProviderName == cloud.ProviderName():
  99. allPlugins = append(allPlugins, vsphere_volume.ProbeVolumePlugins()...)
  100. }
  101. }
  102. return allPlugins
  103. }
  104. // NewAlphaVolumeProvisioner returns a volume provisioner to use when running in
  105. // a cloud or development environment. The alpha implementation of provisioning
  106. // allows 1 implied provisioner per cloud and is here only for compatibility
  107. // with Kubernetes 1.3
  108. // TODO: remove in Kubernetes 1.5
  109. func NewAlphaVolumeProvisioner(cloud cloudprovider.Interface, config componentconfig.VolumeConfiguration) (volume.ProvisionableVolumePlugin, error) {
  110. switch {
  111. case !utilconfig.DefaultFeatureGate.DynamicVolumeProvisioning():
  112. return nil, nil
  113. case cloud == nil && config.EnableHostPathProvisioning:
  114. return getProvisionablePluginFromVolumePlugins(host_path.ProbeVolumePlugins(
  115. volume.VolumeConfig{
  116. ProvisioningEnabled: true,
  117. }))
  118. case cloud != nil && aws.ProviderName == cloud.ProviderName():
  119. return getProvisionablePluginFromVolumePlugins(aws_ebs.ProbeVolumePlugins())
  120. case cloud != nil && gce.ProviderName == cloud.ProviderName():
  121. return getProvisionablePluginFromVolumePlugins(gce_pd.ProbeVolumePlugins())
  122. case cloud != nil && openstack.ProviderName == cloud.ProviderName():
  123. return getProvisionablePluginFromVolumePlugins(cinder.ProbeVolumePlugins())
  124. case cloud != nil && vsphere.ProviderName == cloud.ProviderName():
  125. return getProvisionablePluginFromVolumePlugins(vsphere_volume.ProbeVolumePlugins())
  126. }
  127. return nil, nil
  128. }
  129. func getProvisionablePluginFromVolumePlugins(plugins []volume.VolumePlugin) (volume.ProvisionableVolumePlugin, error) {
  130. for _, plugin := range plugins {
  131. if provisonablePlugin, ok := plugin.(volume.ProvisionableVolumePlugin); ok {
  132. return provisonablePlugin, nil
  133. }
  134. }
  135. return nil, fmt.Errorf("ProvisionablePlugin expected but not found in %#v: ", plugins)
  136. }
  137. // AttemptToLoadRecycler tries decoding a pod from a filepath for use as a recycler for a volume.
  138. // If successful, this method will set the recycler on the config.
  139. // If unsuccessful, an error is returned. Function is exported for reuse downstream.
  140. func AttemptToLoadRecycler(path string, config *volume.VolumeConfig) error {
  141. if path != "" {
  142. recyclerPod, err := io.LoadPodFromFile(path)
  143. if err != nil {
  144. return err
  145. }
  146. config.RecyclerPodTemplate = recyclerPod
  147. }
  148. return nil
  149. }