cloudproviders.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 app
  14. import (
  15. "fmt"
  16. "k8s.io/klog"
  17. "k8s.io/client-go/informers"
  18. cloudprovider "k8s.io/cloud-provider"
  19. )
  20. // createCloudProvider helps consolidate what is needed for cloud providers, we explicitly list the things
  21. // that the cloud providers need as parameters, so we can control
  22. func createCloudProvider(cloudProvider string, externalCloudVolumePlugin string, cloudConfigFile string,
  23. allowUntaggedCloud bool, sharedInformers informers.SharedInformerFactory) (cloudprovider.Interface, ControllerLoopMode, error) {
  24. var cloud cloudprovider.Interface
  25. var loopMode ControllerLoopMode
  26. var err error
  27. if cloudprovider.IsExternal(cloudProvider) {
  28. loopMode = ExternalLoops
  29. if externalCloudVolumePlugin == "" {
  30. // externalCloudVolumePlugin is temporary until we split all cloud providers out.
  31. // So we just tell the caller that we need to run ExternalLoops without any cloud provider.
  32. return nil, loopMode, nil
  33. }
  34. cloud, err = cloudprovider.InitCloudProvider(externalCloudVolumePlugin, cloudConfigFile)
  35. } else {
  36. loopMode = IncludeCloudLoops
  37. cloud, err = cloudprovider.InitCloudProvider(cloudProvider, cloudConfigFile)
  38. }
  39. if err != nil {
  40. return nil, loopMode, fmt.Errorf("cloud provider could not be initialized: %v", err)
  41. }
  42. if cloud != nil && !cloud.HasClusterID() {
  43. if allowUntaggedCloud {
  44. klog.Warning("detected a cluster without a ClusterID. A ClusterID will be required in the future. Please tag your cluster to avoid any future issues")
  45. } else {
  46. return nil, loopMode, fmt.Errorf("no ClusterID Found. A ClusterID is required for the cloud provider to function properly. This check can be bypassed by setting the allow-untagged-cloud option")
  47. }
  48. }
  49. if informerUserCloud, ok := cloud.(cloudprovider.InformerUser); ok {
  50. informerUserCloud.SetInformers(sharedInformers)
  51. }
  52. return cloud, loopMode, err
  53. }