Spec consistency

This commit is contained in:
Abin Shahab 2014-10-15 07:58:58 +00:00
parent 9264114895
commit f170df0a76
3 changed files with 21 additions and 10 deletions

View File

@ -22,6 +22,7 @@ import (
"flag" "flag"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"os"
) )
var argContainerHints = flag.String("container_hints", "/etc/cadvisor/container_hints.json", "container hints file") var argContainerHints = flag.String("container_hints", "/etc/cadvisor/container_hints.json", "container hints file")
type containerHints struct { type containerHints struct {
@ -36,12 +37,13 @@ type containerHint struct {
type networkInterface struct { type networkInterface struct {
VethHost string `json:"veth_host,omitempty"` VethHost string `json:"veth_host,omitempty"`
VethChild string `json:"veth_child,omitempty"` VethChild string `json:"veth_child,omitempty"`
NsPath string `json:"ns_path,omitempty"`
} }
func getContainerHintsFromFile(containerHintsFile string) (containerHints, error) { func getContainerHintsFromFile(containerHintsFile string) (containerHints, error) {
dat, err := ioutil.ReadFile(containerHintsFile) dat, err := ioutil.ReadFile(containerHintsFile)
if os.IsNotExist(err) {
return containerHints{}, nil
}
var cHints containerHints var cHints containerHints
if err == nil { if err == nil {
err = json.Unmarshal(dat, &cHints) err = json.Unmarshal(dat, &cHints)

View File

@ -4,7 +4,7 @@ import (
"testing" "testing"
) )
func TestUnmarshal(t *testing.T) { func TestGetContainerHintsFromFile(t *testing.T) {
cHints, err := getContainerHintsFromFile("test_resources/container_hints.json") cHints, err := getContainerHintsFromFile("test_resources/container_hints.json")
if err != nil { if err != nil {
@ -16,3 +16,10 @@ func TestUnmarshal(t *testing.T) {
t.Errorf("Cannot find network interface in %s", cHints) t.Errorf("Cannot find network interface in %s", cHints)
} }
} }
func TestFileNotExist(t *testing.T) {
_, err := getContainerHintsFromFile("/file_does_not_exist.json")
if err != nil {
t.Fatalf("getContainerHintsFromFile must not error for blank file: %s", err)
}
}

View File

@ -167,25 +167,27 @@ func (self *rawContainerHandler) GetSpec() (info.ContainerSpec, error) {
if self.name == "/" { if self.name == "/" {
spec.HasFilesystem = true spec.HasFilesystem = true
} }
//Network
if self.networkInterface != nil {
spec.HasNetwork = true
}
return spec, nil return spec, nil
} }
func (self *rawContainerHandler) GetStats() (*info.ContainerStats, error) { func (self *rawContainerHandler) GetStats() (*info.ContainerStats, error) {
var stats *info.ContainerStats state := dockerlibcontainer.State{}
var err error
if self.networkInterface != nil { if self.networkInterface != nil {
state := dockerlibcontainer.State{ state = dockerlibcontainer.State{
NetworkState: network.NetworkState{ NetworkState: network.NetworkState{
VethHost: self.networkInterface.VethHost, VethHost: self.networkInterface.VethHost,
VethChild: self.networkInterface.VethChild, VethChild: self.networkInterface.VethChild,
NsPath: "unknown", NsPath: "unknown",
}, },
} }
stats, err = libcontainer.GetStats(self.cgroup, &state)
} else {
stats, err = libcontainer.GetStatsCgroupOnly(self.cgroup)
} }
stats, err := libcontainer.GetStats(self.cgroup, &state)
if err != nil { if err != nil {
return nil, err return nil, err
} }