plugins.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 cloudprovider
  14. import (
  15. "fmt"
  16. "io"
  17. "os"
  18. "sync"
  19. "github.com/golang/glog"
  20. )
  21. // Factory is a function that returns a cloudprovider.Interface.
  22. // The config parameter provides an io.Reader handler to the factory in
  23. // order to load specific configurations. If no configuration is provided
  24. // the parameter is nil.
  25. type Factory func(config io.Reader) (Interface, error)
  26. // All registered cloud providers.
  27. var providersMutex sync.Mutex
  28. var providers = make(map[string]Factory)
  29. // RegisterCloudProvider registers a cloudprovider.Factory by name. This
  30. // is expected to happen during app startup.
  31. func RegisterCloudProvider(name string, cloud Factory) {
  32. providersMutex.Lock()
  33. defer providersMutex.Unlock()
  34. if _, found := providers[name]; found {
  35. glog.Fatalf("Cloud provider %q was registered twice", name)
  36. }
  37. glog.V(1).Infof("Registered cloud provider %q", name)
  38. providers[name] = cloud
  39. }
  40. // GetCloudProvider creates an instance of the named cloud provider, or nil if
  41. // the name is not known. The error return is only used if the named provider
  42. // was known but failed to initialize. The config parameter specifies the
  43. // io.Reader handler of the configuration file for the cloud provider, or nil
  44. // for no configuation.
  45. func GetCloudProvider(name string, config io.Reader) (Interface, error) {
  46. providersMutex.Lock()
  47. defer providersMutex.Unlock()
  48. f, found := providers[name]
  49. if !found {
  50. return nil, nil
  51. }
  52. return f(config)
  53. }
  54. // InitCloudProvider creates an instance of the named cloud provider.
  55. func InitCloudProvider(name string, configFilePath string) (Interface, error) {
  56. var cloud Interface
  57. var err error
  58. if name == "" {
  59. glog.Info("No cloud provider specified.")
  60. return nil, nil
  61. }
  62. if configFilePath != "" {
  63. var config *os.File
  64. config, err = os.Open(configFilePath)
  65. if err != nil {
  66. glog.Fatalf("Couldn't open cloud provider configuration %s: %#v",
  67. configFilePath, err)
  68. }
  69. defer config.Close()
  70. cloud, err = GetCloudProvider(name, config)
  71. } else {
  72. // Pass explicit nil so plugins can actually check for nil. See
  73. // "Why is my nil error value not equal to nil?" in golang.org/doc/faq.
  74. cloud, err = GetCloudProvider(name, nil)
  75. }
  76. if err != nil {
  77. return nil, fmt.Errorf("could not init cloud provider %q: %v", name, err)
  78. }
  79. if cloud == nil {
  80. return nil, fmt.Errorf("unknown cloud provider %q", name)
  81. }
  82. return cloud, nil
  83. }