Change /containers to /state with machine info
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
3ea5dd79e0
commit
c1eb9ac90b
5 changed files with 56 additions and 10 deletions
|
@ -13,7 +13,6 @@ Add a process:
|
||||||
```bash
|
```bash
|
||||||
curl -s -XPUT localhost:8888/containers/redis/process -d@process.json | json_pp
|
curl -s -XPUT localhost:8888/containers/redis/process -d@process.json | json_pp
|
||||||
{
|
{
|
||||||
"pid" : 25671,
|
|
||||||
"user" : {
|
"user" : {
|
||||||
"gid" : 0,
|
"gid" : 0,
|
||||||
"uid" : 0
|
"uid" : 0
|
||||||
|
@ -31,10 +30,10 @@ curl -s -XPUT localhost:8888/containers/redis/process -d@process.json | json_pp
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Get containers:
|
Get containers and state:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
curl -s localhost:8888/containers | json_pp
|
curl -s localhost:8888/state | json_pp
|
||||||
{
|
{
|
||||||
"containers" : [
|
"containers" : [
|
||||||
{
|
{
|
||||||
|
|
|
@ -30,7 +30,7 @@ func NewServer(supervisor *containerd.Supervisor) http.Handler {
|
||||||
// internal method for replaying the journal
|
// internal method for replaying the journal
|
||||||
r.HandleFunc("/event", s.event).Methods("POST")
|
r.HandleFunc("/event", s.event).Methods("POST")
|
||||||
r.HandleFunc("/events", s.events).Methods("GET")
|
r.HandleFunc("/events", s.events).Methods("GET")
|
||||||
r.HandleFunc("/containers", s.containers).Methods("GET")
|
r.HandleFunc("/state", s.state).Methods("GET")
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +100,7 @@ func (s *server) event(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if e.Containers != nil && len(e.Containers) > 0 {
|
if e.Containers != nil && len(e.Containers) > 0 {
|
||||||
if err := writeContainers(w, &e); err != nil {
|
if err := s.writeState(w, &e); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -170,22 +170,28 @@ func (s *server) signalPid(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) containers(w http.ResponseWriter, r *http.Request) {
|
func (s *server) state(w http.ResponseWriter, r *http.Request) {
|
||||||
e := containerd.NewEvent(containerd.GetContainerEventType)
|
e := containerd.NewEvent(containerd.GetContainerEventType)
|
||||||
s.supervisor.SendEvent(e)
|
s.supervisor.SendEvent(e)
|
||||||
if err := <-e.Err; err != nil {
|
if err := <-e.Err; err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := writeContainers(w, e); err != nil {
|
if err := s.writeState(w, e); err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeContainers(w http.ResponseWriter, e *containerd.Event) error {
|
func (s *server) writeState(w http.ResponseWriter, e *containerd.Event) error {
|
||||||
var state State
|
m := s.supervisor.Machine()
|
||||||
state.Containers = []Container{}
|
state := State{
|
||||||
|
Containers: []Container{},
|
||||||
|
Machine: Machine{
|
||||||
|
Cpus: m.Cpus,
|
||||||
|
Memory: m.Memory,
|
||||||
|
},
|
||||||
|
}
|
||||||
for _, c := range e.Containers {
|
for _, c := range e.Containers {
|
||||||
processes, err := c.Processes()
|
processes, err := c.Processes()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package v1
|
||||||
|
|
||||||
type State struct {
|
type State struct {
|
||||||
Containers []Container `json:"containers"`
|
Containers []Container `json:"containers"`
|
||||||
|
Machine Machine `json:"machine"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Status string
|
type Status string
|
||||||
|
@ -11,6 +12,11 @@ const (
|
||||||
Running Status = "running"
|
Running Status = "running"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Machine struct {
|
||||||
|
Cpus int `json:"cpus"`
|
||||||
|
Memory int64 `json:"memory"`
|
||||||
|
}
|
||||||
|
|
||||||
type ContainerState struct {
|
type ContainerState struct {
|
||||||
Status Status `json:"status,omitempty"`
|
Status Status `json:"status,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
23
machine.go
Normal file
23
machine.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package containerd
|
||||||
|
|
||||||
|
import "github.com/cloudfoundry/gosigar"
|
||||||
|
|
||||||
|
type Machine struct {
|
||||||
|
Cpus int
|
||||||
|
Memory int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func CollectMachineInformation() (Machine, error) {
|
||||||
|
m := Machine{}
|
||||||
|
cpu := sigar.CpuList{}
|
||||||
|
if err := cpu.Get(); err != nil {
|
||||||
|
return m, err
|
||||||
|
}
|
||||||
|
m.Cpus = len(cpu.List)
|
||||||
|
mem := sigar.Mem{}
|
||||||
|
if err := mem.Get(); err != nil {
|
||||||
|
return m, err
|
||||||
|
}
|
||||||
|
m.Memory = int64(mem.Total)
|
||||||
|
return m, nil
|
||||||
|
}
|
|
@ -24,6 +24,10 @@ func NewSupervisor(stateDir string, tasks chan *StartTask) (*Supervisor, error)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
machine, err := CollectMachineInformation()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
s := &Supervisor{
|
s := &Supervisor{
|
||||||
stateDir: stateDir,
|
stateDir: stateDir,
|
||||||
containers: make(map[string]runtime.Container),
|
containers: make(map[string]runtime.Container),
|
||||||
|
@ -32,6 +36,7 @@ func NewSupervisor(stateDir string, tasks chan *StartTask) (*Supervisor, error)
|
||||||
journal: j,
|
journal: j,
|
||||||
tasks: tasks,
|
tasks: tasks,
|
||||||
events: make(chan *Event, 2048),
|
events: make(chan *Event, 2048),
|
||||||
|
machine: machine,
|
||||||
}
|
}
|
||||||
// register default event handlers
|
// register default event handlers
|
||||||
s.handlers = map[EventType]Handler{
|
s.handlers = map[EventType]Handler{
|
||||||
|
@ -59,6 +64,7 @@ type Supervisor struct {
|
||||||
events chan *Event
|
events chan *Event
|
||||||
tasks chan *StartTask
|
tasks chan *StartTask
|
||||||
subscribers map[subscriber]bool
|
subscribers map[subscriber]bool
|
||||||
|
machine Machine
|
||||||
}
|
}
|
||||||
|
|
||||||
type subscriber chan *Event
|
type subscriber chan *Event
|
||||||
|
@ -114,6 +120,12 @@ func (s *Supervisor) Start() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Machine returns the machine information for which the
|
||||||
|
// supervisor is executing on.
|
||||||
|
func (s *Supervisor) Machine() Machine {
|
||||||
|
return s.machine
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Supervisor) getContainerForPid(pid int) (runtime.Container, error) {
|
func (s *Supervisor) getContainerForPid(pid int) (runtime.Container, error) {
|
||||||
for _, container := range s.containers {
|
for _, container := range s.containers {
|
||||||
cpid, err := container.Pid()
|
cpid, err := container.Pid()
|
||||||
|
|
Loading…
Reference in a new issue