containerd/supervisor.go

146 lines
3.5 KiB
Go
Raw Normal View History

2015-11-05 23:29:53 +00:00
package containerd
import (
"os"
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-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),
}
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-07 00:44:52 +00:00
events chan Event
tasks chan *startTask
workerGroup sync.WaitGroup
2015-11-05 23:29:53 +00:00
}
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-06 00:40:57 +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() {
for evt := range events {
switch e := evt.(type) {
case *ExitEvent:
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)
2015-11-07 00:44:52 +00:00
if err := s.deleteContainer(container); err != nil {
2015-11-06 21:01:55 +00:00
logrus.WithField("error", err).Error("containerd: deleting container")
2015-11-06 00:40:57 +00:00
}
2015-11-06 23:42:32 +00:00
case *StartContainerEvent:
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,
}
case *ContainerStartErrorEvent:
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 19:38:26 +00:00
case *GetContainersEvent:
for _, c := range s.containers {
e.Containers = append(e.Containers, c)
}
e.Err <- nil
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-05 23:29:53 +00:00
func (s *Supervisor) SendEvent(evt Event) {
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 {
s.SendEvent(&ContainerStartErrorEvent{
ID: t.container.ID(),
})
t.err <- err
continue
}
t.err <- nil
}
}