Skip to content

Commit 4ca6c21

Browse files
authored
Merge pull request #2213 from tallclair/cloud
Remove CloudProvider SDK dependencies
2 parents 68532f6 + c492e4a commit 4ca6c21

File tree

5 files changed

+102
-85
lines changed

5 files changed

+102
-85
lines changed

cadvisor.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package main
1616

1717
import (
18+
"crypto/tls"
1819
"flag"
1920
"fmt"
2021
"net/http"
@@ -29,12 +30,14 @@ import (
2930
"github.com/google/cadvisor/container"
3031
cadvisorhttp "github.com/google/cadvisor/http"
3132
"github.com/google/cadvisor/manager"
33+
"github.com/google/cadvisor/metrics"
3234
"github.com/google/cadvisor/utils/sysfs"
3335
"github.com/google/cadvisor/version"
3436

35-
"crypto/tls"
36-
37-
"github.com/google/cadvisor/metrics"
37+
// Register CloudProviders
38+
_ "github.com/google/cadvisor/utils/cloudinfo/aws"
39+
_ "github.com/google/cadvisor/utils/cloudinfo/azure"
40+
_ "github.com/google/cadvisor/utils/cloudinfo/gce"
3841

3942
"k8s.io/klog"
4043
)
@@ -147,7 +150,7 @@ func main() {
147150

148151
collectorHttpClient := createCollectorHttpClient(*collectorCert, *collectorKey)
149152

150-
containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, includedMetrics, &collectorHttpClient, strings.Split(*rawCgroupPrefixWhiteList,","))
153+
containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, includedMetrics, &collectorHttpClient, strings.Split(*rawCgroupPrefixWhiteList, ","))
151154
if err != nil {
152155
klog.Fatalf("Failed to create a Container Manager: %s", err)
153156
}

utils/cloudinfo/aws.go renamed to utils/cloudinfo/aws/aws.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,50 @@
1515
package cloudinfo
1616

1717
import (
18-
"github.com/aws/aws-sdk-go/aws"
19-
"github.com/aws/aws-sdk-go/aws/ec2metadata"
20-
"github.com/aws/aws-sdk-go/aws/session"
2118
"io/ioutil"
2219
"os"
2320
"strings"
2421

22+
"github.com/aws/aws-sdk-go/aws"
23+
"github.com/aws/aws-sdk-go/aws/ec2metadata"
24+
"github.com/aws/aws-sdk-go/aws/session"
25+
2526
info "github.com/google/cadvisor/info/v1"
27+
"github.com/google/cadvisor/utils/cloudinfo"
2628
)
2729

2830
const (
29-
ProductVerFileName = "/sys/class/dmi/id/product_version"
30-
BiosVerFileName = "/sys/class/dmi/id/bios_vendor"
31-
Amazon = "amazon"
31+
productVerFileName = "/sys/class/dmi/id/product_version"
32+
biosVerFileName = "/sys/class/dmi/id/bios_vendor"
33+
amazon = "amazon"
3234
)
3335

34-
func onAWS() bool {
36+
func init() {
37+
cloudinfo.RegisterCloudProvider(info.AWS, &provider{})
38+
}
39+
40+
type provider struct{}
41+
42+
var _ cloudinfo.CloudProvider = provider{}
43+
44+
func (provider) IsActiveProvider() bool {
3545
var dataProduct []byte
3646
var dataBios []byte
37-
if _, err := os.Stat(ProductVerFileName); err == nil {
38-
dataProduct, err = ioutil.ReadFile(ProductVerFileName)
47+
if _, err := os.Stat(productVerFileName); err == nil {
48+
dataProduct, err = ioutil.ReadFile(productVerFileName)
3949
if err != nil {
4050
return false
4151
}
4252
}
4353

44-
if _, err := os.Stat(BiosVerFileName); err == nil {
45-
dataBios, err = ioutil.ReadFile(BiosVerFileName)
54+
if _, err := os.Stat(biosVerFileName); err == nil {
55+
dataBios, err = ioutil.ReadFile(biosVerFileName)
4656
if err != nil {
4757
return false
4858
}
4959
}
5060

51-
return strings.Contains(string(dataProduct), Amazon) || strings.Contains(strings.ToLower(string(dataBios)), Amazon)
61+
return strings.Contains(string(dataProduct), amazon) || strings.Contains(strings.ToLower(string(dataBios)), amazon)
5262
}
5363

5464
func getAwsMetadata(name string) string {
@@ -60,10 +70,10 @@ func getAwsMetadata(name string) string {
6070
return data
6171
}
6272

63-
func getAwsInstanceType() info.InstanceType {
73+
func (provider) GetInstanceType() info.InstanceType {
6474
return info.InstanceType(getAwsMetadata("instance-type"))
6575
}
6676

67-
func getAwsInstanceID() info.InstanceID {
77+
func (provider) GetInstanceID() info.InstanceID {
6878
return info.InstanceID(getAwsMetadata("instance-id"))
6979
}

utils/cloudinfo/azure.go renamed to utils/cloudinfo/azure/azure.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,42 @@
1515
package cloudinfo
1616

1717
import (
18-
info "github.com/google/cadvisor/info/v1"
1918
"io/ioutil"
2019
"strings"
20+
21+
info "github.com/google/cadvisor/info/v1"
22+
"github.com/google/cadvisor/utils/cloudinfo"
2123
)
2224

2325
const (
24-
SysVendorFileName = "/sys/class/dmi/id/sys_vendor"
25-
BiosUUIDFileName = "/sys/class/dmi/id/product_uuid"
26-
MicrosoftCorporation = "Microsoft Corporation"
26+
sysVendorFileName = "/sys/class/dmi/id/sys_vendor"
27+
biosUUIDFileName = "/sys/class/dmi/id/product_uuid"
28+
microsoftCorporation = "Microsoft Corporation"
2729
)
2830

29-
func onAzure() bool {
30-
data, err := ioutil.ReadFile(SysVendorFileName)
31+
func init() {
32+
cloudinfo.RegisterCloudProvider(info.Azure, &provider{})
33+
}
34+
35+
type provider struct{}
36+
37+
var _ cloudinfo.CloudProvider = provider{}
38+
39+
func (provider) IsActiveProvider() bool {
40+
data, err := ioutil.ReadFile(sysVendorFileName)
3141
if err != nil {
3242
return false
3343
}
34-
return strings.Contains(string(data), MicrosoftCorporation)
44+
return strings.Contains(string(data), microsoftCorporation)
3545
}
3646

3747
// TODO: Implement method.
38-
func getAzureInstanceType() info.InstanceType {
48+
func (provider) GetInstanceType() info.InstanceType {
3949
return info.UnknownInstance
4050
}
4151

42-
func getAzureInstanceID() info.InstanceID {
43-
data, err := ioutil.ReadFile(BiosUUIDFileName)
52+
func (provider) GetInstanceID() info.InstanceID {
53+
data, err := ioutil.ReadFile(biosUUIDFileName)
4454
if err != nil {
4555
return info.UnNamedInstance
4656
}

utils/cloudinfo/cloudinfo.go

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cloudinfo
1818

1919
import (
2020
info "github.com/google/cadvisor/info/v1"
21+
"k8s.io/klog"
2122
)
2223

2324
type CloudInfo interface {
@@ -26,20 +27,51 @@ type CloudInfo interface {
2627
GetInstanceID() info.InstanceID
2728
}
2829

30+
// CloudProvider is an abstraction for providing cloud-specific information.
31+
type CloudProvider interface {
32+
// IsActiveProvider determines whether this is the cloud provider operating
33+
// this instance.
34+
IsActiveProvider() bool
35+
// GetInstanceType gets the type of instance this process is running on.
36+
// The behavior is undefined if this is not the active provider.
37+
GetInstanceType() info.InstanceType
38+
// GetInstanceType gets the ID of the instance this process is running on.
39+
// The behavior is undefined if this is not the active provider.
40+
GetInstanceID() info.InstanceID
41+
}
42+
43+
var providers = map[info.CloudProvider]CloudProvider{}
44+
45+
// RegisterCloudProvider registers the given cloud provider
46+
func RegisterCloudProvider(name info.CloudProvider, provider CloudProvider) {
47+
if _, alreadyRegistered := providers[name]; alreadyRegistered {
48+
klog.Warningf("Duplicate registration of CloudProvider %s", name)
49+
}
50+
providers[name] = provider
51+
}
52+
2953
type realCloudInfo struct {
3054
cloudProvider info.CloudProvider
3155
instanceType info.InstanceType
3256
instanceID info.InstanceID
3357
}
3458

3559
func NewRealCloudInfo() CloudInfo {
36-
cloudProvider := detectCloudProvider()
37-
instanceType := detectInstanceType(cloudProvider)
38-
instanceID := detectInstanceID(cloudProvider)
60+
for name, provider := range providers {
61+
if provider.IsActiveProvider() {
62+
return &realCloudInfo{
63+
cloudProvider: name,
64+
instanceType: provider.GetInstanceType(),
65+
instanceID: provider.GetInstanceID(),
66+
}
67+
}
68+
}
69+
70+
// No registered active provider.
3971
return &realCloudInfo{
40-
cloudProvider: cloudProvider,
41-
instanceType: instanceType,
42-
instanceID: instanceID,
72+
cloudProvider: info.UnknownProvider,
73+
instanceType: info.UnknownInstance,
74+
instanceID: info.UnNamedInstance,
4375
}
4476
}
4577

@@ -54,50 +86,3 @@ func (self *realCloudInfo) GetInstanceType() info.InstanceType {
5486
func (self *realCloudInfo) GetInstanceID() info.InstanceID {
5587
return self.instanceID
5688
}
57-
58-
func detectCloudProvider() info.CloudProvider {
59-
switch {
60-
case onGCE():
61-
return info.GCE
62-
case onAWS():
63-
return info.AWS
64-
case onAzure():
65-
return info.Azure
66-
case onBaremetal():
67-
return info.Baremetal
68-
}
69-
return info.UnknownProvider
70-
}
71-
72-
func detectInstanceType(cloudProvider info.CloudProvider) info.InstanceType {
73-
switch cloudProvider {
74-
case info.GCE:
75-
return getGceInstanceType()
76-
case info.AWS:
77-
return getAwsInstanceType()
78-
case info.Azure:
79-
return getAzureInstanceType()
80-
case info.Baremetal:
81-
return info.NoInstance
82-
}
83-
return info.UnknownInstance
84-
}
85-
86-
func detectInstanceID(cloudProvider info.CloudProvider) info.InstanceID {
87-
switch cloudProvider {
88-
case info.GCE:
89-
return getGceInstanceID()
90-
case info.AWS:
91-
return getAwsInstanceID()
92-
case info.Azure:
93-
return getAzureInstanceID()
94-
case info.Baremetal:
95-
return info.UnNamedInstance
96-
}
97-
return info.UnNamedInstance
98-
}
99-
100-
// TODO: Implement method.
101-
func onBaremetal() bool {
102-
return false
103-
}

utils/cloudinfo/gce.go renamed to utils/cloudinfo/gce/gce.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package cloudinfo
15+
package gce
1616

1717
import (
1818
"io/ioutil"
1919
"strings"
2020

2121
info "github.com/google/cadvisor/info/v1"
22+
"github.com/google/cadvisor/utils/cloudinfo"
2223

2324
"cloud.google.com/go/compute/metadata"
2425
"k8s.io/klog"
@@ -29,7 +30,15 @@ const (
2930
google = "Google"
3031
)
3132

32-
func onGCE() bool {
33+
func init() {
34+
cloudinfo.RegisterCloudProvider(info.GCE, &provider{})
35+
}
36+
37+
type provider struct{}
38+
39+
var _ cloudinfo.CloudProvider = provider{}
40+
41+
func (provider) IsActiveProvider() bool {
3342
data, err := ioutil.ReadFile(gceProductName)
3443
if err != nil {
3544
klog.V(2).Infof("Error while reading product_name: %v", err)
@@ -38,7 +47,7 @@ func onGCE() bool {
3847
return strings.Contains(string(data), google)
3948
}
4049

41-
func getGceInstanceType() info.InstanceType {
50+
func (provider) GetInstanceType() info.InstanceType {
4251
machineType, err := metadata.Get("instance/machine-type")
4352
if err != nil {
4453
return info.UnknownInstance
@@ -48,7 +57,7 @@ func getGceInstanceType() info.InstanceType {
4857
return info.InstanceType(responseParts[len(responseParts)-1])
4958
}
5059

51-
func getGceInstanceID() info.InstanceID {
60+
func (provider) GetInstanceID() info.InstanceID {
5261
instanceID, err := metadata.Get("instance/id")
5362
if err != nil {
5463
return info.UnknownInstance

0 commit comments

Comments
 (0)