Move local sandbox controller under plugins package
Add options to sandbox controller interface. Update sandbox controller interface to fully utilize sandbox controller interface. Move grpc error conversion to service. Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
@@ -18,13 +18,15 @@ package sandbox
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
api "github.com/containerd/containerd/api/services/sandbox/v1"
|
||||
"github.com/containerd/containerd/errdefs"
|
||||
"github.com/containerd/containerd/log"
|
||||
"github.com/containerd/containerd/plugin"
|
||||
"github.com/containerd/containerd/services"
|
||||
"github.com/containerd/containerd/protobuf"
|
||||
"github.com/containerd/containerd/sandbox"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -32,33 +34,23 @@ func init() {
|
||||
Type: plugin.GRPCPlugin,
|
||||
ID: "sandbox-controllers",
|
||||
Requires: []plugin.Type{
|
||||
plugin.ServicePlugin,
|
||||
plugin.SandboxControllerPlugin,
|
||||
},
|
||||
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
|
||||
plugins, err := ic.GetByType(plugin.ServicePlugin)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p, ok := plugins[services.SandboxControllerService]
|
||||
if !ok {
|
||||
return nil, errors.New("sandbox service not found")
|
||||
}
|
||||
|
||||
i, err := p.Instance()
|
||||
sc, err := ic.GetByID(plugin.SandboxControllerPlugin, "local")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &controllerService{
|
||||
local: i.(api.ControllerClient),
|
||||
local: sc.(sandbox.Controller),
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type controllerService struct {
|
||||
local api.ControllerClient
|
||||
local sandbox.Controller
|
||||
api.UnimplementedControllerServer
|
||||
}
|
||||
|
||||
@@ -71,30 +63,68 @@ func (s *controllerService) Register(server *grpc.Server) error {
|
||||
|
||||
func (s *controllerService) Create(ctx context.Context, req *api.ControllerCreateRequest) (*api.ControllerCreateResponse, error) {
|
||||
log.G(ctx).WithField("req", req).Debug("create sandbox")
|
||||
return s.local.Create(ctx, req)
|
||||
// TODO: Rootfs, any
|
||||
err := s.local.Create(ctx, req.GetSandboxID())
|
||||
if err != nil {
|
||||
return &api.ControllerCreateResponse{}, errdefs.ToGRPC(err)
|
||||
}
|
||||
return &api.ControllerCreateResponse{
|
||||
SandboxID: req.GetSandboxID(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *controllerService) Start(ctx context.Context, req *api.ControllerStartRequest) (*api.ControllerStartResponse, error) {
|
||||
log.G(ctx).WithField("req", req).Debug("start sandbox")
|
||||
return s.local.Start(ctx, req)
|
||||
inst, err := s.local.Start(ctx, req.GetSandboxID())
|
||||
if err != nil {
|
||||
return &api.ControllerStartResponse{}, errdefs.ToGRPC(err)
|
||||
}
|
||||
return &api.ControllerStartResponse{
|
||||
SandboxID: inst.SandboxID,
|
||||
Pid: inst.Pid,
|
||||
CreatedAt: protobuf.ToTimestamp(inst.CreatedAt),
|
||||
Labels: inst.Labels,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *controllerService) Stop(ctx context.Context, req *api.ControllerStopRequest) (*api.ControllerStopResponse, error) {
|
||||
log.G(ctx).WithField("req", req).Debug("delete sandbox")
|
||||
return s.local.Stop(ctx, req)
|
||||
return &api.ControllerStopResponse{}, errdefs.ToGRPC(s.local.Stop(ctx, req.GetSandboxID()))
|
||||
}
|
||||
|
||||
func (s *controllerService) Wait(ctx context.Context, req *api.ControllerWaitRequest) (*api.ControllerWaitResponse, error) {
|
||||
log.G(ctx).WithField("req", req).Debug("wait sandbox")
|
||||
return s.local.Wait(ctx, req)
|
||||
exitStatus, err := s.local.Wait(ctx, req.GetSandboxID())
|
||||
if err != nil {
|
||||
return &api.ControllerWaitResponse{}, errdefs.ToGRPC(err)
|
||||
}
|
||||
return &api.ControllerWaitResponse{
|
||||
ExitStatus: exitStatus.ExitStatus,
|
||||
ExitedAt: protobuf.ToTimestamp(exitStatus.ExitedAt),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *controllerService) Status(ctx context.Context, req *api.ControllerStatusRequest) (*api.ControllerStatusResponse, error) {
|
||||
log.G(ctx).WithField("req", req).Debug("sandbox status")
|
||||
return s.local.Status(ctx, req)
|
||||
cstatus, err := s.local.Status(ctx, req.GetSandboxID(), req.GetVerbose())
|
||||
if err != nil {
|
||||
return &api.ControllerStatusResponse{}, errdefs.ToGRPC(err)
|
||||
}
|
||||
return &api.ControllerStatusResponse{
|
||||
SandboxID: cstatus.SandboxID,
|
||||
Pid: cstatus.Pid,
|
||||
State: cstatus.State,
|
||||
Info: cstatus.Info,
|
||||
CreatedAt: protobuf.ToTimestamp(cstatus.CreatedAt),
|
||||
ExitedAt: protobuf.ToTimestamp(cstatus.ExitedAt),
|
||||
Extra: &anypb.Any{
|
||||
TypeUrl: cstatus.Extra.GetTypeUrl(),
|
||||
Value: cstatus.Extra.GetValue(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *controllerService) Shutdown(ctx context.Context, req *api.ControllerShutdownRequest) (*api.ControllerShutdownResponse, error) {
|
||||
log.G(ctx).WithField("req", req).Debug("shutdown sandbox")
|
||||
return s.local.Shutdown(ctx, req)
|
||||
return &api.ControllerShutdownResponse{}, errdefs.ToGRPC(s.local.Shutdown(ctx, req.GetSandboxID()))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user