From 4157d1adfd2aefbd553b17eea8d7d101e1494a2f Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Fri, 9 Dec 2016 10:00:30 -0800 Subject: [PATCH] Pass context down to executors Signed-off-by: Kenfe-Mickael Laventure --- execution/executor.go | 23 ++++++++--------- execution/executors/oci/oci.go | 45 +++++++++++++++++----------------- execution/service.go | 36 +++++++++++++-------------- 3 files changed, 53 insertions(+), 51 deletions(-) diff --git a/execution/executor.go b/execution/executor.go index 810bf08..3af38e6 100644 --- a/execution/executor.go +++ b/execution/executor.go @@ -1,6 +1,7 @@ package execution import ( + "context" "os" "github.com/opencontainers/runtime-spec/specs-go" @@ -23,16 +24,16 @@ type StartProcessOpts struct { } type Executor interface { - Create(id string, o CreateOpts) (*Container, error) - Pause(*Container) error - Resume(*Container) error - Status(*Container) (Status, error) - List() ([]*Container, error) - Load(id string) (*Container, error) - Delete(*Container) error - Start(*Container) error + Create(ctx context.Context, id string, o CreateOpts) (*Container, error) + Pause(context.Context, *Container) error + Resume(context.Context, *Container) error + Status(context.Context, *Container) (Status, error) + List(context.Context) ([]*Container, error) + Load(ctx context.Context, id string) (*Container, error) + Delete(context.Context, *Container) error + Start(context.Context, *Container) error - StartProcess(*Container, StartProcessOpts) (Process, error) - SignalProcess(*Container, string, os.Signal) error - DeleteProcess(*Container, string) error + StartProcess(context.Context, *Container, StartProcessOpts) (Process, error) + SignalProcess(ctx context.Context, c *Container, id string, sig os.Signal) error + DeleteProcess(ctx context.Context, c *Container, id string) error } diff --git a/execution/executors/oci/oci.go b/execution/executors/oci/oci.go index b8b52f7..e5a0737 100644 --- a/execution/executors/oci/oci.go +++ b/execution/executors/oci/oci.go @@ -1,6 +1,7 @@ package oci import ( + "context" "errors" "fmt" "io" @@ -77,7 +78,7 @@ func setupConsole(rio runc.IO) (*os.File, string, error) { return master, console, nil } -func (r *OCIRuntime) Create(id string, o execution.CreateOpts) (container *execution.Container, err error) { +func (r *OCIRuntime) Create(ctx context.Context, id string, o execution.CreateOpts) (container *execution.Container, err error) { rio, err := getRuncIO(o.Stdin, o.Stdout, o.Stderr) if err != nil { return nil, err @@ -115,7 +116,7 @@ func (r *OCIRuntime) Create(id string, o execution.CreateOpts) (container *execu return nil, err } pidFile := filepath.Join(initDir, "pid") - err = r.runc.Create(id, o.Bundle, &runc.CreateOpts{ + err = r.runc.Create(ctx, id, o.Bundle, &runc.CreateOpts{ PidFile: pidFile, Console: consolePath, IO: rio, @@ -125,8 +126,8 @@ func (r *OCIRuntime) Create(id string, o execution.CreateOpts) (container *execu } defer func() { if err != nil { - r.runc.Kill(id, int(syscall.SIGKILL)) - r.runc.Delete(id) + r.runc.Kill(ctx, id, int(syscall.SIGKILL)) + r.runc.Delete(ctx, id) } }() @@ -146,12 +147,12 @@ func (r *OCIRuntime) Create(id string, o execution.CreateOpts) (container *execu return container, nil } -func (r *OCIRuntime) Start(c *execution.Container) error { - return r.runc.Start(c.ID()) +func (r *OCIRuntime) Start(ctx context.Context, c *execution.Container) error { + return r.runc.Start(ctx, c.ID()) } -func (r *OCIRuntime) Status(c *execution.Container) (execution.Status, error) { - state, err := r.runc.State(c.ID()) +func (r *OCIRuntime) Status(ctx context.Context, c *execution.Container) (execution.Status, error) { + state, err := r.runc.State(ctx, c.ID()) if err != nil { return "", err } @@ -190,8 +191,8 @@ func (r *OCIRuntime) load(runcC *runc.Container) (*execution.Container, error) { return container, nil } -func (r *OCIRuntime) List() ([]*execution.Container, error) { - runcCs, err := r.runc.List() +func (r *OCIRuntime) List(ctx context.Context) ([]*execution.Container, error) { + runcCs, err := r.runc.List(ctx) if err != nil { return nil, err } @@ -208,8 +209,8 @@ func (r *OCIRuntime) List() ([]*execution.Container, error) { return containers, nil } -func (r *OCIRuntime) Load(id string) (*execution.Container, error) { - runcC, err := r.runc.State(id) +func (r *OCIRuntime) Load(ctx context.Context, id string) (*execution.Container, error) { + runcC, err := r.runc.State(ctx, id) if err != nil { return nil, err } @@ -217,9 +218,9 @@ func (r *OCIRuntime) Load(id string) (*execution.Container, error) { return r.load(runcC) } -func (r *OCIRuntime) Delete(c *execution.Container) error { +func (r *OCIRuntime) Delete(ctx context.Context, c *execution.Container) error { id := c.ID() - if err := r.runc.Delete(id); err != nil { + if err := r.runc.Delete(ctx, id); err != nil { return err } c.StateDir().Delete() @@ -228,15 +229,15 @@ func (r *OCIRuntime) Delete(c *execution.Container) error { return nil } -func (r *OCIRuntime) Pause(c *execution.Container) error { - return r.runc.Pause(c.ID()) +func (r *OCIRuntime) Pause(ctx context.Context, c *execution.Container) error { + return r.runc.Pause(ctx, c.ID()) } -func (r *OCIRuntime) Resume(c *execution.Container) error { - return r.runc.Resume(c.ID()) +func (r *OCIRuntime) Resume(ctx context.Context, c *execution.Container) error { + return r.runc.Resume(ctx, c.ID()) } -func (r *OCIRuntime) StartProcess(c *execution.Container, o execution.StartProcessOpts) (p execution.Process, err error) { +func (r *OCIRuntime) StartProcess(ctx context.Context, c *execution.Container, o execution.StartProcessOpts) (p execution.Process, err error) { rio, err := getRuncIO(o.Stdin, o.Stdout, o.Stderr) if err != nil { return nil, err @@ -271,7 +272,7 @@ func (r *OCIRuntime) StartProcess(c *execution.Container, o execution.StartProce }() pidFile := filepath.Join(processStateDir, "pid") - if err := r.runc.ExecProcess(c.ID(), o.Spec, &runc.ExecOpts{ + if err := r.runc.Exec(ctx, c.ID(), o.Spec, &runc.ExecOpts{ PidFile: pidFile, Detach: false, Console: consolePath, @@ -297,7 +298,7 @@ func (r *OCIRuntime) StartProcess(c *execution.Container, o execution.StartProce return process, nil } -func (r *OCIRuntime) SignalProcess(c *execution.Container, id string, sig os.Signal) error { +func (r *OCIRuntime) SignalProcess(ctx context.Context, c *execution.Container, id string, sig os.Signal) error { process := c.GetProcess(id) if process == nil { return fmt.Errorf("Make a Process Not Found error") @@ -305,7 +306,7 @@ func (r *OCIRuntime) SignalProcess(c *execution.Container, id string, sig os.Sig return syscall.Kill(int(process.Pid()), sig.(syscall.Signal)) } -func (r *OCIRuntime) DeleteProcess(c *execution.Container, id string) error { +func (r *OCIRuntime) DeleteProcess(ctx context.Context, c *execution.Container, id string) error { ioID := fmt.Sprintf("%s-%s", c.ID(), id) closeRuncIO(r.ios[ioID]) delete(r.ios, ioID) diff --git a/execution/service.go b/execution/service.go index 6486a4d..3e1f6ef 100644 --- a/execution/service.go +++ b/execution/service.go @@ -30,7 +30,7 @@ func (s *Service) Create(ctx context.Context, r *api.CreateContainerRequest) (*a // TODO: write io and bundle path to dir var err error - container, err := s.executor.Create(r.ID, CreateOpts{ + container, err := s.executor.Create(ctx, r.ID, CreateOpts{ Bundle: r.BundlePath, Console: r.Console, Stdin: r.Stdin, @@ -49,19 +49,19 @@ func (s *Service) Create(ctx context.Context, r *api.CreateContainerRequest) (*a } func (s *Service) Delete(ctx context.Context, r *api.DeleteContainerRequest) (*google_protobuf.Empty, error) { - container, err := s.executor.Load(r.ID) + container, err := s.executor.Load(ctx, r.ID) if err != nil { return emptyResponse, err } - if err = s.executor.Delete(container); err != nil { + if err = s.executor.Delete(ctx, container); err != nil { return emptyResponse, err } return emptyResponse, nil } func (s *Service) List(ctx context.Context, r *api.ListContainersRequest) (*api.ListContainersResponse, error) { - containers, err := s.executor.List() + containers, err := s.executor.List(ctx) if err != nil { return nil, err } @@ -72,7 +72,7 @@ func (s *Service) List(ctx context.Context, r *api.ListContainersRequest) (*api. return resp, nil } func (s *Service) Get(ctx context.Context, r *api.GetContainerRequest) (*api.GetContainerResponse, error) { - container, err := s.executor.Load(r.ID) + container, err := s.executor.Load(ctx, r.ID) if err != nil { return nil, err } @@ -86,31 +86,31 @@ func (s *Service) Update(ctx context.Context, r *api.UpdateContainerRequest) (*g } func (s *Service) Pause(ctx context.Context, r *api.PauseContainerRequest) (*google_protobuf.Empty, error) { - container, err := s.executor.Load(r.ID) + container, err := s.executor.Load(ctx, r.ID) if err != nil { return nil, err } - return emptyResponse, s.executor.Pause(container) + return emptyResponse, s.executor.Pause(ctx, container) } func (s *Service) Resume(ctx context.Context, r *api.ResumeContainerRequest) (*google_protobuf.Empty, error) { - container, err := s.executor.Load(r.ID) + container, err := s.executor.Load(ctx, r.ID) if err != nil { return nil, err } - return emptyResponse, s.executor.Resume(container) + return emptyResponse, s.executor.Resume(ctx, container) } func (s *Service) Start(ctx context.Context, r *api.StartContainerRequest) (*google_protobuf.Empty, error) { - container, err := s.executor.Load(r.ID) + container, err := s.executor.Load(ctx, r.ID) if err != nil { return nil, err } - return emptyResponse, s.executor.Start(container) + return emptyResponse, s.executor.Start(ctx, container) } func (s *Service) StartProcess(ctx context.Context, r *api.StartProcessRequest) (*api.StartProcessResponse, error) { - container, err := s.executor.Load(r.ContainerId) + container, err := s.executor.Load(ctx, r.ContainerId) if err != nil { return nil, err } @@ -128,7 +128,7 @@ func (s *Service) StartProcess(ctx context.Context, r *api.StartProcessRequest) NoNewPrivileges: true, } - process, err := s.executor.StartProcess(container, StartProcessOpts{ + process, err := s.executor.StartProcess(ctx, container, StartProcessOpts{ Spec: spec, Console: r.Console, Stdin: r.Stdin, @@ -147,7 +147,7 @@ func (s *Service) StartProcess(ctx context.Context, r *api.StartProcessRequest) // containerd managed execs + system pids forked in container func (s *Service) GetProcess(ctx context.Context, r *api.GetProcessRequest) (*api.GetProcessResponse, error) { - container, err := s.executor.Load(r.Container.ID) + container, err := s.executor.Load(ctx, r.Container.ID) if err != nil { return nil, err } @@ -161,7 +161,7 @@ func (s *Service) GetProcess(ctx context.Context, r *api.GetProcessRequest) (*ap } func (s *Service) SignalProcess(ctx context.Context, r *api.SignalProcessRequest) (*google_protobuf.Empty, error) { - container, err := s.executor.Load(r.Container.ID) + container, err := s.executor.Load(ctx, r.Container.ID) if err != nil { return emptyResponse, err } @@ -173,18 +173,18 @@ func (s *Service) SignalProcess(ctx context.Context, r *api.SignalProcessRequest } func (s *Service) DeleteProcess(ctx context.Context, r *api.DeleteProcessRequest) (*google_protobuf.Empty, error) { - container, err := s.executor.Load(r.Container.ID) + container, err := s.executor.Load(ctx, r.Container.ID) if err != nil { return emptyResponse, err } - if err := s.executor.DeleteProcess(container, r.Process.ID); err != nil { + if err := s.executor.DeleteProcess(ctx, container, r.Process.ID); err != nil { return emptyResponse, err } return emptyResponse, nil } func (s *Service) ListProcesses(ctx context.Context, r *api.ListProcessesRequest) (*api.ListProcessesResponse, error) { - container, err := s.executor.Load(r.Container.ID) + container, err := s.executor.Load(ctx, r.Container.ID) if err != nil { return nil, err }