provider.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. Copyright 2018 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 framework
  14. import (
  15. "fmt"
  16. "os"
  17. "sync"
  18. "github.com/pkg/errors"
  19. "k8s.io/api/core/v1"
  20. clientset "k8s.io/client-go/kubernetes"
  21. )
  22. // Factory is a func which operates provider specific behavior.
  23. type Factory func() (ProviderInterface, error)
  24. var (
  25. providers = make(map[string]Factory)
  26. mutex sync.Mutex
  27. )
  28. // RegisterProvider is expected to be called during application init,
  29. // typically by an init function in a provider package.
  30. func RegisterProvider(name string, factory Factory) {
  31. mutex.Lock()
  32. defer mutex.Unlock()
  33. if _, ok := providers[name]; ok {
  34. panic(fmt.Sprintf("provider %s already registered", name))
  35. }
  36. providers[name] = factory
  37. }
  38. // GetProviders returns the names of all currently registered providers.
  39. func GetProviders() []string {
  40. mutex.Lock()
  41. defer mutex.Unlock()
  42. var providerNames []string
  43. for name := range providers {
  44. providerNames = append(providerNames, name)
  45. }
  46. return providerNames
  47. }
  48. func init() {
  49. // "local" or "skeleton" can always be used.
  50. RegisterProvider("local", func() (ProviderInterface, error) {
  51. return NullProvider{}, nil
  52. })
  53. RegisterProvider("skeleton", func() (ProviderInterface, error) {
  54. return NullProvider{}, nil
  55. })
  56. // The empty string used to be accepted in the past, but is not
  57. // a valid value anymore.
  58. }
  59. // SetupProviderConfig validates the chosen provider and creates
  60. // an interface instance for it.
  61. func SetupProviderConfig(providerName string) (ProviderInterface, error) {
  62. var err error
  63. mutex.Lock()
  64. defer mutex.Unlock()
  65. factory, ok := providers[providerName]
  66. if !ok {
  67. return nil, errors.Wrapf(os.ErrNotExist, "The provider %s is unknown.", providerName)
  68. }
  69. provider, err := factory()
  70. return provider, err
  71. }
  72. // ProviderInterface contains the implementation for certain
  73. // provider-specific functionality.
  74. type ProviderInterface interface {
  75. FrameworkBeforeEach(f *Framework)
  76. FrameworkAfterEach(f *Framework)
  77. ResizeGroup(group string, size int32) error
  78. GetGroupNodes(group string) ([]string, error)
  79. GroupSize(group string) (int, error)
  80. DeleteNode(node *v1.Node) error
  81. CreatePD(zone string) (string, error)
  82. DeletePD(pdName string) error
  83. CreatePVSource(zone, diskName string) (*v1.PersistentVolumeSource, error)
  84. DeletePVSource(pvSource *v1.PersistentVolumeSource) error
  85. CleanupServiceResources(c clientset.Interface, loadBalancerName, region, zone string)
  86. EnsureLoadBalancerResourcesDeleted(ip, portRange string) error
  87. LoadBalancerSrcRanges() []string
  88. EnableAndDisableInternalLB() (enable, disable func(svc *v1.Service))
  89. }
  90. // NullProvider is the default implementation of the ProviderInterface
  91. // which doesn't do anything.
  92. type NullProvider struct{}
  93. // FrameworkBeforeEach is a base implementation which does BeforeEach.
  94. func (n NullProvider) FrameworkBeforeEach(f *Framework) {}
  95. // FrameworkAfterEach is a base implementation which does AfterEach.
  96. func (n NullProvider) FrameworkAfterEach(f *Framework) {}
  97. // ResizeGroup is a base implementation which resizes group.
  98. func (n NullProvider) ResizeGroup(string, int32) error {
  99. return fmt.Errorf("Provider does not support InstanceGroups")
  100. }
  101. // GetGroupNodes is a base implementation which returns group nodes.
  102. func (n NullProvider) GetGroupNodes(group string) ([]string, error) {
  103. return nil, fmt.Errorf("provider does not support InstanceGroups")
  104. }
  105. // GroupSize returns the size of an instance group
  106. func (n NullProvider) GroupSize(group string) (int, error) {
  107. return -1, fmt.Errorf("provider does not support InstanceGroups")
  108. }
  109. // DeleteNode is a base implementation which deletes a node.
  110. func (n NullProvider) DeleteNode(node *v1.Node) error {
  111. return fmt.Errorf("provider does not support DeleteNode")
  112. }
  113. // CreatePD is a base implementation which creates PD.
  114. func (n NullProvider) CreatePD(zone string) (string, error) {
  115. return "", fmt.Errorf("provider does not support volume creation")
  116. }
  117. // DeletePD is a base implementation which deletes PD.
  118. func (n NullProvider) DeletePD(pdName string) error {
  119. return fmt.Errorf("provider does not support volume deletion")
  120. }
  121. // CreatePVSource is a base implementation which creates PV source.
  122. func (n NullProvider) CreatePVSource(zone, diskName string) (*v1.PersistentVolumeSource, error) {
  123. return nil, fmt.Errorf("Provider not supported")
  124. }
  125. // DeletePVSource is a base implementation which deletes PV source.
  126. func (n NullProvider) DeletePVSource(pvSource *v1.PersistentVolumeSource) error {
  127. return fmt.Errorf("Provider not supported")
  128. }
  129. // CleanupServiceResources is a base implementation which cleans up service resources.
  130. func (n NullProvider) CleanupServiceResources(c clientset.Interface, loadBalancerName, region, zone string) {
  131. }
  132. // EnsureLoadBalancerResourcesDeleted is a base implementation which ensures load balancer is deleted.
  133. func (n NullProvider) EnsureLoadBalancerResourcesDeleted(ip, portRange string) error {
  134. return nil
  135. }
  136. // LoadBalancerSrcRanges is a base implementation which returns the ranges of ips used by load balancers.
  137. func (n NullProvider) LoadBalancerSrcRanges() []string {
  138. return nil
  139. }
  140. // EnableAndDisableInternalLB is a base implementation which returns functions for enabling/disabling an internal LB.
  141. func (n NullProvider) EnableAndDisableInternalLB() (enable, disable func(svc *v1.Service)) {
  142. nop := func(svc *v1.Service) {}
  143. return nop, nop
  144. }
  145. var _ ProviderInterface = NullProvider{}