api: introduce api/{types,services} for deduplication of API definition

Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
This commit is contained in:
Akihiro Suda 2017-02-09 07:31:26 +00:00
parent 6527a57d7b
commit bf8abef70f
37 changed files with 2179 additions and 1753 deletions

View file

@ -9,7 +9,7 @@ import (
"github.com/crosbymichael/console"
runc "github.com/crosbymichael/go-runc"
apishim "github.com/docker/containerd/api/shim"
shimapi "github.com/docker/containerd/api/services/shim"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
@ -25,7 +25,7 @@ type execProcess struct {
parent *initProcess
}
func newExecProcess(context context.Context, r *apishim.ExecRequest, parent *initProcess, id int) (process, error) {
func newExecProcess(context context.Context, r *shimapi.ExecRequest, parent *initProcess, id int) (process, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
@ -82,7 +82,7 @@ func newExecProcess(context context.Context, r *apishim.ExecRequest, parent *ini
return e, nil
}
func processFromRequest(r *apishim.ExecRequest) specs.Process {
func processFromRequest(r *shimapi.ExecRequest) specs.Process {
var user specs.User
if r.User != nil {
user.UID = r.User.Uid
@ -103,7 +103,7 @@ func processFromRequest(r *apishim.ExecRequest) specs.Process {
}
}
func rlimits(rr []*apishim.Rlimit) (o []specs.LinuxRlimit) {
func rlimits(rr []*shimapi.Rlimit) (o []specs.LinuxRlimit) {
for _, r := range rr {
o = append(o, specs.LinuxRlimit{
Type: r.Type,

View file

@ -9,7 +9,7 @@ import (
"github.com/crosbymichael/console"
runc "github.com/crosbymichael/go-runc"
apishim "github.com/docker/containerd/api/shim"
shimapi "github.com/docker/containerd/api/services/shim"
)
type initProcess struct {
@ -24,7 +24,7 @@ type initProcess struct {
pid int
}
func newInitProcess(context context.Context, r *apishim.CreateRequest) (*initProcess, error) {
func newInitProcess(context context.Context, r *shimapi.CreateRequest) (*initProcess, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err

View file

@ -5,7 +5,9 @@ import (
"syscall"
"github.com/crosbymichael/console"
apishim "github.com/docker/containerd/api/shim"
shimapi "github.com/docker/containerd/api/services/shim"
processapi "github.com/docker/containerd/api/types/process"
stateapi "github.com/docker/containerd/api/types/state"
"github.com/docker/containerd/utils"
google_protobuf "github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
@ -18,7 +20,7 @@ var emptyResponse = &google_protobuf.Empty{}
func New() *Service {
return &Service{
processes: make(map[int]process),
events: make(chan *apishim.Event, 4096),
events: make(chan *shimapi.Event, 4096),
}
}
@ -28,11 +30,11 @@ type Service struct {
bundle string
mu sync.Mutex
processes map[int]process
events chan *apishim.Event
events chan *shimapi.Event
execID int
}
func (s *Service) Create(ctx context.Context, r *apishim.CreateRequest) (*apishim.CreateResponse, error) {
func (s *Service) Create(ctx context.Context, r *shimapi.CreateRequest) (*shimapi.CreateResponse, error) {
process, err := newInitProcess(ctx, r)
if err != nil {
return nil, err
@ -44,29 +46,29 @@ func (s *Service) Create(ctx context.Context, r *apishim.CreateRequest) (*apishi
pid := process.Pid()
s.processes[pid] = process
s.mu.Unlock()
s.events <- &apishim.Event{
Type: apishim.EventType_CREATE,
s.events <- &shimapi.Event{
Type: shimapi.EventType_CREATE,
ID: r.ID,
Pid: uint32(pid),
}
return &apishim.CreateResponse{
return &shimapi.CreateResponse{
Pid: uint32(pid),
}, nil
}
func (s *Service) Start(ctx context.Context, r *apishim.StartRequest) (*google_protobuf.Empty, error) {
func (s *Service) Start(ctx context.Context, r *shimapi.StartRequest) (*google_protobuf.Empty, error) {
if err := s.initProcess.Start(ctx); err != nil {
return nil, err
}
s.events <- &apishim.Event{
Type: apishim.EventType_START,
s.events <- &shimapi.Event{
Type: shimapi.EventType_START,
ID: s.id,
Pid: uint32(s.initProcess.Pid()),
}
return emptyResponse, nil
}
func (s *Service) Delete(ctx context.Context, r *apishim.DeleteRequest) (*apishim.DeleteResponse, error) {
func (s *Service) Delete(ctx context.Context, r *shimapi.DeleteRequest) (*shimapi.DeleteResponse, error) {
s.mu.Lock()
p, ok := s.processes[int(r.Pid)]
s.mu.Unlock()
@ -79,12 +81,12 @@ func (s *Service) Delete(ctx context.Context, r *apishim.DeleteRequest) (*apishi
s.mu.Lock()
delete(s.processes, p.Pid())
s.mu.Unlock()
return &apishim.DeleteResponse{
return &shimapi.DeleteResponse{
ExitStatus: uint32(p.Status()),
}, nil
}
func (s *Service) Exec(ctx context.Context, r *apishim.ExecRequest) (*apishim.ExecResponse, error) {
func (s *Service) Exec(ctx context.Context, r *shimapi.ExecRequest) (*shimapi.ExecResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.execID++
@ -94,17 +96,17 @@ func (s *Service) Exec(ctx context.Context, r *apishim.ExecRequest) (*apishim.Ex
}
pid := process.Pid()
s.processes[pid] = process
s.events <- &apishim.Event{
Type: apishim.EventType_EXEC_ADDED,
s.events <- &shimapi.Event{
Type: shimapi.EventType_EXEC_ADDED,
ID: s.id,
Pid: uint32(pid),
}
return &apishim.ExecResponse{
return &shimapi.ExecResponse{
Pid: uint32(pid),
}, nil
}
func (s *Service) Pty(ctx context.Context, r *apishim.PtyRequest) (*google_protobuf.Empty, error) {
func (s *Service) Pty(ctx context.Context, r *shimapi.PtyRequest) (*google_protobuf.Empty, error) {
if r.Pid == 0 {
return nil, errors.Errorf("pid not provided in request")
}
@ -124,7 +126,7 @@ func (s *Service) Pty(ctx context.Context, r *apishim.PtyRequest) (*google_proto
return emptyResponse, nil
}
func (s *Service) Events(r *apishim.EventsRequest, stream apishim.Shim_EventsServer) error {
func (s *Service) Events(r *shimapi.EventsRequest, stream shimapi.Shim_EventsServer) error {
for e := range s.events {
if err := stream.Send(e); err != nil {
return err
@ -133,24 +135,24 @@ func (s *Service) Events(r *apishim.EventsRequest, stream apishim.Shim_EventsSer
return nil
}
func (s *Service) State(ctx context.Context, r *apishim.StateRequest) (*apishim.StateResponse, error) {
o := &apishim.StateResponse{
func (s *Service) State(ctx context.Context, r *shimapi.StateRequest) (*shimapi.StateResponse, error) {
o := &shimapi.StateResponse{
ID: s.id,
Bundle: s.bundle,
InitPid: uint32(s.initProcess.Pid()),
Processes: []*apishim.Process{},
Processes: []*processapi.Process{},
}
s.mu.Lock()
defer s.mu.Unlock()
for _, p := range s.processes {
state := apishim.State_RUNNING
state := stateapi.State_RUNNING
if err := syscall.Kill(p.Pid(), 0); err != nil {
if err != syscall.ESRCH {
return nil, err
}
state = apishim.State_STOPPED
state = stateapi.State_STOPPED
}
o.Processes = append(o.Processes, &apishim.Process{
o.Processes = append(o.Processes, &processapi.Process{
Pid: uint32(p.Pid()),
State: state,
})
@ -158,14 +160,14 @@ func (s *Service) State(ctx context.Context, r *apishim.StateRequest) (*apishim.
return o, nil
}
func (s *Service) Pause(ctx context.Context, r *apishim.PauseRequest) (*google_protobuf.Empty, error) {
func (s *Service) Pause(ctx context.Context, r *shimapi.PauseRequest) (*google_protobuf.Empty, error) {
if err := s.initProcess.Pause(ctx); err != nil {
return nil, err
}
return emptyResponse, nil
}
func (s *Service) Resume(ctx context.Context, r *apishim.ResumeRequest) (*google_protobuf.Empty, error) {
func (s *Service) Resume(ctx context.Context, r *shimapi.ResumeRequest) (*google_protobuf.Empty, error) {
if err := s.initProcess.Resume(ctx); err != nil {
return nil, err
}
@ -176,8 +178,8 @@ func (s *Service) ProcessExit(e utils.Exit) error {
s.mu.Lock()
if p, ok := s.processes[e.Pid]; ok {
p.Exited(e.Status)
s.events <- &apishim.Event{
Type: apishim.EventType_EXIT,
s.events <- &shimapi.Event{
Type: shimapi.EventType_EXIT,
ID: s.id,
Pid: uint32(p.Pid()),
ExitStatus: uint32(e.Status),