Refactor sandbox controller interface

Update the sandbox controller interface to use local types rather than
using the API types.

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan
2023-01-18 21:53:34 -08:00
parent 20de989afc
commit 2717685dad
21 changed files with 378 additions and 355 deletions

View File

@@ -18,9 +18,10 @@ package sandbox
import (
"context"
"time"
"github.com/containerd/containerd/api/services/sandbox/v1"
"github.com/containerd/containerd/platforms"
"github.com/containerd/typeurl"
)
// Controller is an interface to manage sandboxes at runtime.
@@ -30,17 +31,39 @@ type Controller interface {
// Create is used to initialize sandbox environment.
Create(ctx context.Context, sandboxID string) error
// Start will start previously created sandbox.
Start(ctx context.Context, sandboxID string) (*sandbox.ControllerStartResponse, error)
Start(ctx context.Context, sandboxID string) (ControllerInstance, error)
// Platform returns target sandbox OS that will be used by Controller.
// containerd will rely on this to generate proper OCI spec.
Platform(_ctx context.Context, _sandboxID string) (platforms.Platform, error)
// Stop will stop sandbox instance
Stop(ctx context.Context, sandboxID string) (*sandbox.ControllerStopResponse, error)
Stop(ctx context.Context, sandboxID string) error
// Wait blocks until sandbox process exits.
Wait(ctx context.Context, sandboxID string) (*sandbox.ControllerWaitResponse, error)
Wait(ctx context.Context, sandboxID string) (ExitStatus, error)
// Status will query sandbox process status. It is heavier than Ping call and must be used whenever you need to
// gather metadata about current sandbox state (status, uptime, resource use, etc).
Status(ctx context.Context, sandboxID string, verbose bool) (*sandbox.ControllerStatusResponse, error)
Status(ctx context.Context, sandboxID string, verbose bool) (ControllerStatus, error)
// Shutdown deletes and cleans all tasks and sandbox instance.
Shutdown(ctx context.Context, sandboxID string) (*sandbox.ControllerShutdownResponse, error)
Shutdown(ctx context.Context, sandboxID string) error
}
type ControllerInstance struct {
SandboxID string
Pid uint32
CreatedAt time.Time
Labels map[string]string
}
type ExitStatus struct {
ExitStatus uint32
ExitedAt time.Time
}
type ControllerStatus struct {
SandboxID string
Pid uint32
State string
Info map[string]string
CreatedAt time.Time
ExitedAt time.Time
Extra typeurl.Any
}

View File

@@ -46,13 +46,18 @@ func (s *remoteSandboxController) Create(ctx context.Context, sandboxID string)
return nil
}
func (s *remoteSandboxController) Start(ctx context.Context, sandboxID string) (*api.ControllerStartResponse, error) {
func (s *remoteSandboxController) Start(ctx context.Context, sandboxID string) (sb.ControllerInstance, error) {
resp, err := s.client.Start(ctx, &api.ControllerStartRequest{SandboxID: sandboxID})
if err != nil {
return nil, errdefs.FromGRPC(err)
return sb.ControllerInstance{}, errdefs.FromGRPC(err)
}
return resp, nil
return sb.ControllerInstance{
SandboxID: sandboxID,
Pid: resp.GetPid(),
CreatedAt: resp.GetCreatedAt().AsTime(),
Labels: resp.GetLabels(),
}, nil
}
func (s *remoteSandboxController) Platform(ctx context.Context, sandboxID string) (platforms.Platform, error) {
@@ -69,38 +74,48 @@ func (s *remoteSandboxController) Platform(ctx context.Context, sandboxID string
}, nil
}
func (s *remoteSandboxController) Stop(ctx context.Context, sandboxID string) (*api.ControllerStopResponse, error) {
resp, err := s.client.Stop(ctx, &api.ControllerStopRequest{SandboxID: sandboxID})
func (s *remoteSandboxController) Stop(ctx context.Context, sandboxID string) error {
_, err := s.client.Stop(ctx, &api.ControllerStopRequest{SandboxID: sandboxID})
if err != nil {
return nil, errdefs.FromGRPC(err)
return errdefs.FromGRPC(err)
}
return resp, nil
return nil
}
func (s *remoteSandboxController) Shutdown(ctx context.Context, sandboxID string) (*api.ControllerShutdownResponse, error) {
resp, err := s.client.Shutdown(ctx, &api.ControllerShutdownRequest{SandboxID: sandboxID})
func (s *remoteSandboxController) Shutdown(ctx context.Context, sandboxID string) error {
_, err := s.client.Shutdown(ctx, &api.ControllerShutdownRequest{SandboxID: sandboxID})
if err != nil {
return nil, errdefs.FromGRPC(err)
return errdefs.FromGRPC(err)
}
return resp, nil
return nil
}
func (s *remoteSandboxController) Wait(ctx context.Context, sandboxID string) (*api.ControllerWaitResponse, error) {
func (s *remoteSandboxController) Wait(ctx context.Context, sandboxID string) (sb.ExitStatus, error) {
resp, err := s.client.Wait(ctx, &api.ControllerWaitRequest{SandboxID: sandboxID})
if err != nil {
return nil, errdefs.FromGRPC(err)
return sb.ExitStatus{}, errdefs.FromGRPC(err)
}
return resp, nil
return sb.ExitStatus{
ExitStatus: resp.GetExitStatus(),
ExitedAt: resp.GetExitedAt().AsTime(),
}, nil
}
func (s *remoteSandboxController) Status(ctx context.Context, sandboxID string, verbose bool) (*api.ControllerStatusResponse, error) {
func (s *remoteSandboxController) Status(ctx context.Context, sandboxID string, verbose bool) (sb.ControllerStatus, error) {
resp, err := s.client.Status(ctx, &api.ControllerStatusRequest{SandboxID: sandboxID, Verbose: verbose})
if err != nil {
return nil, errdefs.FromGRPC(err)
return sb.ControllerStatus{}, errdefs.FromGRPC(err)
}
return resp, nil
return sb.ControllerStatus{
SandboxID: sandboxID,
Pid: resp.GetPid(),
State: resp.GetState(),
Info: resp.GetInfo(),
CreatedAt: resp.GetCreatedAt().AsTime(),
ExitedAt: resp.GetExitedAt().AsTime(),
Extra: resp.GetExtra(),
}, nil
}