cloudinfo: Adding AWS support and InstanceID

This commit is contained in:
Erez Freiberger 2016-02-10 10:58:56 +02:00
parent 8221b8cebc
commit 885649a9b6
6 changed files with 137 additions and 8 deletions

View File

@ -117,6 +117,7 @@ type CloudProvider string
const ( const (
GCE CloudProvider = "GCE" GCE CloudProvider = "GCE"
AWS = "AWS" AWS = "AWS"
Azure = "Azure"
Baremetal = "Baremetal" Baremetal = "Baremetal"
UnkownProvider = "Unknown" UnkownProvider = "Unknown"
) )
@ -128,6 +129,12 @@ const (
UnknownInstance = "Unknown" UnknownInstance = "Unknown"
) )
type InstanceID string
const (
UnNamedInstance InstanceID = "None"
)
type MachineInfo struct { type MachineInfo struct {
// The number of cores in this machine. // The number of cores in this machine.
NumCores int `json:"num_cores"` NumCores int `json:"num_cores"`
@ -165,6 +172,9 @@ type MachineInfo struct {
// Type of cloud instance (e.g. GCE standard) the machine is. // Type of cloud instance (e.g. GCE standard) the machine is.
InstanceType InstanceType `json:"instance_type"` InstanceType InstanceType `json:"instance_type"`
// ID of cloud instance (e.g. instance-1) given to it by the cloud provider.
InstanceID InstanceID `json:"instance_id"`
} }
type VersionInfo struct { type VersionInfo struct {

View File

@ -96,6 +96,7 @@ func getMachineInfo(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (
realCloudInfo := cloudinfo.NewRealCloudInfo() realCloudInfo := cloudinfo.NewRealCloudInfo()
cloudProvider := realCloudInfo.GetCloudProvider() cloudProvider := realCloudInfo.GetCloudProvider()
instanceType := realCloudInfo.GetInstanceType() instanceType := realCloudInfo.GetInstanceType()
instanceID := realCloudInfo.GetInstanceID()
machineInfo := &info.MachineInfo{ machineInfo := &info.MachineInfo{
NumCores: numCores, NumCores: numCores,
@ -109,6 +110,7 @@ func getMachineInfo(sysFs sysfs.SysFs, fsInfo fs.FsInfo, inHostNamespace bool) (
BootID: getInfoFromFiles(filepath.Join(rootFs, *bootIdFilePath)), BootID: getInfoFromFiles(filepath.Join(rootFs, *bootIdFilePath)),
CloudProvider: cloudProvider, CloudProvider: cloudProvider,
InstanceType: instanceType, InstanceType: instanceType,
InstanceID: instanceID,
} }
for _, fs := range filesystems { for _, fs := range filesystems {

45
utils/cloudinfo/aws.go Normal file
View File

@ -0,0 +1,45 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cloudinfo
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
info "github.com/google/cadvisor/info/v1"
)
func onAWS() bool {
client := ec2metadata.New(session.New(&aws.Config{}))
return client.Available()
}
func getAwsMetadata(name string) string {
client := ec2metadata.New(session.New(&aws.Config{}))
data, err := client.GetMetadata(name)
if err != nil {
return info.UnknownInstance
}
return data
}
func getAwsInstanceType() info.InstanceType {
return info.InstanceType(getAwsMetadata("instance-type"))
}
func getAwsInstanceID() info.InstanceID {
return info.InstanceID(getAwsMetadata("instance-id"))
}

48
utils/cloudinfo/azure.go Normal file
View File

@ -0,0 +1,48 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cloudinfo
import (
info "github.com/google/cadvisor/info/v1"
"io/ioutil"
"strings"
)
const (
SysVendorFileName = "/sys/class/dmi/id/sys_vendor"
BiosUUIDFileName = "/sys/class/dmi/id/product_uuid"
MicrosoftCorporation = "Microsoft Corporation"
)
func onAzure() bool {
data, err := ioutil.ReadFile(SysVendorFileName)
if err != nil {
return false
}
return strings.Contains(string(data), MicrosoftCorporation)
}
//TODO: Implement method.
func getAzureInstanceType() info.InstanceType {
return info.UnknownInstance
}
func getAzureInstanceID() info.InstanceID {
data, err := ioutil.ReadFile(BiosUUIDFileName)
if err != nil {
return info.UnNamedInstance
}
return info.InstanceID(strings.TrimSuffix(string(data), "\n"))
}

View File

@ -23,19 +23,23 @@ import (
type CloudInfo interface { type CloudInfo interface {
GetCloudProvider() info.CloudProvider GetCloudProvider() info.CloudProvider
GetInstanceType() info.InstanceType GetInstanceType() info.InstanceType
GetInstanceID() info.InstanceID
} }
type realCloudInfo struct { type realCloudInfo struct {
cloudProvider info.CloudProvider cloudProvider info.CloudProvider
instanceType info.InstanceType instanceType info.InstanceType
instanceID info.InstanceID
} }
func NewRealCloudInfo() CloudInfo { func NewRealCloudInfo() CloudInfo {
cloudProvider := detectCloudProvider() cloudProvider := detectCloudProvider()
instanceType := detectInstanceType(cloudProvider) instanceType := detectInstanceType(cloudProvider)
instanceID := detectInstanceID(cloudProvider)
return &realCloudInfo{ return &realCloudInfo{
cloudProvider: cloudProvider, cloudProvider: cloudProvider,
instanceType: instanceType, instanceType: instanceType,
instanceID: instanceID,
} }
} }
@ -47,12 +51,18 @@ func (self *realCloudInfo) GetInstanceType() info.InstanceType {
return self.instanceType return self.instanceType
} }
func (self *realCloudInfo) GetInstanceID() info.InstanceID {
return self.instanceID
}
func detectCloudProvider() info.CloudProvider { func detectCloudProvider() info.CloudProvider {
switch { switch {
case onGCE(): case onGCE():
return info.GCE return info.GCE
case onAWS(): case onAWS():
return info.AWS return info.AWS
case onAzure():
return info.Azure
case onBaremetal(): case onBaremetal():
return info.Baremetal return info.Baremetal
} }
@ -65,20 +75,26 @@ func detectInstanceType(cloudProvider info.CloudProvider) info.InstanceType {
return getGceInstanceType() return getGceInstanceType()
case info.AWS: case info.AWS:
return getAwsInstanceType() return getAwsInstanceType()
case info.Azure:
return getAzureInstanceType()
case info.Baremetal: case info.Baremetal:
return info.NoInstance return info.NoInstance
} }
return info.UnknownInstance return info.UnknownInstance
} }
//TODO: Implement method. func detectInstanceID(cloudProvider info.CloudProvider) info.InstanceID {
func onAWS() bool { switch cloudProvider {
return false case info.GCE:
return getGceInstanceID()
case info.AWS:
return getAwsInstanceID()
case info.Azure:
return getAzureInstanceID()
case info.Baremetal:
return info.UnNamedInstance
} }
return info.UnNamedInstance
//TODO: Implement method.
func getAwsInstanceType() info.InstanceType {
return info.UnknownInstance
} }
//TODO: Implement method. //TODO: Implement method.

View File

@ -35,3 +35,11 @@ func getGceInstanceType() info.InstanceType {
responseParts := strings.Split(machineType, "/") // Extract the instance name from the machine type. responseParts := strings.Split(machineType, "/") // Extract the instance name from the machine type.
return info.InstanceType(responseParts[len(responseParts)-1]) return info.InstanceType(responseParts[len(responseParts)-1])
} }
func getGceInstanceID() info.InstanceID {
instanceID, err := metadata.Get("instance/id")
if err != nil {
return info.UnknownInstance
}
return info.InstanceID(info.InstanceType(instanceID))
}