Add funcs for events endpoint

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2015-11-30 15:46:36 -08:00
parent 3a30ea0c4f
commit 510c9c852b
3 changed files with 43 additions and 2 deletions

View file

@ -27,6 +27,7 @@ func NewServer(supervisor *containerd.Supervisor) http.Handler {
r.HandleFunc("/containers/{id:.*}", s.createContainer).Methods("POST")
r.HandleFunc("/containers/{id:.*}", s.updateContainer).Methods("PATCH")
r.HandleFunc("/event", s.event).Methods("POST")
r.HandleFunc("/events", s.events).Methods("GET")
r.HandleFunc("/containers", s.containers).Methods("GET")
return s
}
@ -60,6 +61,34 @@ func (s *server) updateContainer(w http.ResponseWriter, r *http.Request) {
}
}
func (s *server) events(w http.ResponseWriter, r *http.Request) {
events, err := s.supervisor.Events()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
enc := json.NewEncoder(w)
for evt := range events {
var v interface{}
switch evt.Type {
case containerd.ExitEventType:
v = createExitEvent(evt)
}
if err := enc.Encode(v); err != nil {
// TODO: handled closed conn
logrus.WithField("error", err).Error("encode event")
}
}
}
func createExitEvent(e *containerd.Event) *Event {
return &Event{
Type: "exit",
ID: e.ID,
Status: e.Status,
}
}
func (s *server) event(w http.ResponseWriter, r *http.Request) {
var e containerd.Event
if err := json.NewDecoder(r.Body).Decode(&e); err != nil {

View file

@ -31,8 +31,8 @@ type User struct {
}
type Process struct {
Terminal bool `json:"terminal,omitempty"`
User User `json:"user,omitempty"`
Terminal bool `json:"terminal"`
User User `json:"user"`
Args []string `json:"args,omitempty"`
Env []string `json:"env,omitempty"`
Cwd string `json:"cwd,omitempty"`
@ -42,3 +42,9 @@ type Process struct {
type Signal struct {
Signal int `json:"signal"`
}
type Event struct {
Type string `json:"type"`
ID string `json:"id,omitempty"`
Status int `json:"status,omitempty"`
}

View file

@ -63,6 +63,10 @@ func (s *Supervisor) Close() error {
return s.journal.Close()
}
func (s *Supervisor) Events() (<-chan *Event, error) {
return nil, nil
}
// Start is a non-blocking call that runs the supervisor for monitoring contianer processes and
// executing new containers.
//
@ -73,6 +77,8 @@ func (s *Supervisor) Start(events chan *Event) error {
}
s.events = events
go func() {
// allocate an entire thread to this goroutine for the main event loop
// so that nothing else is scheduled over the top of it.
runtime.LockOSThread()
for e := range events {
s.journal.write(e)