Victor code review
This commit is contained in:
parent
1abd853d6b
commit
ee16d6e7da
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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])
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user