From 53d25cc90eb47e7529ac0b3f43fb927f2b5003a8 Mon Sep 17 00:00:00 2001 From: Rohit Jnagal Date: Fri, 13 Mar 2015 21:00:38 +0000 Subject: [PATCH] Add alias and namespace information to /spec endpoint --- api/versions.go | 23 +---------------------- info/v2/container.go | 8 ++++++++ manager/manager.go | 34 +++++++++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/api/versions.go b/api/versions.go index 448127e8..710e2f4e 100644 --- a/api/versions.go +++ b/api/versions.go @@ -368,8 +368,7 @@ func (self *version2_0) HandleRequest(requestType string, request []string, m ma if err != nil { return err } - specV2 := convertSpec(spec) - return writeResult(specV2, w) + return writeResult(spec, w) case storageApi: var err error fi := []v2.FsInfo{} @@ -393,26 +392,6 @@ func (self *version2_0) HandleRequest(requestType string, request []string, m ma } } -// Convert container spec from v1 to v2. -func convertSpec(specV1 info.ContainerSpec) v2.ContainerSpec { - specV2 := v2.ContainerSpec{ - CreationTime: specV1.CreationTime, - HasCpu: specV1.HasCpu, - HasMemory: specV1.HasMemory, - } - if specV1.HasCpu { - specV2.Cpu.Limit = specV1.Cpu.Limit - specV2.Cpu.MaxLimit = specV1.Cpu.MaxLimit - specV2.Cpu.Mask = specV1.Cpu.Mask - } - if specV1.HasMemory { - specV2.Memory.Limit = specV1.Memory.Limit - specV2.Memory.Reservation = specV1.Memory.Reservation - specV2.Memory.SwapLimit = specV1.Memory.SwapLimit - } - return specV2 -} - func convertStats(cont *info.ContainerInfo) []v2.ContainerStats { stats := []v2.ContainerStats{} for _, val := range cont.Stats { diff --git a/info/v2/container.go b/info/v2/container.go index c3c6eb68..03a806cc 100644 --- a/info/v2/container.go +++ b/info/v2/container.go @@ -51,6 +51,14 @@ type ContainerSpec struct { // Time at which the container was created. CreationTime time.Time `json:"creation_time,omitempty"` + // Other names by which the container is known within a certain namespace. + // This is unique within that namespace. + Aliases []string `json:"aliases,omitempty"` + + // Namespace under which the aliases of a container are unique. + // An example of a namespace is "docker" for Docker containers. + Namespace string `json:"namespace,omitempty"` + HasCpu bool `json:"has_cpu"` Cpu CpuSpec `json:"cpu,omitempty"` diff --git a/manager/manager.go b/manager/manager.go index b35a622e..47abf7a1 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -64,7 +64,7 @@ type Manager interface { DockerContainer(dockerName string, query *info.ContainerInfoRequest) (info.ContainerInfo, error) // Gets spec for a container. - GetContainerSpec(containerName string) (info.ContainerSpec, error) + GetContainerSpec(containerName string) (v2.ContainerSpec, error) // Get derived stats for a container. GetContainerDerivedStats(containerName string) (v2.DerivedStats, error) @@ -290,16 +290,40 @@ func (self *manager) getContainerData(containerName string) (*containerData, err return cont, nil } -func (self *manager) GetContainerSpec(containerName string) (info.ContainerSpec, error) { +func (self *manager) GetContainerSpec(containerName string) (v2.ContainerSpec, error) { cont, err := self.getContainerData(containerName) if err != nil { - return info.ContainerSpec{}, err + return v2.ContainerSpec{}, err } cinfo, err := cont.GetInfo() if err != nil { - return info.ContainerSpec{}, err + return v2.ContainerSpec{}, err } - return self.getAdjustedSpec(cinfo), nil + spec := self.getV2Spec(cinfo) + return spec, nil +} + +// Get V2 container spec from v1 container info. +func (self *manager) getV2Spec(cinfo *containerInfo) v2.ContainerSpec { + specV1 := self.getAdjustedSpec(cinfo) + specV2 := v2.ContainerSpec{ + CreationTime: specV1.CreationTime, + HasCpu: specV1.HasCpu, + HasMemory: specV1.HasMemory, + } + if specV1.HasCpu { + specV2.Cpu.Limit = specV1.Cpu.Limit + specV2.Cpu.MaxLimit = specV1.Cpu.MaxLimit + specV2.Cpu.Mask = specV1.Cpu.Mask + } + if specV1.HasMemory { + specV2.Memory.Limit = specV1.Memory.Limit + specV2.Memory.Reservation = specV1.Memory.Reservation + specV2.Memory.SwapLimit = specV1.Memory.SwapLimit + } + specV2.Aliases = cinfo.Aliases + specV2.Namespace = cinfo.Namespace + return specV2 } func (self *manager) getAdjustedSpec(cinfo *containerInfo) info.ContainerSpec {