From ee16d6e7da09dad1d2acbd6b2c227e58ab05eac8 Mon Sep 17 00:00:00 2001 From: AnanyaKumar Date: Tue, 30 Jun 2015 15:48:56 -0700 Subject: [PATCH] Victor code review --- info/v1/machine.go | 12 ++++++------ utils/cloudinfo/cloudinfo.go | 22 +++++++++++----------- utils/cloudinfo/gce.go | 28 ++++++---------------------- 3 files changed, 23 insertions(+), 39 deletions(-) diff --git a/info/v1/machine.go b/info/v1/machine.go index ba26d256..9ec46b8a 100644 --- a/info/v1/machine.go +++ b/info/v1/machine.go @@ -115,17 +115,17 @@ type NetInfo struct { type CloudProvider string const ( - GCE CloudProvider = "GCE" - AWS = "AWS" - BAREMETAL = "BAREMETAL" - UNKNOWN_PROVIDER = "UNKNOWN" + GCE CloudProvider = "GCE" + AWS = "AWS" + Baremetal = "Baremetal" + UnkownProvider = "Unknown" ) type InstanceType string const ( - NO_INSTANCE InstanceType = "NONE" - UNKNOWN_INSTANCE = "UNKNOWN" + NoInstance InstanceType = "None" + UnknownInstance = "Uknown" ) type MachineInfo struct { diff --git a/utils/cloudinfo/cloudinfo.go b/utils/cloudinfo/cloudinfo.go index 4ca12f82..e073fae6 100644 --- a/utils/cloudinfo/cloudinfo.go +++ b/utils/cloudinfo/cloudinfo.go @@ -49,14 +49,14 @@ func (self *realCloudInfo) GetInstanceType() info.InstanceType { func detectCloudProvider() info.CloudProvider { switch { - case inGCE(): + case onGCE(): return info.GCE - case inAWS(): + case onAWS(): return info.AWS - case inBareMetal(): - return info.BAREMETAL + case onBaremetal(): + return info.Baremetal } - return info.UNKNOWN_PROVIDER + return info.UnkownProvider } func detectInstanceType(cloudProvider info.CloudProvider) info.InstanceType { @@ -65,23 +65,23 @@ func detectInstanceType(cloudProvider info.CloudProvider) info.InstanceType { return getGceInstanceType() case info.AWS: return getAwsInstanceType() - case info.BAREMETAL: - return info.NO_INSTANCE + case info.Baremetal: + return info.NoInstance } - return info.UNKNOWN_INSTANCE + return info.UnknownInstance } //TODO: Implement method. -func inAWS() bool { +func onAWS() bool { return false } //TODO: Implement method. func getAwsInstanceType() info.InstanceType { - return info.UNKNOWN_INSTANCE + return info.UnknownInstance } //TODO: Implement method. -func inBareMetal() bool { +func onBaremetal() bool { return false } diff --git a/utils/cloudinfo/gce.go b/utils/cloudinfo/gce.go index 9f95fd08..79569355 100644 --- a/utils/cloudinfo/gce.go +++ b/utils/cloudinfo/gce.go @@ -15,38 +15,22 @@ package cloudinfo import ( - "io" - "net/http" "strings" + "github.com/GoogleCloudPlatform/gcloud-golang/compute/metadata" info "github.com/google/cadvisor/info/v1" ) -func inGCE() bool { - _, err := http.Get("http://metadata.google.internal/computeMetadata/v1/instance/machine-type") - return err == nil +func onGCE() bool { + return metadata.OnGCE() } func getGceInstanceType() info.InstanceType { - // Query the metadata server. - req, err := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/instance/machine-type", nil) + machineType, err := metadata.Get("machine-type") if err != nil { - return info.UNKNOWN_INSTANCE - } - req.Header.Set("Metadata-Flavor", "Google") - client := &http.Client{} - resp, err := client.Do(req) - if err != nil { - return info.UNKNOWN_INSTANCE - } - body := make([]byte, 1000) - numRead, err := resp.Body.Read(body) - if err != io.EOF { - return info.UNKNOWN_INSTANCE + return info.UnknownInstance } - // Extract the instance name from the response. - responseString := string(body[:numRead]) - responseParts := strings.Split(responseString, "/") + responseParts := strings.Split(machineType, "/") // Extract the instance name from the machine type. return info.InstanceType(responseParts[len(responseParts)-1]) }