Victor code review
This commit is contained in:
parent
1abd853d6b
commit
ee16d6e7da
@ -115,17 +115,17 @@ type NetInfo struct {
|
|||||||
type CloudProvider string
|
type CloudProvider string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GCE CloudProvider = "GCE"
|
GCE CloudProvider = "GCE"
|
||||||
AWS = "AWS"
|
AWS = "AWS"
|
||||||
BAREMETAL = "BAREMETAL"
|
Baremetal = "Baremetal"
|
||||||
UNKNOWN_PROVIDER = "UNKNOWN"
|
UnkownProvider = "Unknown"
|
||||||
)
|
)
|
||||||
|
|
||||||
type InstanceType string
|
type InstanceType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NO_INSTANCE InstanceType = "NONE"
|
NoInstance InstanceType = "None"
|
||||||
UNKNOWN_INSTANCE = "UNKNOWN"
|
UnknownInstance = "Uknown"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MachineInfo struct {
|
type MachineInfo struct {
|
||||||
|
@ -49,14 +49,14 @@ func (self *realCloudInfo) GetInstanceType() info.InstanceType {
|
|||||||
|
|
||||||
func detectCloudProvider() info.CloudProvider {
|
func detectCloudProvider() info.CloudProvider {
|
||||||
switch {
|
switch {
|
||||||
case inGCE():
|
case onGCE():
|
||||||
return info.GCE
|
return info.GCE
|
||||||
case inAWS():
|
case onAWS():
|
||||||
return info.AWS
|
return info.AWS
|
||||||
case inBareMetal():
|
case onBaremetal():
|
||||||
return info.BAREMETAL
|
return info.Baremetal
|
||||||
}
|
}
|
||||||
return info.UNKNOWN_PROVIDER
|
return info.UnkownProvider
|
||||||
}
|
}
|
||||||
|
|
||||||
func detectInstanceType(cloudProvider info.CloudProvider) info.InstanceType {
|
func detectInstanceType(cloudProvider info.CloudProvider) info.InstanceType {
|
||||||
@ -65,23 +65,23 @@ func detectInstanceType(cloudProvider info.CloudProvider) info.InstanceType {
|
|||||||
return getGceInstanceType()
|
return getGceInstanceType()
|
||||||
case info.AWS:
|
case info.AWS:
|
||||||
return getAwsInstanceType()
|
return getAwsInstanceType()
|
||||||
case info.BAREMETAL:
|
case info.Baremetal:
|
||||||
return info.NO_INSTANCE
|
return info.NoInstance
|
||||||
}
|
}
|
||||||
return info.UNKNOWN_INSTANCE
|
return info.UnknownInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Implement method.
|
//TODO: Implement method.
|
||||||
func inAWS() bool {
|
func onAWS() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Implement method.
|
//TODO: Implement method.
|
||||||
func getAwsInstanceType() info.InstanceType {
|
func getAwsInstanceType() info.InstanceType {
|
||||||
return info.UNKNOWN_INSTANCE
|
return info.UnknownInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Implement method.
|
//TODO: Implement method.
|
||||||
func inBareMetal() bool {
|
func onBaremetal() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -15,38 +15,22 @@
|
|||||||
package cloudinfo
|
package cloudinfo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/GoogleCloudPlatform/gcloud-golang/compute/metadata"
|
||||||
info "github.com/google/cadvisor/info/v1"
|
info "github.com/google/cadvisor/info/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func inGCE() bool {
|
func onGCE() bool {
|
||||||
_, err := http.Get("http://metadata.google.internal/computeMetadata/v1/instance/machine-type")
|
return metadata.OnGCE()
|
||||||
return err == nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getGceInstanceType() info.InstanceType {
|
func getGceInstanceType() info.InstanceType {
|
||||||
// Query the metadata server.
|
machineType, err := metadata.Get("machine-type")
|
||||||
req, err := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/instance/machine-type", nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return info.UNKNOWN_INSTANCE
|
return info.UnknownInstance
|
||||||
}
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the instance name from the response.
|
responseParts := strings.Split(machineType, "/") // Extract the instance name from the machine type.
|
||||||
responseString := string(body[:numRead])
|
|
||||||
responseParts := strings.Split(responseString, "/")
|
|
||||||
return info.InstanceType(responseParts[len(responseParts)-1])
|
return info.InstanceType(responseParts[len(responseParts)-1])
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user