Files
Memoh/internal/containerd/service.go
T
Menci d5b410d7e3 refactor(workspace): new workspace v3 container architecture (#244)
* feat(mcp): workspace container with bridge architecture

Migrate MCP containers to use UDS-based bridge communication instead of
TCP gRPC. Containers now mount runtime binaries and Unix domain sockets
from the host, eliminating the need for a dedicated MCP Docker image.

- Remove Dockerfile.mcp and entrypoint.sh in favor of standard base images
- Add toolkit Dockerfile for building MCP binary separately
- Containers use bind mounts for /opt/memoh (runtime) and /run/memoh (UDS)
- Update all config files with new runtime_path and socket_dir settings
- Support custom base images per bot (debian, alpine, ubuntu, etc.)
- Legacy container detection and TCP fallback for pre-bridge containers
- Frontend: add base image selector in container creation UI

* feat(container): SSE progress bar for container creation

Add real-time progress feedback during container image pull and creation
using Server-Sent Events, without breaking the existing synchronous JSON
API (content negotiation via Accept header).

Backend:
- Add PullProgress/LayerStatus types and OnProgress callback to
  PullImageOptions (containerd service layer)
- DefaultService.PullImage polls ContentStore.ListStatuses every 500ms
  when OnProgress is set; AppleService ignores it
- CreateContainer handler checks Accept: text/event-stream and switches
  to SSE branch: pulling → pull_progress → creating → complete/error

Frontend:
- handleCreateContainer/handleRecreateContainer use fetch + SSE instead
  of the SDK's synchronous postBotsByBotIdContainer
- Progress bar shows layer-level pull progress (offset/total) during
  pulling phase and indeterminate animation during creating phase
- i18n keys added for pullingImage and creatingContainer (en/zh)

* fix(container): clear stale legacy route and type create SSE

* fix(ci): resolve lint errors and arm64 musl node.js download

- Fix unused-receiver lint: rename `s` to `_` on stub methods in
  manager_legacy_test.go
- Fix sloglint: use slog.DiscardHandler instead of
  slog.NewTextHandler(io.Discard, nil)
- Handle missing arm64 musl Node.js builds: unofficial-builds.nodejs.org
  does not provide arm64 musl binaries, fall back to glibc build

* fix(lint): address errcheck, staticcheck, and gosec findings

- Discard os.Setenv/os.Remove return values explicitly with _
- Use omitted receiver name instead of _ (staticcheck ST1006)
- Tighten directory permissions from 0o755 to 0o750 (gosec G301)

* fix(lint): sanitize socket path to satisfy gosec G703

filepath.Clean the env-sourced socket path before os.Remove
to avoid path-traversal taint warning.

* fix(lint): use nolint directive for gosec G703 on socket path

filepath.Clean does not satisfy gosec's taint analysis. The socket
path comes from MCP_SOCKET_PATH env (operator-configured) or a
compiled-in default, not from end-user input.

* refactor: rename MCP container/bridge to workspace/bridge

Split internal/mcp/ to separate container lifecycle management from
Model Context Protocol connections, eliminating naming confusion:

- internal/mcp/ (container mgmt) → internal/workspace/
- internal/mcp/mcpclient/ → internal/workspace/bridge/
- internal/mcp/mcpcontainer/ → internal/workspace/bridgepb/
- cmd/mcp/ → cmd/bridge/
- config: MCPConfig → WorkspaceConfig, [mcp] → [workspace]
- container prefix: mcp-{id} → workspace-{id}
- labels: mcp.bot_id → memoh.bot_id, add memoh.workspace=v1
- socket: mcp.sock → bridge.sock, env BRIDGE_SOCKET_PATH
- runtime: /opt/memoh/runtime/mcp → /opt/memoh/runtime/bridge
- devenv: mcp-build.sh → bridge-build.sh

Legacy containers (mcp- prefix) detected by container name prefix
and handled via existing fallback path.

* fix(container): use memoh.workspace=v3 label value

* refactor(container): drop LegacyBotLabelKey, infer bot ID from container name

Legacy containers use mcp-{botID} naming, so bot ID can be derived
via TrimPrefix instead of looking up the mcp.bot_id label.

* fix(workspace): resolve containers via manager and drop gateway container ID

* docs: fix stale mcp references in AGENTS.md and DEPLOYMENT.md

* refactor(workspace): move container lifecycle ownership into manager

* dev: isolate local devenv from prod config

* toolkit: support musl node runtime

* containerd: fix fallback resolv.conf permissions

* web: preserve container create progress on completion

* web: add bot creation wait hint

* fix(workspace): preserve image selection across recreate

* feat(web): shorten default docker hub image refs

* fix(container): address code review findings

- Remove synchronous CreateContainer path (SSE-only now)
- Move flusher check before WriteHeader to avoid committed 200 on error
- Fix legacy container IP not cached via ensureContainerAndTask path
- Add atomic guard to prevent stale pull_progress after PullImage returns
- Defensive copy for tzEnv slice to avoid mutating shared backing array
- Restore network failure severity in restartContainer (return + Error)
- Extract duplicate progress bar into ContainerCreateProgress component
- Fix codesync comments to use repo-relative paths
- Add SaaS image validation note and kernel version comment on reaper

* refactor(devenv): extract toolkit install into shared script

Unify the Node.js + uv download logic into docker/toolkit/install.sh,
used by the production Dockerfile and runnable locally for dev.

Dev environment no longer bakes toolkit into the Docker image — it is
volume-mounted from .toolkit/ instead, so wrapper script changes take
effect immediately without rebuilding. The entrypoint checks for the
toolkit directory and prints a clear error if missing.

* fix(ci): address go ci failures

* chore(docker): remove unused containerd image

* refactor(config): rename workspace image key

* fix(workspace): fix legacy container data loss on migration and stop swallowing errors

Three root causes were identified and fixed:

1. Delete() used hardcoded "workspace-" prefix to look up legacy "mcp-"
   containers, causing GetContainer to return NotFound. CleanupBotContainer
   then silently skipped the error and deleted the DB record without ever
   calling PreserveData. Fix: resolve the actual container ID via
   ContainerID() (DB → label → scan) before operating.

2. Multiple restore error paths were silently swallowed (logged as Warn
   but not returned), so the user saw HTTP 200/204 with no data and no
   error. Fix: all errors in the preserve/restore chain now block the
   workflow and propagate to the caller.

3. tarGzDir used cached DirEntry.Info() for tar header size, which on
   overlayfs can differ from the actual file size, causing "archive/tar:
   write too long". Fix: open the file first, Fstat the fd for a
   race-free size, and use LimitReader as a safeguard.

Also adds a "restoring" SSE phase so the frontend shows a progress
indicator ("Restoring data, this may take a while...") during data
migration on container recreation.

* refactor(workspace): single-point container ID resolution

Replace the `containerID func(string) string` field with a single
`resolveContainerID(ctx, botID)` method that resolves the actual
container ID via DB → label → scan → fallback. All ~16 lookup
callsites across manager.go, dataio.go, versioning.go, and
manager_lifecycle.go now go through this single resolver, which
correctly handles both legacy "mcp-" and new "workspace-" containers.

Only `ensureBotWithImage` inlines `ContainerPrefix + botID` for
creating brand-new containers — every other path resolves dynamically.

* fix(web): show progress during data backup phase of container recreate

The recreate flow (delete with preserve_data + create with restore_data)
blocked on the DELETE call while backing up /data with no progress
indication. Add a 'preserving' phase to the progress component so
users see "正在备份数据..." instead of an unexplained hang.

* chore: remove [MYDEBUG] debug logging

Clean up all 112 temporary debug log statements added during the
legacy container migration investigation. Kept only meaningful
warn-level logs for non-fatal errors (network teardown, rename
failures).
2026-03-18 15:19:09 +08:00

841 lines
22 KiB
Go

package containerd
import (
"context"
"errors"
"fmt"
"log/slog"
"runtime"
"strings"
"syscall"
"time"
tasksv1 "github.com/containerd/containerd/api/services/tasks/v1"
tasktypes "github.com/containerd/containerd/api/types/task"
containerd "github.com/containerd/containerd/v2/client"
"github.com/containerd/containerd/v2/core/images"
"github.com/containerd/containerd/v2/core/remotes/docker"
"github.com/containerd/containerd/v2/core/snapshots"
"github.com/containerd/containerd/v2/pkg/cio"
"github.com/containerd/containerd/v2/pkg/namespaces"
"github.com/containerd/containerd/v2/pkg/oci"
"github.com/containerd/errdefs"
"github.com/opencontainers/image-spec/identity"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/memohai/memoh/internal/config"
)
var (
ErrInvalidArgument = errors.New("invalid argument")
ErrTaskStopTimeout = errors.New("timeout waiting for task to stop")
)
type PullImageOptions struct {
Unpack bool
Snapshotter string
OnProgress func(PullProgress) // optional, nil = no progress reporting
}
type DeleteImageOptions struct {
Synchronous bool
}
type CreateContainerRequest struct {
ID string
ImageRef string
SnapshotID string
Snapshotter string
Labels map[string]string
Spec ContainerSpec
}
type DeleteContainerOptions struct {
CleanupSnapshot bool
}
type StartTaskOptions struct {
Terminal bool
}
type StopTaskOptions struct {
Signal syscall.Signal
Timeout time.Duration
Force bool
}
type DeleteTaskOptions struct {
Force bool
}
type SnapshotCommitResult struct {
VersionSnapshotName string
ActiveSnapshotName string
}
type ListTasksOptions struct {
Filter string
}
type Service interface {
PullImage(ctx context.Context, ref string, opts *PullImageOptions) (ImageInfo, error)
GetImage(ctx context.Context, ref string) (ImageInfo, error)
ListImages(ctx context.Context) ([]ImageInfo, error)
DeleteImage(ctx context.Context, ref string, opts *DeleteImageOptions) error
// ResolveRemoteDigest fetches only the manifest digest from the registry
// without downloading any layers. Returns ErrNotSupported on backends that
// have no concept of a remote registry (e.g. Apple Virtualization).
ResolveRemoteDigest(ctx context.Context, ref string) (string, error)
CreateContainer(ctx context.Context, req CreateContainerRequest) (ContainerInfo, error)
GetContainer(ctx context.Context, id string) (ContainerInfo, error)
ListContainers(ctx context.Context) ([]ContainerInfo, error)
DeleteContainer(ctx context.Context, id string, opts *DeleteContainerOptions) error
ListContainersByLabel(ctx context.Context, key, value string) ([]ContainerInfo, error)
StartContainer(ctx context.Context, containerID string, opts *StartTaskOptions) error
StopContainer(ctx context.Context, containerID string, opts *StopTaskOptions) error
DeleteTask(ctx context.Context, containerID string, opts *DeleteTaskOptions) error
GetTaskInfo(ctx context.Context, containerID string) (TaskInfo, error)
ListTasks(ctx context.Context, opts *ListTasksOptions) ([]TaskInfo, error)
SetupNetwork(ctx context.Context, req NetworkSetupRequest) (NetworkResult, error)
RemoveNetwork(ctx context.Context, req NetworkSetupRequest) error
CommitSnapshot(ctx context.Context, snapshotter, name, key string) error
ListSnapshots(ctx context.Context, snapshotter string) ([]SnapshotInfo, error)
PrepareSnapshot(ctx context.Context, snapshotter, key, parent string) error
CreateContainerFromSnapshot(ctx context.Context, req CreateContainerRequest) (ContainerInfo, error)
SnapshotMounts(ctx context.Context, snapshotter, key string) ([]MountInfo, error)
}
type DefaultService struct {
client *containerd.Client
namespace string
logger *slog.Logger
}
func NewDefaultService(log *slog.Logger, client *containerd.Client, cfg config.Config) *DefaultService {
namespace := cfg.Containerd.Namespace
if namespace == "" {
namespace = DefaultNamespace
}
return &DefaultService{
client: client,
namespace: namespace,
logger: log.With(slog.String("service", "containerd")),
}
}
func (s *DefaultService) PullImage(ctx context.Context, ref string, opts *PullImageOptions) (ImageInfo, error) {
if ref == "" {
return ImageInfo{}, ErrInvalidArgument
}
ref = config.NormalizeImageRef(ref)
ctx = s.withNamespace(ctx)
pullOpts := []containerd.RemoteOpt{}
if opts == nil || opts.Unpack {
pullOpts = append(pullOpts, containerd.WithPullUnpack)
}
if opts != nil && opts.Snapshotter != "" {
pullOpts = append(pullOpts, containerd.WithPullSnapshotter(opts.Snapshotter))
}
// When OnProgress is set, poll content store for active download statuses.
if opts != nil && opts.OnProgress != nil {
stop := make(chan struct{})
go func() {
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
cs := s.client.ContentStore()
for {
select {
case <-stop:
return
case <-ctx.Done():
return
case <-ticker.C:
statuses, err := cs.ListStatuses(ctx)
if err != nil {
continue
}
layers := make([]LayerStatus, len(statuses))
for i, st := range statuses {
layers[i] = LayerStatus{
Ref: st.Ref,
Offset: st.Offset,
Total: st.Total,
}
}
opts.OnProgress(PullProgress{Layers: layers})
}
}
}()
defer close(stop)
}
img, err := s.client.Pull(ctx, ref, pullOpts...)
if err != nil {
return ImageInfo{}, err
}
return toImageInfo(img), nil
}
func (s *DefaultService) GetImage(ctx context.Context, ref string) (ImageInfo, error) {
if ref == "" {
return ImageInfo{}, ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
img, err := s.getImageWithFallback(ctx, ref)
if err != nil {
return ImageInfo{}, err
}
return toImageInfo(img), nil
}
func (s *DefaultService) ListImages(ctx context.Context) ([]ImageInfo, error) {
ctx = s.withNamespace(ctx)
imgs, err := s.client.ListImages(ctx)
if err != nil {
return nil, err
}
result := make([]ImageInfo, len(imgs))
for i, img := range imgs {
result[i] = toImageInfo(img)
}
return result, nil
}
func (s *DefaultService) DeleteImage(ctx context.Context, ref string, opts *DeleteImageOptions) error {
if ref == "" {
return ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
deleteOpts := []images.DeleteOpt{}
if opts != nil && opts.Synchronous {
deleteOpts = append(deleteOpts, images.SynchronousDelete())
}
return s.client.ImageService().Delete(ctx, ref, deleteOpts...)
}
func specOptsFromSpec(spec ContainerSpec) []oci.SpecOpts {
var opts []oci.SpecOpts
if len(spec.Cmd) > 0 {
opts = append(opts, oci.WithProcessArgs(spec.Cmd...))
}
if len(spec.Env) > 0 {
opts = append(opts, oci.WithEnv(spec.Env))
}
if spec.WorkDir != "" {
opts = append(opts, oci.WithProcessCwd(spec.WorkDir))
}
if spec.User != "" {
opts = append(opts, oci.WithUser(spec.User))
}
if spec.TTY {
opts = append(opts, oci.WithTTY)
}
if len(spec.Mounts) > 0 {
mounts := make([]specs.Mount, len(spec.Mounts))
for i, m := range spec.Mounts {
mounts[i] = specs.Mount{
Destination: m.Destination,
Type: m.Type,
Source: m.Source,
Options: m.Options,
}
}
opts = append(opts, oci.WithMounts(mounts))
}
return opts
}
func (s *DefaultService) CreateContainer(ctx context.Context, req CreateContainerRequest) (ContainerInfo, error) {
if req.ID == "" || req.ImageRef == "" {
return ContainerInfo{}, ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
ctx, done, err := s.client.WithLease(ctx)
if err != nil {
return ContainerInfo{}, err
}
defer func() { _ = done(ctx) }()
image, err := s.getImageWithFallback(ctx, req.ImageRef)
if err != nil {
pullOpts := &PullImageOptions{
Unpack: true,
Snapshotter: req.Snapshotter,
}
_, err = s.PullImage(ctx, req.ImageRef, pullOpts)
if err != nil {
return ContainerInfo{}, err
}
image, err = s.getImageWithFallback(ctx, req.ImageRef)
if err != nil {
return ContainerInfo{}, err
}
}
snapshotID := req.SnapshotID
if snapshotID == "" {
snapshotID = req.ID
}
specOpts := []oci.SpecOpts{
oci.WithDefaultSpecForPlatform("linux/" + runtime.GOARCH),
oci.WithImageConfig(image),
}
specOpts = append(specOpts, specOptsFromSpec(req.Spec)...)
containerOpts := []containerd.NewContainerOpts{
containerd.WithImage(image),
}
if req.Snapshotter != "" {
containerOpts = append(containerOpts, containerd.WithSnapshotter(req.Snapshotter))
}
if req.Snapshotter != "" {
parent, err := s.snapshotParentFromLayers(ctx, image)
if err != nil {
return ContainerInfo{}, err
}
ok, err := s.snapshotExists(ctx, req.Snapshotter, parent)
if err != nil {
return ContainerInfo{}, err
}
if !ok {
return ContainerInfo{}, fmt.Errorf("parent snapshot %s does not exist", parent)
}
if err := s.prepareSnapshot(ctx, req.Snapshotter, snapshotID, parent); err != nil {
return ContainerInfo{}, err
}
containerOpts = append(containerOpts, containerd.WithSnapshot(snapshotID))
} else {
containerOpts = append(containerOpts, containerd.WithNewSnapshot(snapshotID, image))
}
containerOpts = append(containerOpts, containerd.WithNewSpec(specOpts...))
runtimeName := "io.containerd.runc.v2"
containerOpts = append(containerOpts, containerd.WithRuntime(runtimeName, nil))
if len(req.Labels) > 0 {
containerOpts = append(containerOpts, containerd.WithContainerLabels(req.Labels))
}
ctrObj, err := s.client.NewContainer(ctx, req.ID, containerOpts...)
if err != nil {
return ContainerInfo{}, err
}
return toContainerInfo(ctx, ctrObj)
}
func (*DefaultService) snapshotParentFromLayers(ctx context.Context, image containerd.Image) (string, error) {
diffIDs, err := image.RootFS(ctx)
if err != nil {
return "", fmt.Errorf("read image rootfs: %w", err)
}
if len(diffIDs) == 0 {
return "", errors.New("image has no layers")
}
chainIDs := identity.ChainIDs(diffIDs)
return chainIDs[len(chainIDs)-1].String(), nil
}
func (s *DefaultService) snapshotExists(ctx context.Context, snapshotter, key string) (bool, error) {
if snapshotter == "" || key == "" {
return false, ErrInvalidArgument
}
_, err := s.client.SnapshotService(snapshotter).Stat(ctx, key)
if err == nil {
return true, nil
}
if errdefs.IsNotFound(err) {
return false, nil
}
return false, err
}
func (s *DefaultService) prepareSnapshot(ctx context.Context, snapshotter, key, parent string) error {
if snapshotter == "" || key == "" || parent == "" {
return ErrInvalidArgument
}
sn := s.client.SnapshotService(snapshotter)
if _, err := sn.Stat(ctx, key); err == nil {
if err := sn.Remove(ctx, key); err != nil {
return err
}
} else if !errdefs.IsNotFound(err) {
return err
}
_, err := sn.Prepare(ctx, key, parent)
return err
}
func (s *DefaultService) getImageWithFallback(ctx context.Context, ref string) (containerd.Image, error) {
image, err := s.client.GetImage(ctx, ref)
if err == nil {
return image, nil
}
// Official Docker Hub images (e.g. "nginx:latest") may be stored under
// either "docker.io/library/nginx:latest" or the short form. Try both.
if strings.HasPrefix(ref, "docker.io/library/") {
short := strings.TrimPrefix(ref, "docker.io/library/")
if img, altErr := s.client.GetImage(ctx, short); altErr == nil {
return img, nil
}
}
return nil, err
}
func (s *DefaultService) GetContainer(ctx context.Context, id string) (ContainerInfo, error) {
if id == "" {
return ContainerInfo{}, ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
ctrObj, err := s.client.LoadContainer(ctx, id)
if err != nil {
return ContainerInfo{}, err
}
return toContainerInfo(ctx, ctrObj)
}
func (s *DefaultService) ListContainers(ctx context.Context) ([]ContainerInfo, error) {
ctx = s.withNamespace(ctx)
ctrs, err := s.client.Containers(ctx)
if err != nil {
return nil, err
}
result := make([]ContainerInfo, 0, len(ctrs))
for _, c := range ctrs {
info, err := toContainerInfo(ctx, c)
if err != nil {
return nil, err
}
result = append(result, info)
}
return result, nil
}
func (s *DefaultService) DeleteContainer(ctx context.Context, id string, opts *DeleteContainerOptions) error {
if id == "" {
return ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
container, err := s.client.LoadContainer(ctx, id)
if err != nil {
return err
}
// A stopped task still holds an entry in containerd; container.Delete fails
// with FAILED_PRECONDITION if any task entry exists. Delete it first.
if task, err := container.Task(ctx, nil); err == nil {
if _, err := task.Delete(ctx, containerd.WithProcessKill); err != nil && !errdefs.IsNotFound(err) {
return err
}
} else if !errdefs.IsNotFound(err) {
return err
}
deleteOpts := []containerd.DeleteOpts{}
cleanupSnapshot := true
if opts != nil {
cleanupSnapshot = opts.CleanupSnapshot
}
if cleanupSnapshot {
deleteOpts = append(deleteOpts, containerd.WithSnapshotCleanup)
}
return container.Delete(ctx, deleteOpts...)
}
func (s *DefaultService) StartContainer(ctx context.Context, containerID string, _ *StartTaskOptions) error {
if containerID == "" {
return ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
container, err := s.client.LoadContainer(ctx, containerID)
if err != nil {
return err
}
task, err := container.NewTask(ctx, cio.NullIO)
if err != nil {
return err
}
return task.Start(ctx)
}
func (s *DefaultService) getTask(ctx context.Context, containerID string) (containerd.Task, error) {
if containerID == "" {
return nil, ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
container, err := s.client.LoadContainer(ctx, containerID)
if err != nil {
return nil, err
}
return container.Task(ctx, nil)
}
func (s *DefaultService) GetTaskInfo(ctx context.Context, containerID string) (TaskInfo, error) {
task, err := s.getTask(ctx, containerID)
if err != nil {
return TaskInfo{}, err
}
status, err := task.Status(ctx)
if err != nil {
return TaskInfo{}, err
}
return TaskInfo{
ContainerID: containerID,
ID: task.ID(),
PID: task.Pid(),
Status: convertTaskStatus(status.Status),
ExitCode: status.ExitStatus,
}, nil
}
func (s *DefaultService) ListTasks(ctx context.Context, opts *ListTasksOptions) ([]TaskInfo, error) {
ctx = s.withNamespace(ctx)
request := &tasksv1.ListTasksRequest{}
if opts != nil {
request.Filter = opts.Filter
}
response, err := s.client.TaskService().List(ctx, request)
if err != nil {
return nil, err
}
tasks := make([]TaskInfo, 0, len(response.Tasks))
for _, task := range response.Tasks {
tasks = append(tasks, TaskInfo{
ContainerID: task.ContainerID,
ID: task.ID,
PID: task.Pid,
Status: convertContainerdTaskStatus(task.Status),
ExitCode: task.ExitStatus,
})
}
return tasks, nil
}
func (s *DefaultService) StopContainer(ctx context.Context, containerID string, opts *StopTaskOptions) error {
if containerID == "" {
return ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
task, err := s.getTask(ctx, containerID)
if err != nil {
return err
}
signal := syscall.SIGTERM
timeout := 10 * time.Second
force := false
if opts != nil {
if opts.Signal != 0 {
signal = opts.Signal
}
if opts.Timeout != 0 {
timeout = opts.Timeout
}
force = opts.Force
}
if err := task.Kill(ctx, signal); err != nil {
return err
}
statusC, err := task.Wait(ctx)
if err != nil {
return err
}
timer := time.NewTimer(timeout)
defer timer.Stop()
select {
case <-statusC:
return nil
case <-timer.C:
if force {
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
return fmt.Errorf("force kill failed: %w", err)
}
<-statusC
return nil
}
return ErrTaskStopTimeout
}
}
func (s *DefaultService) DeleteTask(ctx context.Context, containerID string, opts *DeleteTaskOptions) error {
if containerID == "" {
return ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
task, err := s.getTask(ctx, containerID)
if err != nil {
return err
}
if opts != nil && opts.Force {
// Kill and wait for exit before deleting; containerd rejects Delete on a
// still-running process even when force is requested.
_ = task.Kill(ctx, syscall.SIGKILL)
if statusC, waitErr := task.Wait(ctx); waitErr == nil {
waitCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
select {
case <-statusC:
case <-waitCtx.Done():
}
}
}
_, err = task.Delete(ctx)
return err
}
func (s *DefaultService) ListContainersByLabel(ctx context.Context, key, value string) ([]ContainerInfo, error) {
if key == "" {
return nil, ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
containers, err := s.client.Containers(ctx)
if err != nil {
return nil, err
}
filtered := make([]ContainerInfo, 0, len(containers))
for _, container := range containers {
ci, err := toContainerInfo(ctx, container)
if err != nil {
return nil, err
}
if labelValue, ok := ci.Labels[key]; ok && (value == "" || value == labelValue) {
filtered = append(filtered, ci)
}
}
return filtered, nil
}
func (s *DefaultService) CommitSnapshot(ctx context.Context, snapshotter, name, key string) error {
if snapshotter == "" || name == "" || key == "" {
return ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
return s.client.SnapshotService(snapshotter).Commit(ctx, name, key)
}
func (s *DefaultService) ListSnapshots(ctx context.Context, snapshotter string) ([]SnapshotInfo, error) {
if snapshotter == "" {
return nil, ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
var infos []SnapshotInfo
if err := s.client.SnapshotService(snapshotter).Walk(ctx, func(_ context.Context, info snapshots.Info) error {
infos = append(infos, SnapshotInfo{
Name: info.Name,
Parent: info.Parent,
Kind: info.Kind.String(),
Created: info.Created,
Updated: info.Updated,
Labels: info.Labels,
})
return nil
}); err != nil {
return nil, err
}
return infos, nil
}
func (s *DefaultService) PrepareSnapshot(ctx context.Context, snapshotter, key, parent string) error {
if snapshotter == "" || key == "" || parent == "" {
return ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
_, err := s.client.SnapshotService(snapshotter).Prepare(ctx, key, parent)
return err
}
func (s *DefaultService) CreateContainerFromSnapshot(ctx context.Context, req CreateContainerRequest) (ContainerInfo, error) {
if req.ID == "" || req.SnapshotID == "" {
return ContainerInfo{}, ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
imageRef := req.ImageRef
if imageRef == "" {
return ContainerInfo{}, ErrInvalidArgument
}
image, err := s.getImageWithFallback(ctx, imageRef)
if err != nil {
_, pullErr := s.PullImage(ctx, imageRef, &PullImageOptions{
Unpack: true,
Snapshotter: req.Snapshotter,
})
if pullErr != nil {
return ContainerInfo{}, pullErr
}
image, err = s.getImageWithFallback(ctx, imageRef)
if err != nil {
return ContainerInfo{}, err
}
}
specOpts := []oci.SpecOpts{
oci.WithDefaultSpecForPlatform("linux/" + runtime.GOARCH),
oci.WithImageConfig(image),
}
specOpts = append(specOpts, specOptsFromSpec(req.Spec)...)
containerOpts := []containerd.NewContainerOpts{
containerd.WithImage(image),
}
if req.Snapshotter != "" {
containerOpts = append(containerOpts, containerd.WithSnapshotter(req.Snapshotter))
}
containerOpts = append(containerOpts,
containerd.WithSnapshot(req.SnapshotID),
containerd.WithNewSpec(specOpts...),
)
if len(req.Labels) > 0 {
containerOpts = append(containerOpts, containerd.WithContainerLabels(req.Labels))
}
runtimeName := "io.containerd.runc.v2"
containerOpts = append(containerOpts, containerd.WithRuntime(runtimeName, nil))
ctrObj, err := s.client.NewContainer(ctx, req.ID, containerOpts...)
if err != nil {
return ContainerInfo{}, err
}
return toContainerInfo(ctx, ctrObj)
}
func (s *DefaultService) SnapshotMounts(ctx context.Context, snapshotter, key string) ([]MountInfo, error) {
if snapshotter == "" || key == "" {
return nil, ErrInvalidArgument
}
ctx = s.withNamespace(ctx)
mounts, err := s.client.SnapshotService(snapshotter).Mounts(ctx, key)
if err != nil {
return nil, err
}
result := make([]MountInfo, len(mounts))
for i, m := range mounts {
result[i] = MountInfo{
Type: m.Type,
Source: m.Source,
Options: m.Options,
}
}
return result, nil
}
func (s *DefaultService) SetupNetwork(ctx context.Context, req NetworkSetupRequest) (NetworkResult, error) {
ctx = s.withNamespace(ctx)
task, err := s.getTask(ctx, req.ContainerID)
if err != nil {
return NetworkResult{}, err
}
ip, err := setupCNINetwork(ctx, task, req.ContainerID, req.CNIBinDir, req.CNIConfDir)
if err != nil {
return NetworkResult{}, err
}
return NetworkResult{IP: ip}, nil
}
func (s *DefaultService) RemoveNetwork(ctx context.Context, req NetworkSetupRequest) error {
ctx = s.withNamespace(ctx)
task, err := s.getTask(ctx, req.ContainerID)
if err != nil {
return err
}
return removeCNINetwork(ctx, task, req.ContainerID, req.CNIBinDir, req.CNIConfDir)
}
func (s *DefaultService) withNamespace(ctx context.Context) context.Context {
return namespaces.WithNamespace(ctx, s.namespace)
}
func (*DefaultService) ResolveRemoteDigest(ctx context.Context, ref string) (string, error) {
if ref == "" {
return "", ErrInvalidArgument
}
ref = config.NormalizeImageRef(ref)
resolver := docker.NewResolver(docker.ResolverOptions{
Hosts: docker.ConfigureDefaultRegistries(),
})
_, desc, err := resolver.Resolve(ctx, ref)
if err != nil {
return "", err
}
return desc.Digest.String(), nil
}
func toImageInfo(img containerd.Image) ImageInfo {
return ImageInfo{
Name: img.Name(),
ID: img.Target().Digest.String(),
Tags: []string{img.Name()},
}
}
func toContainerInfo(ctx context.Context, c containerd.Container) (ContainerInfo, error) {
info, err := c.Info(ctx)
if err != nil {
return ContainerInfo{}, err
}
return ContainerInfo{
ID: info.ID,
Image: info.Image,
Labels: info.Labels,
Snapshotter: info.Snapshotter,
SnapshotKey: info.SnapshotKey,
Runtime: RuntimeInfo{Name: info.Runtime.Name},
CreatedAt: info.CreatedAt,
UpdatedAt: info.UpdatedAt,
}, nil
}
func convertTaskStatus(s containerd.ProcessStatus) TaskStatus {
switch s {
case containerd.Running:
return TaskStatusRunning
case containerd.Created:
return TaskStatusCreated
case containerd.Stopped:
return TaskStatusStopped
case containerd.Paused, containerd.Pausing:
return TaskStatusPaused
default:
return TaskStatusUnknown
}
}
func convertContainerdTaskStatus(s tasktypes.Status) TaskStatus {
switch s {
case tasktypes.Status_RUNNING:
return TaskStatusRunning
case tasktypes.Status_CREATED:
return TaskStatusCreated
case tasktypes.Status_STOPPED:
return TaskStatusStopped
case tasktypes.Status_PAUSED, tasktypes.Status_PAUSING:
return TaskStatusPaused
default:
return TaskStatusUnknown
}
}