2015-11-05 23:29:53 +00:00
|
|
|
package containerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2015-11-10 22:24:34 +00:00
|
|
|
"path/filepath"
|
2015-12-01 19:56:08 +00:00
|
|
|
goruntime "runtime"
|
2015-11-05 23:29:53 +00:00
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
2015-12-01 19:56:08 +00:00
|
|
|
"github.com/docker/containerd/runtime"
|
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-12-03 01:42:28 +00:00
|
|
|
func NewSupervisor(stateDir string, tasks chan *StartTask) (*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-12-01 23:47:27 +00:00
|
|
|
r, err := newRuntime(stateDir)
|
2015-11-05 23:49:13 +00:00
|
|
|
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-12-03 19:49:56 +00:00
|
|
|
machine, err := CollectMachineInformation()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-05 23:49:13 +00:00
|
|
|
s := &Supervisor{
|
|
|
|
stateDir: stateDir,
|
2015-12-01 19:56:08 +00:00
|
|
|
containers: make(map[string]runtime.Container),
|
|
|
|
processes: make(map[int]runtime.Container),
|
|
|
|
runtime: r,
|
2015-11-10 22:24:34 +00:00
|
|
|
journal: j,
|
2015-12-03 01:42:28 +00:00
|
|
|
tasks: tasks,
|
2015-12-03 01:44:39 +00:00
|
|
|
events: make(chan *Event, 2048),
|
2015-12-03 19:49:56 +00:00
|
|
|
machine: machine,
|
2015-11-07 00:44:52 +00:00
|
|
|
}
|
2015-12-01 18:55:13 +00:00
|
|
|
// register default event handlers
|
|
|
|
s.handlers = map[EventType]Handler{
|
2015-12-04 00:07:53 +00:00
|
|
|
ExecExitEventType: &ExecExitEvent{s},
|
|
|
|
ExitEventType: &ExitEvent{s},
|
|
|
|
StartContainerEventType: &StartEvent{s},
|
|
|
|
DeleteEventType: &DeleteEvent{s},
|
|
|
|
GetContainerEventType: &GetContainersEvent{s},
|
|
|
|
SignalEventType: &SignalEvent{s},
|
|
|
|
AddProcessEventType: &AddProcessEvent{s},
|
|
|
|
UpdateContainerEventType: &UpdateEvent{s},
|
|
|
|
CreateCheckpointEventType: &CreateCheckpointEvent{s},
|
2015-12-01 18:55:13 +00:00
|
|
|
}
|
|
|
|
// start the container workers for concurrent container starts
|
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.
|
2015-12-01 18:55:13 +00:00
|
|
|
stateDir string
|
2015-12-01 19:56:08 +00:00
|
|
|
containers map[string]runtime.Container
|
|
|
|
processes map[int]runtime.Container
|
2015-12-01 18:55:13 +00:00
|
|
|
handlers map[EventType]Handler
|
2015-12-01 19:56:08 +00:00
|
|
|
runtime runtime.Runtime
|
2015-12-01 18:55:13 +00:00
|
|
|
journal *journal
|
2015-11-10 22:24:34 +00:00
|
|
|
events chan *Event
|
2015-12-03 01:42:28 +00:00
|
|
|
tasks chan *StartTask
|
2015-12-01 23:49:24 +00:00
|
|
|
subscribers map[subscriber]bool
|
2015-12-03 19:49:56 +00:00
|
|
|
machine Machine
|
2015-11-05 23:29:53 +00:00
|
|
|
}
|
|
|
|
|
2015-12-01 23:49:24 +00:00
|
|
|
type subscriber chan *Event
|
|
|
|
|
2015-11-10 22:57:10 +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 {
|
2015-12-01 23:49:24 +00:00
|
|
|
//TODO: unsubscribe all channels
|
2015-11-10 22:24:34 +00:00
|
|
|
return s.journal.Close()
|
|
|
|
}
|
|
|
|
|
2015-12-01 23:49:24 +00:00
|
|
|
func (s *Supervisor) Events() subscriber {
|
|
|
|
return subscriber(make(chan *Event))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Supervisor) Unsubscribe(sub subscriber) {
|
|
|
|
delete(s.subscribers, sub)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Supervisor) NotifySubscribers(e *Event) {
|
|
|
|
for sub := range s.subscribers {
|
|
|
|
sub <- e
|
|
|
|
}
|
2015-11-30 23:46:36 +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-12-03 01:44:39 +00:00
|
|
|
func (s *Supervisor) Start() error {
|
2015-11-06 00:40:57 +00:00
|
|
|
go func() {
|
2015-11-30 23:46:36 +00:00
|
|
|
// allocate an entire thread to this goroutine for the main event loop
|
|
|
|
// so that nothing else is scheduled over the top of it.
|
2015-12-01 19:56:08 +00:00
|
|
|
goruntime.LockOSThread()
|
2015-12-03 01:44:39 +00:00
|
|
|
for e := range s.events {
|
2015-12-03 01:42:28 +00:00
|
|
|
EventsCounter.Inc(1)
|
2015-11-13 22:09:35 +00:00
|
|
|
s.journal.write(e)
|
2015-12-01 18:55:13 +00:00
|
|
|
h, ok := s.handlers[e.Type]
|
|
|
|
if !ok {
|
|
|
|
e.Err <- ErrUnknownEvent
|
2015-11-10 22:57:10 +00:00
|
|
|
continue
|
2015-12-01 18:55:13 +00:00
|
|
|
}
|
|
|
|
if err := h.Handle(e); err != nil {
|
|
|
|
if err != errDeferedResponse {
|
2015-11-10 21:44:35 +00:00
|
|
|
e.Err <- err
|
2015-12-01 18:55:13 +00:00
|
|
|
close(e.Err)
|
2015-11-10 21:44:35 +00:00
|
|
|
}
|
2015-11-10 22:57:10 +00:00
|
|
|
continue
|
2015-11-05 23:29:53 +00:00
|
|
|
}
|
2015-11-10 22:57:10 +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-12-03 19:49:56 +00:00
|
|
|
// Machine returns the machine information for which the
|
|
|
|
// supervisor is executing on.
|
|
|
|
func (s *Supervisor) Machine() Machine {
|
|
|
|
return s.machine
|
|
|
|
}
|
|
|
|
|
2015-12-01 19:56:08 +00:00
|
|
|
func (s *Supervisor) getContainerForPid(pid int) (runtime.Container, error) {
|
2015-11-06 21:01:55 +00:00
|
|
|
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
|
|
|
|
}
|