github.com/google/cadvisor@v0.49.1/utils/cloudinfo/cloudinfo.go (about)

     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  
    15  // Get information about the cloud provider (if any) cAdvisor is running on.
    16  
    17  package cloudinfo
    18  
    19  import (
    20  	"k8s.io/klog/v2"
    21  
    22  	info "github.com/google/cadvisor/info/v1"
    23  )
    24  
    25  type CloudInfo interface {
    26  	GetCloudProvider() info.CloudProvider
    27  	GetInstanceType() info.InstanceType
    28  	GetInstanceID() info.InstanceID
    29  }
    30  
    31  // CloudProvider is an abstraction for providing cloud-specific information.
    32  type CloudProvider interface {
    33  	// IsActiveProvider determines whether this is the cloud provider operating
    34  	// this instance.
    35  	IsActiveProvider() bool
    36  	// GetInstanceType gets the type of instance this process is running on.
    37  	// The behavior is undefined if this is not the active provider.
    38  	GetInstanceType() info.InstanceType
    39  	// GetInstanceType gets the ID of the instance this process is running on.
    40  	// The behavior is undefined if this is not the active provider.
    41  	GetInstanceID() info.InstanceID
    42  }
    43  
    44  var providers = map[info.CloudProvider]CloudProvider{}
    45  
    46  // RegisterCloudProvider registers the given cloud provider
    47  func RegisterCloudProvider(name info.CloudProvider, provider CloudProvider) {
    48  	if _, alreadyRegistered := providers[name]; alreadyRegistered {
    49  		klog.Warningf("Duplicate registration of CloudProvider %s", name)
    50  	}
    51  	providers[name] = provider
    52  }
    53  
    54  type realCloudInfo struct {
    55  	cloudProvider info.CloudProvider
    56  	instanceType  info.InstanceType
    57  	instanceID    info.InstanceID
    58  }
    59  
    60  func NewRealCloudInfo() CloudInfo {
    61  	for name, provider := range providers {
    62  		if provider.IsActiveProvider() {
    63  			return &realCloudInfo{
    64  				cloudProvider: name,
    65  				instanceType:  provider.GetInstanceType(),
    66  				instanceID:    provider.GetInstanceID(),
    67  			}
    68  		}
    69  	}
    70  
    71  	// No registered active provider.
    72  	return &realCloudInfo{
    73  		cloudProvider: info.UnknownProvider,
    74  		instanceType:  info.UnknownInstance,
    75  		instanceID:    info.UnNamedInstance,
    76  	}
    77  }
    78  
    79  func (i *realCloudInfo) GetCloudProvider() info.CloudProvider {
    80  	return i.cloudProvider
    81  }
    82  
    83  func (i *realCloudInfo) GetInstanceType() info.InstanceType {
    84  	return i.instanceType
    85  }
    86  
    87  func (i *realCloudInfo) GetInstanceID() info.InstanceID {
    88  	return i.instanceID
    89  }