Skip to content

Commit 9d86150

Browse files
committed
stub: pass context to plugin event handlers.
Don't hide context from plugins, pass the corresponding context to each plugin handler. Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
1 parent 4ba54cc commit 9d86150

File tree

1 file changed

+42
-42
lines changed

1 file changed

+42
-42
lines changed

pkg/stub/stub.go

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -47,52 +47,52 @@ type ConfigureInterface interface {
4747
// Configure the plugin with the given NRI-supplied configuration.
4848
// If a non-zero EventMask is returned, the plugin will be subscribed
4949
// to the corresponding.
50-
Configure(config, runtime, version string) (api.EventMask, error)
50+
Configure(ctx context.Context, config, runtime, version string) (api.EventMask, error)
5151
}
5252

5353
// SynchronizeInterface handles Synchronize API requests.
5454
type SynchronizeInterface interface {
5555
// Synchronize the state of the plugin with the runtime.
5656
// The plugin can request updates to containers in response.
57-
Synchronize([]*api.PodSandbox, []*api.Container) ([]*api.ContainerUpdate, error)
57+
Synchronize(context.Context, []*api.PodSandbox, []*api.Container) ([]*api.ContainerUpdate, error)
5858
}
5959

6060
// ShutdownInterface handles a Shutdown API request.
6161
type ShutdownInterface interface {
6262
// Shutdown notifies the plugin about the runtime shutting down.
63-
Shutdown(*api.ShutdownRequest)
63+
Shutdown(context.Context)
6464
}
6565

6666
// RunPodInterface handles RunPodSandbox API events.
6767
type RunPodInterface interface {
6868
// RunPodSandbox relays a RunPodSandbox event to the plugin.
69-
RunPodSandbox(*api.PodSandbox) error
69+
RunPodSandbox(context.Context, *api.PodSandbox) error
7070
}
7171

7272
// StopPodInterface handles StopPodSandbox API events.
7373
type StopPodInterface interface {
7474
// StopPodSandbox relays a StopPodSandbox event to the plugin.
75-
StopPodSandbox(*api.PodSandbox) error
75+
StopPodSandbox(context.Context, *api.PodSandbox) error
7676
}
7777

7878
// RemovePodInterface handles RemovePodSandbox API events.
7979
type RemovePodInterface interface {
8080
// RemovePodSandbox relays a RemovePodSandbox event to the plugin.
81-
RemovePodSandbox(*api.PodSandbox) error
81+
RemovePodSandbox(context.Context, *api.PodSandbox) error
8282
}
8383

8484
// CreateContainerInterface handles CreateContainer API requests.
8585
type CreateContainerInterface interface {
8686
// CreateContainer relays a CreateContainer request to the plugin.
8787
// The plugin can request adjustments to the container being created
8888
// and updates to other unstopped containers in response.
89-
CreateContainer(*api.PodSandbox, *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error)
89+
CreateContainer(context.Context, *api.PodSandbox, *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error)
9090
}
9191

9292
// StartContainerInterface handles StartContainer API requests.
9393
type StartContainerInterface interface {
9494
// StartContainer relays a StartContainer event to the plugin.
95-
StartContainer(*api.PodSandbox, *api.Container) error
95+
StartContainer(context.Context, *api.PodSandbox, *api.Container) error
9696
}
9797

9898
// UpdateContainerInterface handles UpdateContainer API requests.
@@ -101,38 +101,38 @@ type UpdateContainerInterface interface {
101101
// The plugin can request updates both to the container being updated
102102
// (which then supersedes the original update) and to other unstopped
103103
// containers in response.
104-
UpdateContainer(*api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
104+
UpdateContainer(context.Context, *api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
105105
}
106106

107107
// StopContainerInterface handles StopContainer API requests.
108108
type StopContainerInterface interface {
109109
// StopContainer relays a StopContainer request to the plugin.
110110
// The plugin can request updates to unstopped containers in response.
111-
StopContainer(*api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
111+
StopContainer(context.Context, *api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
112112
}
113113

114114
// RemoveContainerInterface handles RemoveContainer API events.
115115
type RemoveContainerInterface interface {
116116
// RemoveContainer relays a RemoveContainer event to the plugin.
117-
RemoveContainer(*api.PodSandbox, *api.Container) error
117+
RemoveContainer(context.Context, *api.PodSandbox, *api.Container) error
118118
}
119119

120120
// PostCreateContainerInterface handles PostCreateContainer API events.
121121
type PostCreateContainerInterface interface {
122122
// PostCreateContainer relays a PostCreateContainer event to the plugin.
123-
PostCreateContainer(*api.PodSandbox, *api.Container) error
123+
PostCreateContainer(context.Context, *api.PodSandbox, *api.Container) error
124124
}
125125

126126
// PostStartContainerInterface handles PostStartContainer API events.
127127
type PostStartContainerInterface interface {
128128
// PostStartContainer relays a PostStartContainer event to the plugin.
129-
PostStartContainer(*api.PodSandbox, *api.Container) error
129+
PostStartContainer(context.Context, *api.PodSandbox, *api.Container) error
130130
}
131131

132132
// PostUpdateContainerInterface handles PostUpdateContainer API events.
133133
type PostUpdateContainerInterface interface {
134134
// PostUpdateContainer relays a PostUpdateContainer event to the plugin.
135-
PostUpdateContainer(*api.PodSandbox, *api.Container) error
135+
PostUpdateContainer(context.Context, *api.PodSandbox, *api.Container) error
136136
}
137137

138138
// Stub is the interface the stub provides for the plugin implementation.
@@ -253,20 +253,20 @@ type stub struct {
253253

254254
// Handlers for NRI plugin event and request.
255255
type handlers struct {
256-
Configure func(string, string, string) (api.EventMask, error)
257-
Synchronize func([]*api.PodSandbox, []*api.Container) ([]*api.ContainerUpdate, error)
258-
Shutdown func(*api.ShutdownRequest)
259-
RunPodSandbox func(*api.PodSandbox) error
260-
StopPodSandbox func(*api.PodSandbox) error
261-
RemovePodSandbox func(*api.PodSandbox) error
262-
CreateContainer func(*api.PodSandbox, *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error)
263-
StartContainer func(*api.PodSandbox, *api.Container) error
264-
UpdateContainer func(*api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
265-
StopContainer func(*api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
266-
RemoveContainer func(*api.PodSandbox, *api.Container) error
267-
PostCreateContainer func(*api.PodSandbox, *api.Container) error
268-
PostStartContainer func(*api.PodSandbox, *api.Container) error
269-
PostUpdateContainer func(*api.PodSandbox, *api.Container) error
256+
Configure func(context.Context, string, string, string) (api.EventMask, error)
257+
Synchronize func(context.Context, []*api.PodSandbox, []*api.Container) ([]*api.ContainerUpdate, error)
258+
Shutdown func(context.Context)
259+
RunPodSandbox func(context.Context, *api.PodSandbox) error
260+
StopPodSandbox func(context.Context, *api.PodSandbox) error
261+
RemovePodSandbox func(context.Context, *api.PodSandbox) error
262+
CreateContainer func(context.Context, *api.PodSandbox, *api.Container) (*api.ContainerAdjustment, []*api.ContainerUpdate, error)
263+
StartContainer func(context.Context, *api.PodSandbox, *api.Container) error
264+
UpdateContainer func(context.Context, *api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
265+
StopContainer func(context.Context, *api.PodSandbox, *api.Container) ([]*api.ContainerUpdate, error)
266+
RemoveContainer func(context.Context, *api.PodSandbox, *api.Container) error
267+
PostCreateContainer func(context.Context, *api.PodSandbox, *api.Container) error
268+
PostStartContainer func(context.Context, *api.PodSandbox, *api.Container) error
269+
PostUpdateContainer func(context.Context, *api.PodSandbox, *api.Container) error
270270
}
271271

272272
// New creates a stub with the given plugin and options.
@@ -552,7 +552,7 @@ func (stub *stub) Configure(ctx context.Context, req *api.ConfigureRequest) (rpl
552552
if handler := stub.handlers.Configure; handler == nil {
553553
events = stub.events
554554
} else {
555-
events, err = handler(req.Config, req.RuntimeName, req.RuntimeVersion)
555+
events, err = handler(ctx, req.Config, req.RuntimeName, req.RuntimeVersion)
556556
if err != nil {
557557
log.Errorf(ctx, "Plugin configuration failed: %v", err)
558558
return nil, err
@@ -585,7 +585,7 @@ func (stub *stub) Synchronize(ctx context.Context, req *api.SynchronizeRequest)
585585
if handler == nil {
586586
return &api.SynchronizeResponse{}, nil
587587
}
588-
update, err := handler(req.Pods, req.Containers)
588+
update, err := handler(ctx, req.Pods, req.Containers)
589589
return &api.SynchronizeResponse{
590590
Update: update,
591591
}, err
@@ -595,7 +595,7 @@ func (stub *stub) Synchronize(ctx context.Context, req *api.SynchronizeRequest)
595595
func (stub *stub) Shutdown(ctx context.Context, req *api.ShutdownRequest) (*api.ShutdownResponse, error) {
596596
handler := stub.handlers.Shutdown
597597
if handler != nil {
598-
handler(req)
598+
handler(ctx)
599599
}
600600
return &api.ShutdownResponse{}, nil
601601
}
@@ -606,7 +606,7 @@ func (stub *stub) CreateContainer(ctx context.Context, req *api.CreateContainerR
606606
if handler == nil {
607607
return nil, nil
608608
}
609-
adjust, update, err := handler(req.Pod, req.Container)
609+
adjust, update, err := handler(ctx, req.Pod, req.Container)
610610
return &api.CreateContainerResponse{
611611
Adjust: adjust,
612612
Update: update,
@@ -619,7 +619,7 @@ func (stub *stub) UpdateContainer(ctx context.Context, req *api.UpdateContainerR
619619
if handler == nil {
620620
return nil, nil
621621
}
622-
update, err := handler(req.Pod, req.Container)
622+
update, err := handler(ctx, req.Pod, req.Container)
623623
return &api.UpdateContainerResponse{
624624
Update: update,
625625
}, err
@@ -631,7 +631,7 @@ func (stub *stub) StopContainer(ctx context.Context, req *api.StopContainerReque
631631
if handler == nil {
632632
return nil, nil
633633
}
634-
update, err := handler(req.Pod, req.Container)
634+
update, err := handler(ctx, req.Pod, req.Container)
635635
return &api.StopContainerResponse{
636636
Update: update,
637637
}, err
@@ -643,35 +643,35 @@ func (stub *stub) StateChange(ctx context.Context, evt *api.StateChangeEvent) (*
643643
switch evt.Event {
644644
case api.Event_RUN_POD_SANDBOX:
645645
if handler := stub.handlers.RunPodSandbox; handler != nil {
646-
err = handler(evt.Pod)
646+
err = handler(ctx, evt.Pod)
647647
}
648648
case api.Event_STOP_POD_SANDBOX:
649649
if handler := stub.handlers.StopPodSandbox; handler != nil {
650-
err = handler(evt.Pod)
650+
err = handler(ctx, evt.Pod)
651651
}
652652
case api.Event_REMOVE_POD_SANDBOX:
653653
if handler := stub.handlers.RemovePodSandbox; handler != nil {
654-
err = handler(evt.Pod)
654+
err = handler(ctx, evt.Pod)
655655
}
656656
case api.Event_POST_CREATE_CONTAINER:
657657
if handler := stub.handlers.PostCreateContainer; handler != nil {
658-
err = handler(evt.Pod, evt.Container)
658+
err = handler(ctx, evt.Pod, evt.Container)
659659
}
660660
case api.Event_START_CONTAINER:
661661
if handler := stub.handlers.StartContainer; handler != nil {
662-
err = handler(evt.Pod, evt.Container)
662+
err = handler(ctx, evt.Pod, evt.Container)
663663
}
664664
case api.Event_POST_START_CONTAINER:
665665
if handler := stub.handlers.PostStartContainer; handler != nil {
666-
err = handler(evt.Pod, evt.Container)
666+
err = handler(ctx, evt.Pod, evt.Container)
667667
}
668668
case api.Event_POST_UPDATE_CONTAINER:
669669
if handler := stub.handlers.PostUpdateContainer; handler != nil {
670-
err = handler(evt.Pod, evt.Container)
670+
err = handler(ctx, evt.Pod, evt.Container)
671671
}
672672
case api.Event_REMOVE_CONTAINER:
673673
if handler := stub.handlers.RemoveContainer; handler != nil {
674-
err = handler(evt.Pod, evt.Container)
674+
err = handler(ctx, evt.Pod, evt.Container)
675675
}
676676
}
677677

0 commit comments

Comments
 (0)