containerd/supervisor.go

198 lines
4.7 KiB
Go
Raw Normal View History

2015-11-05 23:29:53 +00:00
package containerd
import (
"os"
2015-11-10 22:24:34 +00:00
"path/filepath"
2015-11-07 00:44:52 +00:00
"sync"
2015-11-05 23:29:53 +00:00
"github.com/Sirupsen/logrus"
2015-11-06 21:01:55 +00:00
"github.com/opencontainers/runc/libcontainer"
2015-11-05 23:29:53 +00:00
)
// NewSupervisor returns an initialized Process supervisor.
2015-11-05 23:49:13 +00:00
func NewSupervisor(stateDir string, concurrency int) (*Supervisor, error) {
2015-11-05 23:29:53 +00:00
if err := os.MkdirAll(stateDir, 0755); err != nil {
return nil, err
}
2015-11-06 00:16:11 +00:00
// register counters
2015-11-05 23:49:13 +00:00
runtime, err := NewRuntime(stateDir)
if err != nil {
return nil, err
2015-11-05 23:29:53 +00:00
}
2015-11-10 22:24:34 +00:00
j, err := newJournal(filepath.Join(stateDir, "journal.json"))
if err != nil {
return nil, err
}
2015-11-05 23:49:13 +00:00
s := &Supervisor{
stateDir: stateDir,
containers: make(map[string]Container),
runtime: runtime,
2015-11-07 00:44:52 +00:00
tasks: make(chan *startTask, concurrency*100),
2015-11-10 22:24:34 +00:00
journal: j,
2015-11-07 00:44:52 +00:00
}
for i := 0; i < concurrency; i++ {
s.workerGroup.Add(1)
go s.startContainerWorker(s.tasks)
2015-11-05 23:29:53 +00:00
}
return s, nil
}
type Supervisor struct {
// stateDir is the directory on the system to store container runtime state information.
stateDir string
containers map[string]Container
runtime Runtime
2015-11-10 22:24:34 +00:00
journal *journal
events chan *Event
2015-11-07 00:44:52 +00:00
tasks chan *startTask
workerGroup sync.WaitGroup
2015-11-05 23:29:53 +00:00
}
// need proper close logic for jobs and stuff so that sending to the channels dont panic
// but can complete jobs
2015-11-10 22:24:34 +00:00
func (s *Supervisor) Close() error {
return s.journal.Close()
}
2015-11-06 00:40:57 +00:00
// Start is a non-blocking call that runs the supervisor for monitoring contianer processes and
2015-11-05 23:29:53 +00:00
// executing new containers.
//
// This event loop is the only thing that is allowed to modify state of containers and processes.
2015-11-10 22:24:34 +00:00
func (s *Supervisor) Start(events chan *Event) error {
2015-11-05 23:29:53 +00:00
if events == nil {
return ErrEventChanNil
}
s.events = events
2015-11-06 00:40:57 +00:00
go func() {
2015-11-10 22:24:34 +00:00
for e := range events {
if err := s.journal.write(e); err != nil {
logrus.WithField("error", err).Error("write journal entry")
}
switch e.Type {
case ExitEventType:
2015-11-06 23:42:32 +00:00
logrus.WithFields(logrus.Fields{"pid": e.Pid, "status": e.Status}).
Debug("containerd: process exited")
2015-11-06 21:01:55 +00:00
container, err := s.getContainerForPid(e.Pid)
if err != nil {
if err != errNoContainerForPid {
logrus.WithField("error", err).Error("containerd: find container for pid")
2015-11-06 00:40:57 +00:00
}
2015-11-06 21:01:55 +00:00
continue
}
container.SetExited(e.Status)
ne := NewEvent(DeleteEventType)
ne.ID = container.ID()
s.SendEvent(ne)
2015-11-10 22:24:34 +00:00
case StartContainerEventType:
2015-11-06 21:01:55 +00:00
container, err := s.runtime.Create(e.ID, e.BundlePath)
2015-11-06 00:40:57 +00:00
if err != nil {
2015-11-06 21:01:55 +00:00
e.Err <- err
2015-11-06 00:40:57 +00:00
continue
}
2015-11-06 21:01:55 +00:00
s.containers[e.ID] = container
2015-11-07 00:44:52 +00:00
s.tasks <- &startTask{
err: e.Err,
container: container,
}
continue
case DeleteEventType:
2015-11-07 00:44:52 +00:00
if container, ok := s.containers[e.ID]; ok {
if err := s.deleteContainer(container); err != nil {
logrus.WithField("error", err).Error("containerd: deleting container")
}
2015-11-06 00:40:57 +00:00
}
2015-11-10 22:24:34 +00:00
case GetContainerEventType:
2015-11-10 19:38:26 +00:00
for _, c := range s.containers {
e.Containers = append(e.Containers, c)
}
2015-11-10 22:24:34 +00:00
case SignalEventType:
2015-11-10 21:44:35 +00:00
container, ok := s.containers[e.ID]
if !ok {
e.Err <- ErrContainerNotFound
continue
}
processes, err := container.Processes()
if err != nil {
e.Err <- err
continue
}
for _, p := range processes {
if pid, err := p.Pid(); err == nil && pid == e.Pid {
2015-11-10 21:44:35 +00:00
e.Err <- p.Signal(e.Signal)
continue
}
}
e.Err <- ErrProcessNotFound
continue
case AddProcessEventType:
container, ok := s.containers[e.ID]
if !ok {
e.Err <- ErrContainerNotFound
continue
}
p, err := s.runtime.StartProcess(container, *e.Process)
if err != nil {
e.Err <- err
continue
}
if e.Pid, err = p.Pid(); err != nil {
e.Err <- err
continue
}
2015-11-05 23:29:53 +00:00
}
close(e.Err)
2015-11-05 23:29:53 +00:00
}
2015-11-06 00:40:57 +00:00
}()
2015-11-05 23:29:53 +00:00
return nil
}
2015-11-07 00:44:52 +00:00
func (s *Supervisor) deleteContainer(container Container) error {
delete(s.containers, container.ID())
return container.Delete()
}
2015-11-06 21:01:55 +00:00
func (s *Supervisor) getContainerForPid(pid int) (Container, error) {
for _, container := range s.containers {
cpid, err := container.Pid()
if err != nil {
if lerr, ok := err.(libcontainer.Error); ok {
if lerr.Code() == libcontainer.ProcessNotExecuted {
continue
}
}
logrus.WithField("error", err).Error("containerd: get container pid")
}
if pid == cpid {
return container, nil
}
}
return nil, errNoContainerForPid
}
2015-11-10 22:24:34 +00:00
func (s *Supervisor) SendEvent(evt *Event) {
2015-11-05 23:29:53 +00:00
s.events <- evt
}
2015-11-07 00:44:52 +00:00
type startTask struct {
container Container
err chan error
}
func (s *Supervisor) startContainerWorker(tasks chan *startTask) {
defer s.workerGroup.Done()
for t := range tasks {
if err := t.container.Start(); err != nil {
2015-11-10 22:24:34 +00:00
e := NewEvent(StartContainerEventType)
e.ID = t.container.ID()
s.SendEvent(e)
2015-11-07 00:44:52 +00:00
t.err <- err
continue
}
t.err <- nil
}
}