1- //go:build windows
1+ //go:build windows && lcow
22
33package service
44
55import (
66 "context"
7+ "fmt"
78 "sync"
89
910 "github.com/Microsoft/hcsshim/internal/builder/vm/lcow"
11+ "github.com/Microsoft/hcsshim/internal/controller/pod"
1012 "github.com/Microsoft/hcsshim/internal/controller/vm"
1113 "github.com/Microsoft/hcsshim/internal/log"
1214 "github.com/Microsoft/hcsshim/internal/shim"
1315 "github.com/Microsoft/hcsshim/internal/shimdiag"
1416
1517 sandboxsvc "github.com/containerd/containerd/api/runtime/sandbox/v1"
16- tasksvc "github.com/containerd/containerd/api/runtime/task/v3 "
18+ tasksvc "github.com/containerd/containerd/api/runtime/task/v2 "
1719 "github.com/containerd/containerd/v2/core/runtime"
1820 "github.com/containerd/containerd/v2/pkg/namespaces"
1921 "github.com/containerd/containerd/v2/pkg/shutdown"
22+ "github.com/containerd/errdefs"
2023 "github.com/containerd/ttrpc"
2124)
2225
@@ -29,7 +32,7 @@ const (
2932// All Service methods (sandbox, task, and shimdiag) operate on this shared struct.
3033type Service struct {
3134 // mu is used to synchronize access to shared state within the Service.
32- mu sync.Mutex
35+ mu sync.RWMutex
3336
3437 // publisher is used to publish events from the shim to containerd.
3538 publisher shim.Publisher
@@ -45,7 +48,15 @@ type Service struct {
4548 sandboxOptions * lcow.SandboxOptions
4649
4750 // vmController is responsible for managing the lifecycle of the underlying utility VM and its associated resources.
48- vmController vm.Controller
51+ vmController * vm.Controller
52+
53+ // podControllers maps podID -> PodController for each active pod.
54+ podControllers map [string ]* pod.Controller
55+
56+ // containerPodMapping maps containerID -> podID, allowing callers to look up
57+ // which pod a container belongs to and then retrieve the corresponding controller
58+ // from podControllers.
59+ containerPodMapping map [string ]string
4960
5061 // shutdown manages graceful shutdown operations and allows registration of cleanup callbacks.
5162 shutdown shutdown.Service
@@ -56,10 +67,12 @@ var _ shim.TTRPCService = (*Service)(nil)
5667// NewService creates a new instance of the Service with the shared state.
5768func NewService (ctx context.Context , eventsPublisher shim.Publisher , sd shutdown.Service ) * Service {
5869 svc := & Service {
59- publisher : eventsPublisher ,
60- events : make (chan interface {}, 128 ), // Buffered channel for events
61- vmController : vm .NewController (),
62- shutdown : sd ,
70+ publisher : eventsPublisher ,
71+ events : make (chan interface {}, 128 ), // Buffered channel for events
72+ vmController : vm .New (),
73+ podControllers : make (map [string ]* pod.Controller ),
74+ containerPodMapping : make (map [string ]string ),
75+ shutdown : sd ,
6376 }
6477
6578 go svc .forward (ctx , eventsPublisher )
@@ -83,23 +96,27 @@ func NewService(ctx context.Context, eventsPublisher shim.Publisher, sd shutdown
8396// RegisterTTRPC registers the Task, Sandbox, and ShimDiag TTRPC services on
8497// the provided server so that containerd can call into the shim over TTRPC.
8598func (s * Service ) RegisterTTRPC (server * ttrpc.Server ) error {
86- tasksvc .RegisterTTRPCTaskService (server , s )
99+ tasksvc .RegisterTaskService (server , s )
87100 sandboxsvc .RegisterTTRPCSandboxService (server , s )
88101 shimdiag .RegisterShimDiagService (server , s )
89102 return nil
90103}
91104
105+ // ensureVMRunning returns an error if the VM is not in the running state.
106+ func (s * Service ) ensureVMRunning () error {
107+ if state := s .vmController .State (); state != vm .StateRunning {
108+ return fmt .Errorf ("vm is not running (state: %s): %w" , state , errdefs .ErrFailedPrecondition )
109+ }
110+ return nil
111+ }
112+
92113// SandboxID returns the unique identifier for the sandbox managed by this Service.
93114func (s * Service ) SandboxID () string {
94115 return s .sandboxID
95116}
96117
97118// send enqueues an event onto the internal events channel so that it can be
98119// forwarded to containerd asynchronously by the forward goroutine.
99- //
100- // TODO: wire up send() for task events once task lifecycle methods are implemented.
101- //
102- //nolint:unused
103120func (s * Service ) send (evt interface {}) {
104121 s .events <- evt
105122}
0 commit comments