cloudinfo.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Get information about the cloud provider (if any) cAdvisor is running on.
  15. package cloudinfo
  16. import (
  17. info "github.com/google/cadvisor/info/v1"
  18. "k8s.io/klog"
  19. )
  20. type CloudInfo interface {
  21. GetCloudProvider() info.CloudProvider
  22. GetInstanceType() info.InstanceType
  23. GetInstanceID() info.InstanceID
  24. }
  25. // CloudProvider is an abstraction for providing cloud-specific information.
  26. type CloudProvider interface {
  27. // IsActiveProvider determines whether this is the cloud provider operating
  28. // this instance.
  29. IsActiveProvider() bool
  30. // GetInstanceType gets the type of instance this process is running on.
  31. // The behavior is undefined if this is not the active provider.
  32. GetInstanceType() info.InstanceType
  33. // GetInstanceType gets the ID of the instance this process is running on.
  34. // The behavior is undefined if this is not the active provider.
  35. GetInstanceID() info.InstanceID
  36. }
  37. var providers = map[info.CloudProvider]CloudProvider{}
  38. // RegisterCloudProvider registers the given cloud provider
  39. func RegisterCloudProvider(name info.CloudProvider, provider CloudProvider) {
  40. if _, alreadyRegistered := providers[name]; alreadyRegistered {
  41. klog.Warningf("Duplicate registration of CloudProvider %s", name)
  42. }
  43. providers[name] = provider
  44. }
  45. type realCloudInfo struct {
  46. cloudProvider info.CloudProvider
  47. instanceType info.InstanceType
  48. instanceID info.InstanceID
  49. }
  50. func NewRealCloudInfo() CloudInfo {
  51. for name, provider := range providers {
  52. if provider.IsActiveProvider() {
  53. return &realCloudInfo{
  54. cloudProvider: name,
  55. instanceType: provider.GetInstanceType(),
  56. instanceID: provider.GetInstanceID(),
  57. }
  58. }
  59. }
  60. // No registered active provider.
  61. return &realCloudInfo{
  62. cloudProvider: info.UnknownProvider,
  63. instanceType: info.UnknownInstance,
  64. instanceID: info.UnNamedInstance,
  65. }
  66. }
  67. func (self *realCloudInfo) GetCloudProvider() info.CloudProvider {
  68. return self.cloudProvider
  69. }
  70. func (self *realCloudInfo) GetInstanceType() info.InstanceType {
  71. return self.instanceType
  72. }
  73. func (self *realCloudInfo) GetInstanceID() info.InstanceID {
  74. return self.instanceID
  75. }