implement state api command
This commit is contained in:
parent
cb7ab16427
commit
a2ddcc2232
5 changed files with 66 additions and 12 deletions
|
@ -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) {
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
9
event.go
9
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"
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
|
Loading…
Reference in a new issue