From d619954a2b845b97dfb35ea4884c9bcc9bde9c3d Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 24 Jan 2017 15:55:32 -0800 Subject: [PATCH] Move shim service into top lvl package Signed-off-by: Michael Crosby --- cmd/containerd-shim/checkpoint.go | 38 ------------------- cmd/containerd-shim/main.go | 11 +++--- {cmd/containerd-shim => shim}/exec.go | 10 ++--- {cmd/containerd-shim => shim}/init.go | 6 +-- .../process_linux.go => shim/io.go | 2 +- {cmd/containerd-shim => shim}/process.go | 5 +-- {cmd/containerd-shim => shim}/service.go | 30 +++++++++------ 7 files changed, 33 insertions(+), 69 deletions(-) delete mode 100644 cmd/containerd-shim/checkpoint.go rename {cmd/containerd-shim => shim}/exec.go (88%) rename {cmd/containerd-shim => shim}/init.go (93%) rename cmd/containerd-shim/process_linux.go => shim/io.go (99%) rename {cmd/containerd-shim => shim}/process.go (85%) rename {cmd/containerd-shim => shim}/service.go (65%) diff --git a/cmd/containerd-shim/checkpoint.go b/cmd/containerd-shim/checkpoint.go deleted file mode 100644 index d423319..0000000 --- a/cmd/containerd-shim/checkpoint.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -import ( - "encoding/json" - "os" - "path/filepath" - "time" -) - -type checkpoint struct { - // Timestamp is the time that checkpoint happened - Created time.Time `json:"created"` - // Name is the name of the checkpoint - Name string `json:"name"` - // TCP checkpoints open tcp connections - TCP bool `json:"tcp"` - // UnixSockets persists unix sockets in the checkpoint - UnixSockets bool `json:"unixSockets"` - // Shell persists tty sessions in the checkpoint - Shell bool `json:"shell"` - // Exit exits the container after the checkpoint is finished - Exit bool `json:"exit"` - // EmptyNS tells CRIU not to restore a particular namespace - EmptyNS []string `json:"emptyNS,omitempty"` -} - -func loadCheckpoint(checkpointPath string) (*checkpoint, error) { - f, err := os.Open(filepath.Join(checkpointPath, "config.json")) - if err != nil { - return nil, err - } - defer f.Close() - var cpt checkpoint - if err := json.NewDecoder(f).Decode(&cpt); err != nil { - return nil, err - } - return &cpt, nil -} diff --git a/cmd/containerd-shim/main.go b/cmd/containerd-shim/main.go index 4a4d951..e61c2e3 100644 --- a/cmd/containerd-shim/main.go +++ b/cmd/containerd-shim/main.go @@ -10,7 +10,8 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/containerd" - "github.com/docker/containerd/api/shim" + apishim "github.com/docker/containerd/api/shim" + "github.com/docker/containerd/shim" "github.com/docker/containerd/sys" "github.com/docker/containerd/utils" "github.com/urfave/cli" @@ -52,11 +53,9 @@ func main() { } var ( server = grpc.NewServer() - sv = &service{ - processes: make(map[int]process), - } + sv = shim.NewService() ) - shim.RegisterShimServer(server, sv) + apishim.RegisterShimServer(server, sv) l, err := utils.CreateUnixSocket("shim.sock") if err != nil { return err @@ -77,7 +76,7 @@ func main() { logrus.WithError(err).Error("reap exit status") } for _, e := range exits { - if err := sv.processExited(e); err != nil { + if err := sv.ProcessExit(e); err != nil { return err } } diff --git a/cmd/containerd-shim/exec.go b/shim/exec.go similarity index 88% rename from cmd/containerd-shim/exec.go rename to shim/exec.go index 7dcaca9..2991d13 100644 --- a/cmd/containerd-shim/exec.go +++ b/shim/exec.go @@ -1,4 +1,4 @@ -package main +package shim import ( "context" @@ -8,7 +8,7 @@ import ( "sync" runc "github.com/crosbymichael/go-runc" - "github.com/docker/containerd/api/shim" + apishim "github.com/docker/containerd/api/shim" specs "github.com/opencontainers/runtime-spec/specs-go" ) @@ -24,7 +24,7 @@ type execProcess struct { parent *initProcess } -func newExecProcess(context context.Context, r *shim.ExecRequest, parent *initProcess) (process, error) { +func newExecProcess(context context.Context, r *apishim.ExecRequest, parent *initProcess) (process, error) { cwd, err := os.Getwd() if err != nil { return nil, err @@ -67,7 +67,7 @@ func newExecProcess(context context.Context, r *shim.ExecRequest, parent *initPr return e, nil } -func processFromRequest(r *shim.ExecRequest) specs.Process { +func processFromRequest(r *apishim.ExecRequest) specs.Process { return specs.Process{ Terminal: r.Terminal, User: specs.User{ @@ -86,7 +86,7 @@ func processFromRequest(r *shim.ExecRequest) specs.Process { } } -func rlimits(rr []*shim.Rlimit) (o []specs.LinuxRlimit) { +func rlimits(rr []*apishim.Rlimit) (o []specs.LinuxRlimit) { for _, r := range rr { o = append(o, specs.LinuxRlimit{ Type: r.Type, diff --git a/cmd/containerd-shim/init.go b/shim/init.go similarity index 93% rename from cmd/containerd-shim/init.go rename to shim/init.go index 828b227..f2d3887 100644 --- a/cmd/containerd-shim/init.go +++ b/shim/init.go @@ -1,4 +1,4 @@ -package main +package shim import ( "context" @@ -8,7 +8,7 @@ import ( "syscall" runc "github.com/crosbymichael/go-runc" - "github.com/docker/containerd/api/shim" + apishim "github.com/docker/containerd/api/shim" ) type initProcess struct { @@ -23,7 +23,7 @@ type initProcess struct { pid int } -func newInitProcess(context context.Context, r *shim.CreateRequest) (process, error) { +func newInitProcess(context context.Context, r *apishim.CreateRequest) (process, error) { cwd, err := os.Getwd() if err != nil { return nil, err diff --git a/cmd/containerd-shim/process_linux.go b/shim/io.go similarity index 99% rename from cmd/containerd-shim/process_linux.go rename to shim/io.go index 656bc33..39f641d 100644 --- a/cmd/containerd-shim/process_linux.go +++ b/shim/io.go @@ -1,4 +1,4 @@ -package main +package shim import ( "context" diff --git a/cmd/containerd-shim/process.go b/shim/process.go similarity index 85% rename from cmd/containerd-shim/process.go rename to shim/process.go index ff38442..a476e88 100644 --- a/cmd/containerd-shim/process.go +++ b/shim/process.go @@ -1,14 +1,11 @@ -package main +package shim import ( "context" - "errors" runc "github.com/crosbymichael/go-runc" ) -var errRuntime = errors.New("shim: runtime execution error") - type process interface { // Pid returns the pid for the process Pid() int diff --git a/cmd/containerd-shim/service.go b/shim/service.go similarity index 65% rename from cmd/containerd-shim/service.go rename to shim/service.go index d66582b..9458a35 100644 --- a/cmd/containerd-shim/service.go +++ b/shim/service.go @@ -1,11 +1,11 @@ -package main +package shim import ( "fmt" "sync" runc "github.com/crosbymichael/go-runc" - "github.com/docker/containerd/api/shim" + apishim "github.com/docker/containerd/api/shim" "github.com/docker/containerd/utils" google_protobuf "github.com/golang/protobuf/ptypes/empty" "golang.org/x/net/context" @@ -13,13 +13,19 @@ import ( var emptyResponse = &google_protobuf.Empty{} -type service struct { +func NewService() *Service { + return &Service{ + processes: make(map[int]process), + } +} + +type Service struct { initPid int mu sync.Mutex processes map[int]process } -func (s *service) Create(ctx context.Context, r *shim.CreateRequest) (*shim.CreateResponse, error) { +func (s *Service) Create(ctx context.Context, r *apishim.CreateRequest) (*apishim.CreateResponse, error) { process, err := newInitProcess(ctx, r) if err != nil { return nil, err @@ -28,12 +34,12 @@ func (s *service) Create(ctx context.Context, r *shim.CreateRequest) (*shim.Crea pid := process.Pid() s.initPid, s.processes[pid] = pid, process s.mu.Unlock() - return &shim.CreateResponse{ + return &apishim.CreateResponse{ Pid: uint32(pid), }, nil } -func (s *service) Start(ctx context.Context, r *shim.StartRequest) (*google_protobuf.Empty, error) { +func (s *Service) Start(ctx context.Context, r *apishim.StartRequest) (*google_protobuf.Empty, error) { s.mu.Lock() p := s.processes[s.initPid] s.mu.Unlock() @@ -43,7 +49,7 @@ func (s *service) Start(ctx context.Context, r *shim.StartRequest) (*google_prot return emptyResponse, nil } -func (s *service) Delete(ctx context.Context, r *shim.DeleteRequest) (*shim.DeleteResponse, error) { +func (s *Service) Delete(ctx context.Context, r *apishim.DeleteRequest) (*apishim.DeleteResponse, error) { s.mu.Lock() p, ok := s.processes[int(r.Pid)] s.mu.Unlock() @@ -56,12 +62,12 @@ func (s *service) Delete(ctx context.Context, r *shim.DeleteRequest) (*shim.Dele s.mu.Lock() delete(s.processes, int(r.Pid)) s.mu.Unlock() - return &shim.DeleteResponse{ + return &apishim.DeleteResponse{ ExitStatus: uint32(p.Status()), }, nil } -func (s *service) Exec(ctx context.Context, r *shim.ExecRequest) (*shim.ExecResponse, error) { +func (s *Service) Exec(ctx context.Context, r *apishim.ExecRequest) (*apishim.ExecResponse, error) { s.mu.Lock() defer s.mu.Unlock() process, err := newExecProcess(ctx, r, s.processes[s.initPid].(*initProcess)) @@ -70,12 +76,12 @@ func (s *service) Exec(ctx context.Context, r *shim.ExecRequest) (*shim.ExecResp } pid := process.Pid() s.processes[pid] = process - return &shim.ExecResponse{ + return &apishim.ExecResponse{ Pid: uint32(pid), }, nil } -func (s *service) Pty(ctx context.Context, r *shim.PtyRequest) (*google_protobuf.Empty, error) { +func (s *Service) Pty(ctx context.Context, r *apishim.PtyRequest) (*google_protobuf.Empty, error) { ws := runc.WinSize{ Width: uint16(r.Width), Height: uint16(r.Height), @@ -92,7 +98,7 @@ func (s *service) Pty(ctx context.Context, r *shim.PtyRequest) (*google_protobuf return emptyResponse, nil } -func (s *service) processExited(e utils.Exit) error { +func (s *Service) ProcessExit(e utils.Exit) error { s.mu.Lock() if p, ok := s.processes[e.Pid]; ok { p.Exited(e.Status)