Skip to content

Commit

Permalink
Merge pull request google#519 from rjnagal/summary
Browse files Browse the repository at this point in the history
Add spec to v2.0 API.
  • Loading branch information
vmarmol committed Feb 19, 2015
2 parents db90148 + 28984d7 commit 34eb23f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
9 changes: 9 additions & 0 deletions api/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (
machineApi = "machine"
dockerApi = "docker"
summaryApi = "summary"
specApi = "spec"
)

// Interface for a cAdvisor API version
Expand Down Expand Up @@ -257,6 +258,14 @@ func (self *version2_0) HandleRequest(requestType string, request []string, m ma
}

return writeResult(stats, w)
case specApi:
containerName := getContainerName(request)
glog.V(2).Infof("Api - Spec(%v)", containerName)
spec, err := m.GetContainerSpec(containerName)
if err != nil {
return err
}
return writeResult(spec, w)
default:
return self.baseVersion.HandleRequest(requestType, request, m, w, r)
}
Expand Down
49 changes: 38 additions & 11 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ type Manager interface {
// Gets information about a specific Docker container. The specified name is within the Docker namespace.
DockerContainer(dockerName string, query *info.ContainerInfoRequest) (info.ContainerInfo, error)

// Gets spec for a container.
GetContainerSpec(containerName string) (info.ContainerSpec, error)

// Get derived stats for a container.
GetContainerDerivedStats(containerName string) (info.DerivedStats, error)

Expand Down Expand Up @@ -226,8 +229,7 @@ func (self *manager) globalHousekeeping(quit chan error) {
}
}

// Get a container by name.
func (self *manager) GetContainerInfo(containerName string, query *info.ContainerInfoRequest) (*info.ContainerInfo, error) {
func (self *manager) getContainerData(containerName string) (*containerData, error) {
var cont *containerData
var ok bool
func() {
Expand All @@ -242,7 +244,40 @@ func (self *manager) GetContainerInfo(containerName string, query *info.Containe
if !ok {
return nil, fmt.Errorf("unknown container %q", containerName)
}
return cont, nil
}

func (self *manager) GetContainerSpec(containerName string) (info.ContainerSpec, error) {
cont, err := self.getContainerData(containerName)
if err != nil {
return info.ContainerSpec{}, err
}
cinfo, err := cont.GetInfo()
if err != nil {
return info.ContainerSpec{}, err
}
return self.getAdjustedSpec(cinfo), nil
}

func (self *manager) getAdjustedSpec(cinfo *containerInfo) info.ContainerSpec {
spec := cinfo.Spec

// Set default value to an actual value
if spec.HasMemory {
// Memory.Limit is 0 means there's no limit
if spec.Memory.Limit == 0 {
spec.Memory.Limit = uint64(self.machineInfo.MemoryCapacity)
}
}
return spec
}

// Get a container by name.
func (self *manager) GetContainerInfo(containerName string, query *info.ContainerInfoRequest) (*info.ContainerInfo, error) {
cont, err := self.getContainerData(containerName)
if err != nil {
return nil, err
}
return self.containerDataToContainerInfo(cont, query)
}

Expand All @@ -262,17 +297,9 @@ func (self *manager) containerDataToContainerInfo(cont *containerData, query *in
ret := &info.ContainerInfo{
ContainerReference: cinfo.ContainerReference,
Subcontainers: cinfo.Subcontainers,
Spec: cinfo.Spec,
Spec: self.getAdjustedSpec(cinfo),
Stats: stats,
}

// Set default value to an actual value
if ret.Spec.HasMemory {
// Memory.Limit is 0 means there's no limit
if ret.Spec.Memory.Limit == 0 {
ret.Spec.Memory.Limit = uint64(self.machineInfo.MemoryCapacity)
}
}
return ret, nil
}

Expand Down

0 comments on commit 34eb23f

Please sign in to comment.