From a2ddcc2232240feda7beaed92a87151dd7d5ca3b Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 10 Nov 2015 11:38:26 -0800 Subject: [PATCH] implement state api command --- api/v1/server.go | 30 +++++++++++++++++++++++++++++- container.go | 2 ++ event.go | 9 +++++++++ runtime_linux.go | 32 +++++++++++++++++++++----------- supervisor.go | 5 +++++ 5 files changed, 66 insertions(+), 12 deletions(-) diff --git a/api/v1/server.go b/api/v1/server.go index de867c8..724bff0 100644 --- a/api/v1/server.go +++ b/api/v1/server.go @@ -4,6 +4,7 @@ import ( "encoding/json" "net/http" + "github.com/Sirupsen/logrus" "github.com/crosbymichael/containerd" "github.com/gorilla/mux" ) @@ -30,7 +31,34 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (s *server) containers(w http.ResponseWriter, r *http.Request) { - + var state State + state.Containers = []Container{} + e := &containerd.GetContainersEvent{ + Err: make(chan error, 1), + } + s.supervisor.SendEvent(e) + if err := <-e.Err; err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + for _, c := range e.Containers { + processes, err := c.Processes() + if err != nil { + logrus.WithFields(logrus.Fields{ + "error": err, + "container": c.ID(), + }).Error("get processes for container") + } + state.Containers = append(state.Containers, Container{ + ID: c.ID(), + BundlePath: c.Path(), + Processes: processes, + }) + } + if err := json.NewEncoder(w).Encode(&state); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } } func (s *server) events(w http.ResponseWriter, r *http.Request) { diff --git a/container.go b/container.go index e06cc60..9e18a25 100644 --- a/container.go +++ b/container.go @@ -3,7 +3,9 @@ package containerd type Container interface { ID() string Start() error + Path() string Pid() (int, error) SetExited(status int) Delete() error + Processes() ([]int, error) } diff --git a/event.go b/event.go index 7c634e2..8dd7260 100644 --- a/event.go +++ b/event.go @@ -35,3 +35,12 @@ type ContainerStartErrorEvent struct { func (c *ContainerStartErrorEvent) String() string { return "container start error" } + +type GetContainersEvent struct { + Containers []Container + Err chan error +} + +func (c *GetContainersEvent) String() string { + return "get containers" +} diff --git a/runtime_linux.go b/runtime_linux.go index da20d16..a7968dc 100644 --- a/runtime_linux.go +++ b/runtime_linux.go @@ -163,12 +163,17 @@ type libcontainerContainer struct { initProcess *libcontainer.Process exitStatus int exited bool + path string } func (c *libcontainerContainer) ID() string { return c.c.ID() } +func (c *libcontainerContainer) Path() string { + return c.path +} + func (c *libcontainerContainer) Pid() (int, error) { return c.initProcess.Pid() } @@ -187,6 +192,10 @@ func (c *libcontainerContainer) Delete() error { return c.c.Destroy() } +func (c *libcontainerContainer) Processes() ([]int, error) { + return c.c.Processes() +} + func NewRuntime(stateDir string) (Runtime, error) { f, err := libcontainer.New(stateDir, libcontainer.Cgroupfs, func(l *libcontainer.LinuxFactory) error { //l.CriuPath = context.GlobalString("criu") @@ -225,6 +234,7 @@ func (r *libcontainerRuntime) Create(id, bundlePath string) (Container, error) { c := &libcontainerContainer{ c: container, initProcess: process, + path: bundlePath, } return c, nil } @@ -380,16 +390,16 @@ func (rt *libcontainerRuntime) createCgroupConfig(name string, spec *specs.Linux AllowedDevices: append(devices, allowedDevices...), } r := spec.Linux.Resources - c.Memory = r.Memory.Limit - c.MemoryReservation = r.Memory.Reservation - c.MemorySwap = r.Memory.Swap - c.KernelMemory = r.Memory.Kernel - c.MemorySwappiness = r.Memory.Swappiness - c.CpuShares = r.CPU.Shares - c.CpuQuota = r.CPU.Quota - c.CpuPeriod = r.CPU.Period - c.CpuRtRuntime = r.CPU.RealtimeRuntime - c.CpuRtPeriod = r.CPU.RealtimePeriod + c.Memory = int64(r.Memory.Limit) + c.MemoryReservation = int64(r.Memory.Reservation) + c.MemorySwap = int64(r.Memory.Swap) + c.KernelMemory = int64(r.Memory.Kernel) + c.MemorySwappiness = int64(r.Memory.Swappiness) + c.CpuShares = int64(r.CPU.Shares) + c.CpuQuota = int64(r.CPU.Quota) + c.CpuPeriod = int64(r.CPU.Period) + c.CpuRtRuntime = int64(r.CPU.RealtimeRuntime) + c.CpuRtPeriod = int64(r.CPU.RealtimePeriod) c.CpusetCpus = r.CPU.Cpus c.CpusetMems = r.CPU.Mems c.BlkioWeight = r.BlockIO.Weight @@ -425,7 +435,7 @@ func (rt *libcontainerRuntime) createCgroupConfig(name string, spec *specs.Linux for _, m := range r.Network.Priorities { c.NetPrioIfpriomap = append(c.NetPrioIfpriomap, &configs.IfPrioMap{ Interface: m.Name, - Priority: m.Priority, + Priority: int64(m.Priority), }) } return c, nil diff --git a/supervisor.go b/supervisor.go index 1e9adba..798af1d 100644 --- a/supervisor.go +++ b/supervisor.go @@ -87,6 +87,11 @@ func (s *Supervisor) Start(events chan Event) error { logrus.WithField("error", err).Error("containerd: deleting container") } } + case *GetContainersEvent: + for _, c := range s.containers { + e.Containers = append(e.Containers, c) + } + e.Err <- nil } } }()