180 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			180 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
/*
 | 
						|
   Copyright The containerd Authors.
 | 
						|
 | 
						|
   Licensed under the Apache License, Version 2.0 (the "License");
 | 
						|
   you may not use this file except in compliance with the License.
 | 
						|
   You may obtain a copy of the License at
 | 
						|
 | 
						|
       http://www.apache.org/licenses/LICENSE-2.0
 | 
						|
 | 
						|
   Unless required by applicable law or agreed to in writing, software
 | 
						|
   distributed under the License is distributed on an "AS IS" BASIS,
 | 
						|
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
						|
   See the License for the specific language governing permissions and
 | 
						|
   limitations under the License.
 | 
						|
*/
 | 
						|
 | 
						|
package example
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"os"
 | 
						|
 | 
						|
	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"
 | 
						|
	"github.com/containerd/containerd/plugin/registry"
 | 
						|
	"github.com/containerd/containerd/plugins"
 | 
						|
	ptypes "github.com/containerd/containerd/protobuf/types"
 | 
						|
	"github.com/containerd/containerd/runtime/v2/shim"
 | 
						|
	"github.com/containerd/ttrpc"
 | 
						|
)
 | 
						|
 | 
						|
func init() {
 | 
						|
	registry.Register(&plugin.Registration{
 | 
						|
		Type: plugins.TTRPCPlugin,
 | 
						|
		ID:   "task",
 | 
						|
		Requires: []plugin.Type{
 | 
						|
			plugins.EventPlugin,
 | 
						|
			plugins.InternalPlugin,
 | 
						|
		},
 | 
						|
		InitFn: func(ic *plugin.InitContext) (interface{}, error) {
 | 
						|
			pp, err := ic.GetByID(plugins.EventPlugin, "publisher")
 | 
						|
			if err != nil {
 | 
						|
				return nil, err
 | 
						|
			}
 | 
						|
			ss, err := ic.GetByID(plugins.InternalPlugin, "shutdown")
 | 
						|
			if err != nil {
 | 
						|
				return nil, err
 | 
						|
			}
 | 
						|
			return newTaskService(ic.Context, pp.(shim.Publisher), ss.(shutdown.Service))
 | 
						|
		},
 | 
						|
	})
 | 
						|
}
 | 
						|
 | 
						|
func NewManager(name string) shim.Manager {
 | 
						|
	return manager{name: name}
 | 
						|
}
 | 
						|
 | 
						|
type manager struct {
 | 
						|
	name string
 | 
						|
}
 | 
						|
 | 
						|
func (m manager) Name() string {
 | 
						|
	return m.name
 | 
						|
}
 | 
						|
 | 
						|
func (m manager) Start(ctx context.Context, id string, opts shim.StartOpts) (shim.BootstrapParams, error) {
 | 
						|
	return shim.BootstrapParams{}, errdefs.ErrNotImplemented
 | 
						|
}
 | 
						|
 | 
						|
func (m manager) Stop(ctx context.Context, id string) (shim.StopStatus, error) {
 | 
						|
	return shim.StopStatus{}, errdefs.ErrNotImplemented
 | 
						|
}
 | 
						|
 | 
						|
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
 | 
						|
}
 | 
						|
 | 
						|
var (
 | 
						|
	_ = shim.TTRPCService(&exampleTaskService{})
 | 
						|
)
 | 
						|
 | 
						|
type exampleTaskService struct {
 | 
						|
}
 | 
						|
 | 
						|
// RegisterTTRPC allows TTRPC services to be registered with the underlying server
 | 
						|
func (s *exampleTaskService) RegisterTTRPC(server *ttrpc.Server) error {
 | 
						|
	taskAPI.RegisterTaskService(server, s)
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
// Create a new container
 | 
						|
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 *exampleTaskService) Start(ctx context.Context, r *taskAPI.StartRequest) (*taskAPI.StartResponse, error) {
 | 
						|
	return nil, errdefs.ErrNotImplemented
 | 
						|
}
 | 
						|
 | 
						|
// Delete a process or container
 | 
						|
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 *exampleTaskService) Exec(ctx context.Context, r *taskAPI.ExecProcessRequest) (*ptypes.Empty, error) {
 | 
						|
	return nil, errdefs.ErrNotImplemented
 | 
						|
}
 | 
						|
 | 
						|
// ResizePty of a process
 | 
						|
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 *exampleTaskService) State(ctx context.Context, r *taskAPI.StateRequest) (*taskAPI.StateResponse, error) {
 | 
						|
	return nil, errdefs.ErrNotImplemented
 | 
						|
}
 | 
						|
 | 
						|
// Pause the container
 | 
						|
func (s *exampleTaskService) Pause(ctx context.Context, r *taskAPI.PauseRequest) (*ptypes.Empty, error) {
 | 
						|
	return nil, errdefs.ErrNotImplemented
 | 
						|
}
 | 
						|
 | 
						|
// Resume the container
 | 
						|
func (s *exampleTaskService) Resume(ctx context.Context, r *taskAPI.ResumeRequest) (*ptypes.Empty, error) {
 | 
						|
	return nil, errdefs.ErrNotImplemented
 | 
						|
}
 | 
						|
 | 
						|
// Kill a process
 | 
						|
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 *exampleTaskService) Pids(ctx context.Context, r *taskAPI.PidsRequest) (*taskAPI.PidsResponse, error) {
 | 
						|
	return nil, errdefs.ErrNotImplemented
 | 
						|
}
 | 
						|
 | 
						|
// CloseIO of a process
 | 
						|
func (s *exampleTaskService) CloseIO(ctx context.Context, r *taskAPI.CloseIORequest) (*ptypes.Empty, error) {
 | 
						|
	return nil, errdefs.ErrNotImplemented
 | 
						|
}
 | 
						|
 | 
						|
// Checkpoint the container
 | 
						|
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 *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 *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 *exampleTaskService) Stats(ctx context.Context, r *taskAPI.StatsRequest) (*taskAPI.StatsResponse, error) {
 | 
						|
	return nil, errdefs.ErrNotImplemented
 | 
						|
}
 | 
						|
 | 
						|
// Update the live container
 | 
						|
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 *exampleTaskService) Wait(ctx context.Context, r *taskAPI.WaitRequest) (*taskAPI.WaitResponse, error) {
 | 
						|
	return nil, errdefs.ErrNotImplemented
 | 
						|
}
 |