diff --git a/runtime/v2/example/cmd/main.go b/runtime/v2/example/cmd/main.go index 187258d05..3240e7870 100644 --- a/runtime/v2/example/cmd/main.go +++ b/runtime/v2/example/cmd/main.go @@ -19,11 +19,14 @@ package main import ( - "github.com/containerd/containerd/runtime/v2/example" + "context" + + _ "github.com/containerd/containerd/runtime/v2/example" + "github.com/containerd/containerd/runtime/v2/runc/manager" "github.com/containerd/containerd/runtime/v2/shim" ) func main() { // init and execute the shim - shim.Run("io.containerd.example.v1", example.New) + shim.Run(context.Background(), manager.NewShimManager("io.containerd.example.v1")) } diff --git a/runtime/v2/example/example.go b/runtime/v2/example/example.go index 2d6d66092..2fea9c5da 100644 --- a/runtime/v2/example/example.go +++ b/runtime/v2/example/example.go @@ -24,115 +24,141 @@ import ( taskAPI "github.com/containerd/containerd/api/runtime/task/v2" "github.com/containerd/containerd/errdefs" + "github.com/containerd/containerd/pkg/shutdown" + "github.com/containerd/containerd/plugin" ptypes "github.com/containerd/containerd/protobuf/types" "github.com/containerd/containerd/runtime/v2/shim" ) var ( - // check to make sure the *service implements the GRPC API - _ = (taskAPI.TaskService)(&service{}) + // check to make sure the *exampleTaskService implements the GRPC API + _ = (taskAPI.TaskService)(&exampleTaskService{}) ) -// New returns a new shim service -func New(ctx context.Context, id string, publisher shim.Publisher, shutdown func()) (shim.Shim, error) { - return &service{}, nil +func init() { + plugin.Register(&plugin.Registration{ + Type: plugin.TTRPCPlugin, + ID: "task", + Requires: []plugin.Type{ + plugin.EventPlugin, + plugin.InternalPlugin, + }, + InitFn: func(ic *plugin.InitContext) (interface{}, error) { + pp, err := ic.GetByID(plugin.EventPlugin, "publisher") + if err != nil { + return nil, err + } + ss, err := ic.GetByID(plugin.InternalPlugin, "shutdown") + if err != nil { + return nil, err + } + return NewTaskService(ic.Context, pp.(shim.Publisher), ss.(shutdown.Service)) + }, + }) } -type service struct { +// NewTaskService creates a new instance of a task service +func NewTaskService(ctx context.Context, publisher shim.Publisher, sd shutdown.Service) (taskAPI.TaskService, error) { + // The shim.Publisher and shutdown.Service are usually useful for your task service, + // but we don't need them in the exampleTaskService. + return &exampleTaskService{}, nil +} + +type exampleTaskService struct { } // StartShim is a binary call that executes a new shim returning the address -func (s *service) StartShim(ctx context.Context, opts shim.StartOpts) (string, error) { +func (s *exampleTaskService) StartShim(ctx context.Context, opts shim.StartOpts) (string, error) { return "", nil } // Cleanup is a binary call that cleans up any resources used by the shim when the service crashes -func (s *service) Cleanup(ctx context.Context) (*taskAPI.DeleteResponse, error) { +func (s *exampleTaskService) Cleanup(ctx context.Context) (*taskAPI.DeleteResponse, error) { return nil, errdefs.ErrNotImplemented } // Create a new container -func (s *service) Create(ctx context.Context, r *taskAPI.CreateTaskRequest) (_ *taskAPI.CreateTaskResponse, err error) { +func (s *exampleTaskService) Create(ctx context.Context, r *taskAPI.CreateTaskRequest) (_ *taskAPI.CreateTaskResponse, err error) { return nil, errdefs.ErrNotImplemented } // Start the primary user process inside the container -func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (*taskAPI.StartResponse, error) { +func (s *exampleTaskService) Start(ctx context.Context, r *taskAPI.StartRequest) (*taskAPI.StartResponse, error) { return nil, errdefs.ErrNotImplemented } // Delete a process or container -func (s *service) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (*taskAPI.DeleteResponse, error) { +func (s *exampleTaskService) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (*taskAPI.DeleteResponse, error) { return nil, errdefs.ErrNotImplemented } // Exec an additional process inside the container -func (s *service) Exec(ctx context.Context, r *taskAPI.ExecProcessRequest) (*ptypes.Empty, error) { +func (s *exampleTaskService) Exec(ctx context.Context, r *taskAPI.ExecProcessRequest) (*ptypes.Empty, error) { return nil, errdefs.ErrNotImplemented } // ResizePty of a process -func (s *service) ResizePty(ctx context.Context, r *taskAPI.ResizePtyRequest) (*ptypes.Empty, error) { +func (s *exampleTaskService) ResizePty(ctx context.Context, r *taskAPI.ResizePtyRequest) (*ptypes.Empty, error) { return nil, errdefs.ErrNotImplemented } // State returns runtime state of a process -func (s *service) State(ctx context.Context, r *taskAPI.StateRequest) (*taskAPI.StateResponse, error) { +func (s *exampleTaskService) State(ctx context.Context, r *taskAPI.StateRequest) (*taskAPI.StateResponse, error) { return nil, errdefs.ErrNotImplemented } // Pause the container -func (s *service) Pause(ctx context.Context, r *taskAPI.PauseRequest) (*ptypes.Empty, error) { +func (s *exampleTaskService) Pause(ctx context.Context, r *taskAPI.PauseRequest) (*ptypes.Empty, error) { return nil, errdefs.ErrNotImplemented } // Resume the container -func (s *service) Resume(ctx context.Context, r *taskAPI.ResumeRequest) (*ptypes.Empty, error) { +func (s *exampleTaskService) Resume(ctx context.Context, r *taskAPI.ResumeRequest) (*ptypes.Empty, error) { return nil, errdefs.ErrNotImplemented } // Kill a process -func (s *service) Kill(ctx context.Context, r *taskAPI.KillRequest) (*ptypes.Empty, error) { +func (s *exampleTaskService) Kill(ctx context.Context, r *taskAPI.KillRequest) (*ptypes.Empty, error) { return nil, errdefs.ErrNotImplemented } // Pids returns all pids inside the container -func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (*taskAPI.PidsResponse, error) { +func (s *exampleTaskService) Pids(ctx context.Context, r *taskAPI.PidsRequest) (*taskAPI.PidsResponse, error) { return nil, errdefs.ErrNotImplemented } // CloseIO of a process -func (s *service) CloseIO(ctx context.Context, r *taskAPI.CloseIORequest) (*ptypes.Empty, error) { +func (s *exampleTaskService) CloseIO(ctx context.Context, r *taskAPI.CloseIORequest) (*ptypes.Empty, error) { return nil, errdefs.ErrNotImplemented } // Checkpoint the container -func (s *service) Checkpoint(ctx context.Context, r *taskAPI.CheckpointTaskRequest) (*ptypes.Empty, error) { +func (s *exampleTaskService) Checkpoint(ctx context.Context, r *taskAPI.CheckpointTaskRequest) (*ptypes.Empty, error) { return nil, errdefs.ErrNotImplemented } // Connect returns shim information of the underlying service -func (s *service) Connect(ctx context.Context, r *taskAPI.ConnectRequest) (*taskAPI.ConnectResponse, error) { +func (s *exampleTaskService) Connect(ctx context.Context, r *taskAPI.ConnectRequest) (*taskAPI.ConnectResponse, error) { return nil, errdefs.ErrNotImplemented } // Shutdown is called after the underlying resources of the shim are cleaned up and the service can be stopped -func (s *service) Shutdown(ctx context.Context, r *taskAPI.ShutdownRequest) (*ptypes.Empty, error) { +func (s *exampleTaskService) Shutdown(ctx context.Context, r *taskAPI.ShutdownRequest) (*ptypes.Empty, error) { os.Exit(0) return &ptypes.Empty{}, nil } // Stats returns container level system stats for a container and its processes -func (s *service) Stats(ctx context.Context, r *taskAPI.StatsRequest) (*taskAPI.StatsResponse, error) { +func (s *exampleTaskService) Stats(ctx context.Context, r *taskAPI.StatsRequest) (*taskAPI.StatsResponse, error) { return nil, errdefs.ErrNotImplemented } // Update the live container -func (s *service) Update(ctx context.Context, r *taskAPI.UpdateTaskRequest) (*ptypes.Empty, error) { +func (s *exampleTaskService) Update(ctx context.Context, r *taskAPI.UpdateTaskRequest) (*ptypes.Empty, error) { return nil, errdefs.ErrNotImplemented } // Wait for a process to exit -func (s *service) Wait(ctx context.Context, r *taskAPI.WaitRequest) (*taskAPI.WaitResponse, error) { +func (s *exampleTaskService) Wait(ctx context.Context, r *taskAPI.WaitRequest) (*taskAPI.WaitResponse, error) { return nil, errdefs.ErrNotImplemented }