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"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/crosbymichael/containerd"
|
"github.com/crosbymichael/containerd"
|
||||||
"github.com/gorilla/mux"
|
"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) {
|
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) {
|
func (s *server) events(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -3,7 +3,9 @@ package containerd
|
||||||
type Container interface {
|
type Container interface {
|
||||||
ID() string
|
ID() string
|
||||||
Start() error
|
Start() error
|
||||||
|
Path() string
|
||||||
Pid() (int, error)
|
Pid() (int, error)
|
||||||
SetExited(status int)
|
SetExited(status int)
|
||||||
Delete() error
|
Delete() error
|
||||||
|
Processes() ([]int, error)
|
||||||
}
|
}
|
||||||
|
|
9
event.go
9
event.go
|
@ -35,3 +35,12 @@ type ContainerStartErrorEvent struct {
|
||||||
func (c *ContainerStartErrorEvent) String() string {
|
func (c *ContainerStartErrorEvent) String() string {
|
||||||
return "container start error"
|
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
|
initProcess *libcontainer.Process
|
||||||
exitStatus int
|
exitStatus int
|
||||||
exited bool
|
exited bool
|
||||||
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *libcontainerContainer) ID() string {
|
func (c *libcontainerContainer) ID() string {
|
||||||
return c.c.ID()
|
return c.c.ID()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *libcontainerContainer) Path() string {
|
||||||
|
return c.path
|
||||||
|
}
|
||||||
|
|
||||||
func (c *libcontainerContainer) Pid() (int, error) {
|
func (c *libcontainerContainer) Pid() (int, error) {
|
||||||
return c.initProcess.Pid()
|
return c.initProcess.Pid()
|
||||||
}
|
}
|
||||||
|
@ -187,6 +192,10 @@ func (c *libcontainerContainer) Delete() error {
|
||||||
return c.c.Destroy()
|
return c.c.Destroy()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *libcontainerContainer) Processes() ([]int, error) {
|
||||||
|
return c.c.Processes()
|
||||||
|
}
|
||||||
|
|
||||||
func NewRuntime(stateDir string) (Runtime, error) {
|
func NewRuntime(stateDir string) (Runtime, error) {
|
||||||
f, err := libcontainer.New(stateDir, libcontainer.Cgroupfs, func(l *libcontainer.LinuxFactory) error {
|
f, err := libcontainer.New(stateDir, libcontainer.Cgroupfs, func(l *libcontainer.LinuxFactory) error {
|
||||||
//l.CriuPath = context.GlobalString("criu")
|
//l.CriuPath = context.GlobalString("criu")
|
||||||
|
@ -225,6 +234,7 @@ func (r *libcontainerRuntime) Create(id, bundlePath string) (Container, error) {
|
||||||
c := &libcontainerContainer{
|
c := &libcontainerContainer{
|
||||||
c: container,
|
c: container,
|
||||||
initProcess: process,
|
initProcess: process,
|
||||||
|
path: bundlePath,
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
@ -380,16 +390,16 @@ func (rt *libcontainerRuntime) createCgroupConfig(name string, spec *specs.Linux
|
||||||
AllowedDevices: append(devices, allowedDevices...),
|
AllowedDevices: append(devices, allowedDevices...),
|
||||||
}
|
}
|
||||||
r := spec.Linux.Resources
|
r := spec.Linux.Resources
|
||||||
c.Memory = r.Memory.Limit
|
c.Memory = int64(r.Memory.Limit)
|
||||||
c.MemoryReservation = r.Memory.Reservation
|
c.MemoryReservation = int64(r.Memory.Reservation)
|
||||||
c.MemorySwap = r.Memory.Swap
|
c.MemorySwap = int64(r.Memory.Swap)
|
||||||
c.KernelMemory = r.Memory.Kernel
|
c.KernelMemory = int64(r.Memory.Kernel)
|
||||||
c.MemorySwappiness = r.Memory.Swappiness
|
c.MemorySwappiness = int64(r.Memory.Swappiness)
|
||||||
c.CpuShares = r.CPU.Shares
|
c.CpuShares = int64(r.CPU.Shares)
|
||||||
c.CpuQuota = r.CPU.Quota
|
c.CpuQuota = int64(r.CPU.Quota)
|
||||||
c.CpuPeriod = r.CPU.Period
|
c.CpuPeriod = int64(r.CPU.Period)
|
||||||
c.CpuRtRuntime = r.CPU.RealtimeRuntime
|
c.CpuRtRuntime = int64(r.CPU.RealtimeRuntime)
|
||||||
c.CpuRtPeriod = r.CPU.RealtimePeriod
|
c.CpuRtPeriod = int64(r.CPU.RealtimePeriod)
|
||||||
c.CpusetCpus = r.CPU.Cpus
|
c.CpusetCpus = r.CPU.Cpus
|
||||||
c.CpusetMems = r.CPU.Mems
|
c.CpusetMems = r.CPU.Mems
|
||||||
c.BlkioWeight = r.BlockIO.Weight
|
c.BlkioWeight = r.BlockIO.Weight
|
||||||
|
@ -425,7 +435,7 @@ func (rt *libcontainerRuntime) createCgroupConfig(name string, spec *specs.Linux
|
||||||
for _, m := range r.Network.Priorities {
|
for _, m := range r.Network.Priorities {
|
||||||
c.NetPrioIfpriomap = append(c.NetPrioIfpriomap, &configs.IfPrioMap{
|
c.NetPrioIfpriomap = append(c.NetPrioIfpriomap, &configs.IfPrioMap{
|
||||||
Interface: m.Name,
|
Interface: m.Name,
|
||||||
Priority: m.Priority,
|
Priority: int64(m.Priority),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
|
|
|
@ -87,6 +87,11 @@ func (s *Supervisor) Start(events chan Event) error {
|
||||||
logrus.WithField("error", err).Error("containerd: deleting container")
|
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…
Add table
Reference in a new issue