Add supervisor config
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
0dcd3a034d
commit
40003e7085
3 changed files with 36 additions and 37 deletions
|
@ -104,13 +104,14 @@ func main() {
|
||||||
func daemon(context *cli.Context) error {
|
func daemon(context *cli.Context) error {
|
||||||
signals := make(chan os.Signal, 2048)
|
signals := make(chan os.Signal, 2048)
|
||||||
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1)
|
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT, syscall.SIGUSR1)
|
||||||
sv, err := supervisor.New(
|
sv, err := supervisor.New(supervisor.Config{
|
||||||
context.String("state-dir"),
|
StateDir: context.String("state-dir"),
|
||||||
context.String("runtime"),
|
Runtime: context.String("runtime"),
|
||||||
context.String("shim"),
|
ShimName: context.String("shim"),
|
||||||
context.StringSlice("runtime-args"),
|
RuntimeArgs: context.StringSlice("runtime-args"),
|
||||||
context.Duration("start-timeout"),
|
Timeout: context.Duration("start-timeout"),
|
||||||
context.Int("retain-count"))
|
EventRetainCount: context.Int("retain-count"),
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,22 +24,22 @@ type StartTask struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Supervisor) start(t *StartTask) error {
|
func (s *Supervisor) start(t *StartTask) error {
|
||||||
rt := s.runtime
|
rt := s.config.Runtime
|
||||||
rtArgs := s.runtimeArgs
|
rtArgs := s.config.RuntimeArgs
|
||||||
if t.Runtime != "" {
|
if t.Runtime != "" {
|
||||||
rt = t.Runtime
|
rt = t.Runtime
|
||||||
rtArgs = t.RuntimeArgs
|
rtArgs = t.RuntimeArgs
|
||||||
}
|
}
|
||||||
container, err := runtime.New(runtime.ContainerOpts{
|
container, err := runtime.New(runtime.ContainerOpts{
|
||||||
Root: s.stateDir,
|
Root: s.config.StateDir,
|
||||||
ID: t.ID,
|
ID: t.ID,
|
||||||
Bundle: t.BundlePath,
|
Bundle: t.BundlePath,
|
||||||
Runtime: rt,
|
Runtime: rt,
|
||||||
RuntimeArgs: rtArgs,
|
RuntimeArgs: rtArgs,
|
||||||
Shim: s.shim,
|
Shim: s.config.ShimName,
|
||||||
Labels: t.Labels,
|
Labels: t.Labels,
|
||||||
NoPivotRoot: t.NoPivotRoot,
|
NoPivotRoot: t.NoPivotRoot,
|
||||||
Timeout: s.timeout,
|
Timeout: s.config.Timeout,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -58,7 +58,6 @@ func (s *Supervisor) start(t *StartTask) error {
|
||||||
if t.Checkpoint != nil {
|
if t.Checkpoint != nil {
|
||||||
task.CheckpointPath = filepath.Join(t.CheckpointDir, t.Checkpoint.Name)
|
task.CheckpointPath = filepath.Join(t.CheckpointDir, t.Checkpoint.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.startTasks <- task
|
s.startTasks <- task
|
||||||
return errDeferredResponse
|
return errDeferredResponse
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,10 +17,19 @@ const (
|
||||||
defaultBufferSize = 2048 // size of queue in eventloop
|
defaultBufferSize = 2048 // size of queue in eventloop
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
StateDir string
|
||||||
|
Runtime string
|
||||||
|
ShimName string
|
||||||
|
RuntimeArgs []string
|
||||||
|
Timeout time.Duration
|
||||||
|
EventRetainCount int
|
||||||
|
}
|
||||||
|
|
||||||
// New returns an initialized Process supervisor.
|
// New returns an initialized Process supervisor.
|
||||||
func New(stateDir string, runtimeName, shimName string, runtimeArgs []string, timeout time.Duration, retainCount int) (*Supervisor, error) {
|
func New(c Config) (*Supervisor, error) {
|
||||||
startTasks := make(chan *startTask, 10)
|
startTasks := make(chan *startTask, 10)
|
||||||
if err := os.MkdirAll(stateDir, 0755); err != nil {
|
if err := os.MkdirAll(c.StateDir, 0755); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
machine, err := CollectMachineInformation()
|
machine, err := CollectMachineInformation()
|
||||||
|
@ -32,19 +41,15 @@ func New(stateDir string, runtimeName, shimName string, runtimeArgs []string, ti
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s := &Supervisor{
|
s := &Supervisor{
|
||||||
stateDir: stateDir,
|
config: c,
|
||||||
containers: make(map[string]*containerInfo),
|
containers: make(map[string]*containerInfo),
|
||||||
startTasks: startTasks,
|
startTasks: startTasks,
|
||||||
machine: machine,
|
machine: machine,
|
||||||
subscribers: make(map[chan Event]struct{}),
|
subscribers: make(map[chan Event]struct{}),
|
||||||
tasks: make(chan Task, defaultBufferSize),
|
tasks: make(chan Task, defaultBufferSize),
|
||||||
monitor: monitor,
|
monitor: monitor,
|
||||||
runtime: runtimeName,
|
|
||||||
runtimeArgs: runtimeArgs,
|
|
||||||
shim: shimName,
|
|
||||||
timeout: timeout,
|
|
||||||
}
|
}
|
||||||
if err := setupEventLog(s, retainCount); err != nil {
|
if err := setupEventLog(s, c.EventRetainCount); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
go s.exitHandler()
|
go s.exitHandler()
|
||||||
|
@ -65,7 +70,7 @@ func setupEventLog(s *Supervisor, retainCount int) error {
|
||||||
}
|
}
|
||||||
logrus.WithField("count", len(s.eventLog)).Debug("containerd: read past events")
|
logrus.WithField("count", len(s.eventLog)).Debug("containerd: read past events")
|
||||||
events := s.Events(time.Time{}, false, "")
|
events := s.Events(time.Time{}, false, "")
|
||||||
return eventLogger(s, filepath.Join(s.stateDir, "events.log"), events, retainCount)
|
return eventLogger(s, filepath.Join(s.config.StateDir, "events.log"), events, retainCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
func eventLogger(s *Supervisor, path string, events chan Event, retainCount int) error {
|
func eventLogger(s *Supervisor, path string, events chan Event, retainCount int) error {
|
||||||
|
@ -122,7 +127,7 @@ func eventLogger(s *Supervisor, path string, events chan Event, retainCount int)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readEventLog(s *Supervisor) error {
|
func readEventLog(s *Supervisor) error {
|
||||||
f, err := os.Open(filepath.Join(s.stateDir, "events.log"))
|
f, err := os.Open(filepath.Join(s.config.StateDir, "events.log"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
return nil
|
return nil
|
||||||
|
@ -146,12 +151,7 @@ func readEventLog(s *Supervisor) error {
|
||||||
|
|
||||||
// Supervisor represents a container supervisor
|
// Supervisor represents a container supervisor
|
||||||
type Supervisor struct {
|
type Supervisor struct {
|
||||||
// stateDir is the directory on the system to store container runtime state information.
|
config Config
|
||||||
stateDir string
|
|
||||||
// name of the OCI compatible runtime used to execute containers
|
|
||||||
runtime string
|
|
||||||
runtimeArgs []string
|
|
||||||
shim string
|
|
||||||
containers map[string]*containerInfo
|
containers map[string]*containerInfo
|
||||||
startTasks chan *startTask
|
startTasks chan *startTask
|
||||||
// we need a lock around the subscribers map only because additions and deletions from
|
// we need a lock around the subscribers map only because additions and deletions from
|
||||||
|
@ -163,7 +163,6 @@ type Supervisor struct {
|
||||||
monitor *Monitor
|
monitor *Monitor
|
||||||
eventLog []Event
|
eventLog []Event
|
||||||
eventLock sync.Mutex
|
eventLock sync.Mutex
|
||||||
timeout time.Duration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop closes all startTasks and sends a SIGTERM to each container's pid1 then waits for they to
|
// Stop closes all startTasks and sends a SIGTERM to each container's pid1 then waits for they to
|
||||||
|
@ -252,9 +251,9 @@ func (s *Supervisor) notifySubscribers(e Event) {
|
||||||
// state of the Supervisor
|
// state of the Supervisor
|
||||||
func (s *Supervisor) Start() error {
|
func (s *Supervisor) Start() error {
|
||||||
logrus.WithFields(logrus.Fields{
|
logrus.WithFields(logrus.Fields{
|
||||||
"stateDir": s.stateDir,
|
"stateDir": s.config.StateDir,
|
||||||
"runtime": s.runtime,
|
"runtime": s.config.Runtime,
|
||||||
"runtimeArgs": s.runtimeArgs,
|
"runtimeArgs": s.config.RuntimeArgs,
|
||||||
"memory": s.machine.Memory,
|
"memory": s.machine.Memory,
|
||||||
"cpus": s.machine.Cpus,
|
"cpus": s.machine.Cpus,
|
||||||
}).Debug("containerd: supervisor running")
|
}).Debug("containerd: supervisor running")
|
||||||
|
@ -300,7 +299,7 @@ func (s *Supervisor) monitorProcess(p runtime.Process) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Supervisor) restore() error {
|
func (s *Supervisor) restore() error {
|
||||||
dirs, err := ioutil.ReadDir(s.stateDir)
|
dirs, err := ioutil.ReadDir(s.config.StateDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -309,7 +308,7 @@ func (s *Supervisor) restore() error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
id := d.Name()
|
id := d.Name()
|
||||||
container, err := runtime.Load(s.stateDir, id, s.shim, s.timeout)
|
container, err := runtime.Load(s.config.StateDir, id, s.config.ShimName, s.config.Timeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue