|
17 | 17 | package server |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "context" |
20 | 21 | "fmt" |
| 22 | + "time" |
21 | 23 |
|
22 | | - runtime "k8s.io/cri-api/pkg/apis/runtime/v1" |
| 24 | + "github.com/containerd/platforms" |
23 | 25 |
|
24 | 26 | "github.com/containerd/containerd/v2/client" |
25 | 27 | "github.com/containerd/containerd/v2/core/sandbox" |
26 | 28 | criconfig "github.com/containerd/containerd/v2/internal/cri/config" |
27 | 29 | ) |
28 | 30 |
|
29 | 31 | type criSandboxService struct { |
30 | | - cli *client.Client |
31 | | - config *criconfig.Config |
| 32 | + sandboxControllers map[string]sandbox.Controller |
| 33 | + config *criconfig.Config |
32 | 34 | } |
33 | 35 |
|
34 | | -func newCriSandboxService(config *criconfig.Config, c *client.Client) *criSandboxService { |
| 36 | +func newCriSandboxService(config *criconfig.Config, sandboxers map[string]sandbox.Controller) *criSandboxService { |
35 | 37 | return &criSandboxService{ |
36 | | - cli: c, |
37 | | - config: config, |
| 38 | + sandboxControllers: sandboxers, |
| 39 | + config: config, |
38 | 40 | } |
39 | 41 | } |
40 | 42 |
|
41 | | -func (c *criSandboxService) SandboxController(config *runtime.PodSandboxConfig, runtimeHandler string) (sandbox.Controller, error) { |
42 | | - ociRuntime, err := c.config.GetSandboxRuntime(config, runtimeHandler) |
| 43 | +func (c *criSandboxService) SandboxController(sandboxer string) (sandbox.Controller, error) { |
| 44 | + sbController, ok := c.sandboxControllers[sandboxer] |
| 45 | + if !ok { |
| 46 | + return nil, fmt.Errorf("failed to get sandbox controller by %s", sandboxer) |
| 47 | + } |
| 48 | + return sbController, nil |
| 49 | +} |
| 50 | + |
| 51 | +func (c *criSandboxService) CreateSandbox(ctx context.Context, info sandbox.Sandbox, opts ...sandbox.CreateOpt) error { |
| 52 | + ctrl, err := c.SandboxController(info.Sandboxer) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + return ctrl.Create(ctx, info, opts...) |
| 57 | +} |
| 58 | + |
| 59 | +func (c *criSandboxService) StartSandbox(ctx context.Context, sandboxer string, sandboxID string) (sandbox.ControllerInstance, error) { |
| 60 | + ctrl, err := c.SandboxController(sandboxer) |
| 61 | + if err != nil { |
| 62 | + return sandbox.ControllerInstance{}, err |
| 63 | + } |
| 64 | + return ctrl.Start(ctx, sandboxID) |
| 65 | +} |
| 66 | + |
| 67 | +func (c *criSandboxService) WaitSandbox(ctx context.Context, sandboxer string, sandboxID string) (<-chan client.ExitStatus, error) { |
| 68 | + ctrl, err := c.SandboxController(sandboxer) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + |
| 73 | + ch := make(chan client.ExitStatus, 1) |
| 74 | + go func() { |
| 75 | + defer close(ch) |
| 76 | + |
| 77 | + exitStatus, err := ctrl.Wait(ctx, sandboxID) |
| 78 | + if err != nil { |
| 79 | + ch <- *client.NewExitStatus(client.UnknownExitStatus, time.Time{}, err) |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + ch <- *client.NewExitStatus(exitStatus.ExitStatus, exitStatus.ExitedAt, nil) |
| 84 | + }() |
| 85 | + |
| 86 | + return ch, nil |
| 87 | +} |
| 88 | + |
| 89 | +func (c *criSandboxService) SandboxStatus(ctx context.Context, sandboxer string, sandboxID string, verbose bool) (sandbox.ControllerStatus, error) { |
| 90 | + ctrl, err := c.SandboxController(sandboxer) |
| 91 | + if err != nil { |
| 92 | + return sandbox.ControllerStatus{}, err |
| 93 | + } |
| 94 | + return ctrl.Status(ctx, sandboxID, verbose) |
| 95 | +} |
| 96 | + |
| 97 | +func (c *criSandboxService) SandboxPlatform(ctx context.Context, sandboxer string, sandboxID string) (platforms.Platform, error) { |
| 98 | + ctrl, err := c.SandboxController(sandboxer) |
| 99 | + if err != nil { |
| 100 | + return platforms.Platform{}, err |
| 101 | + } |
| 102 | + return ctrl.Platform(ctx, sandboxID) |
| 103 | +} |
| 104 | + |
| 105 | +func (c *criSandboxService) ShutdownSandbox(ctx context.Context, sandboxer string, sandboxID string) error { |
| 106 | + ctrl, err := c.SandboxController(sandboxer) |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + return ctrl.Shutdown(ctx, sandboxID) |
| 111 | +} |
| 112 | + |
| 113 | +func (c *criSandboxService) StopSandbox(ctx context.Context, sandboxer, sandboxID string, opts ...sandbox.StopOpt) error { |
| 114 | + ctrl, err := c.SandboxController(sandboxer) |
43 | 115 | if err != nil { |
44 | | - return nil, fmt.Errorf("failed to get sandbox runtime: %w", err) |
| 116 | + return err |
45 | 117 | } |
46 | | - return c.cli.SandboxController(ociRuntime.Sandboxer), nil |
| 118 | + return ctrl.Stop(ctx, sandboxID, opts...) |
47 | 119 | } |
0 commit comments