@@ -19,14 +19,17 @@ package sandbox
1919import (
2020 "context"
2121
22+ "google.golang.org/grpc"
23+ "google.golang.org/protobuf/types/known/anypb"
24+
25+ eventtypes "github.com/containerd/containerd/api/events"
2226 api "github.com/containerd/containerd/api/services/sandbox/v1"
2327 "github.com/containerd/containerd/errdefs"
28+ "github.com/containerd/containerd/events"
2429 "github.com/containerd/containerd/log"
2530 "github.com/containerd/containerd/plugin"
2631 "github.com/containerd/containerd/protobuf"
2732 "github.com/containerd/containerd/sandbox"
28- "google.golang.org/grpc"
29- "google.golang.org/protobuf/types/known/anypb"
3033)
3134
3235func init () {
@@ -35,22 +38,30 @@ func init() {
3538 ID : "sandbox-controllers" ,
3639 Requires : []plugin.Type {
3740 plugin .SandboxControllerPlugin ,
41+ plugin .EventPlugin ,
3842 },
3943 InitFn : func (ic * plugin.InitContext ) (interface {}, error ) {
4044 sc , err := ic .GetByID (plugin .SandboxControllerPlugin , "local" )
4145 if err != nil {
4246 return nil , err
4347 }
4448
49+ ep , err := ic .Get (plugin .EventPlugin )
50+ if err != nil {
51+ return nil , err
52+ }
53+
4554 return & controllerService {
46- local : sc .(sandbox.Controller ),
55+ local : sc .(sandbox.Controller ),
56+ publisher : ep .(events.Publisher ),
4757 }, nil
4858 },
4959 })
5060}
5161
5262type controllerService struct {
53- local sandbox.Controller
63+ local sandbox.Controller
64+ publisher events.Publisher
5465 api.UnimplementedControllerServer
5566}
5667
@@ -68,6 +79,13 @@ func (s *controllerService) Create(ctx context.Context, req *api.ControllerCreat
6879 if err != nil {
6980 return & api.ControllerCreateResponse {}, errdefs .ToGRPC (err )
7081 }
82+
83+ if err := s .publisher .Publish (ctx , "sandboxes/create" , & eventtypes.SandboxCreate {
84+ SandboxID : req .GetSandboxID (),
85+ }); err != nil {
86+ return & api.ControllerCreateResponse {}, errdefs .ToGRPC (err )
87+ }
88+
7189 return & api.ControllerCreateResponse {
7290 SandboxID : req .GetSandboxID (),
7391 }, nil
@@ -79,6 +97,13 @@ func (s *controllerService) Start(ctx context.Context, req *api.ControllerStartR
7997 if err != nil {
8098 return & api.ControllerStartResponse {}, errdefs .ToGRPC (err )
8199 }
100+
101+ if err := s .publisher .Publish (ctx , "sandboxes/start" , & eventtypes.SandboxStart {
102+ SandboxID : req .GetSandboxID (),
103+ }); err != nil {
104+ return & api.ControllerStartResponse {}, errdefs .ToGRPC (err )
105+ }
106+
82107 return & api.ControllerStartResponse {
83108 SandboxID : inst .SandboxID ,
84109 Pid : inst .Pid ,
@@ -98,6 +123,15 @@ func (s *controllerService) Wait(ctx context.Context, req *api.ControllerWaitReq
98123 if err != nil {
99124 return & api.ControllerWaitResponse {}, errdefs .ToGRPC (err )
100125 }
126+
127+ if err := s .publisher .Publish (ctx , "sandboxes/exit" , & eventtypes.SandboxExit {
128+ SandboxID : req .GetSandboxID (),
129+ ExitStatus : exitStatus .ExitStatus ,
130+ ExitedAt : protobuf .ToTimestamp (exitStatus .ExitedAt ),
131+ }); err != nil {
132+ return & api.ControllerWaitResponse {}, errdefs .ToGRPC (err )
133+ }
134+
101135 return & api.ControllerWaitResponse {
102136 ExitStatus : exitStatus .ExitStatus ,
103137 ExitedAt : protobuf .ToTimestamp (exitStatus .ExitedAt ),
0 commit comments