Skip to content

Commit e3187d6

Browse files
committed
adaptation, stub: implement plugin shutdown, delay close.
Implement plugin shutdown. On error paths where this seems the right choice, shut down plugins instead of closing them. This should give plugins a chance to gather more context (from the shutdown reason) than just closing the connection. On other error code paths where we close the connection, delay it to allow any errors to propagate back to the plugin before the connection is closed. Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
1 parent c3aa53a commit e3187d6

File tree

6 files changed

+197
-85
lines changed

6 files changed

+197
-85
lines changed

pkg/adaptation/adaptation.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"io/fs"
2323
"net"
2424
"os"
25+
"path"
2526
"path/filepath"
2627
"sort"
2728
"sync"
@@ -225,6 +226,18 @@ func (r *Adaptation) Stop() {
225226
r.stopPlugins()
226227
}
227228

229+
// Shutdown plugins matching the given pattern, passing them the given reason.
230+
func (r *Adaptation) ShutdownPlugins(reason, pattern string) {
231+
r.Lock()
232+
defer r.Unlock()
233+
234+
for _, p := range r.plugins {
235+
if match, _ := path.Match(pattern, p.name()); match {
236+
p.shutdown(reason)
237+
}
238+
}
239+
}
240+
228241
// RunPodSandbox relays the corresponding CRI event to plugins.
229242
func (r *Adaptation) RunPodSandbox(ctx context.Context, req *RunPodSandboxRequest) error {
230243
r.Lock()

pkg/adaptation/adaptation_suite_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3188,6 +3188,62 @@ var _ = Describe("Plugin configuration request", func() {
31883188
})
31893189
})
31903190

3191+
var _ = Describe("Plugin shutdown request", func() {
3192+
var (
3193+
s = &Suite{}
3194+
)
3195+
3196+
AfterEach(func() {
3197+
s.Cleanup()
3198+
})
3199+
3200+
BeforeEach(func() {
3201+
s.Prepare(&mockRuntime{}, &mockPlugin{idx: "00", name: "test"})
3202+
})
3203+
3204+
It("should be able to shut down plugins with a reason", func() {
3205+
var (
3206+
reason = "test-shutdown-reason"
3207+
timeout = 2 * time.Second
3208+
)
3209+
3210+
s.Startup()
3211+
3212+
s.ShutdownPlugin(s.plugins[0], reason)
3213+
e, err := s.plugins[0].EventQ().Wait(PluginShutdown, time.After(timeout))
3214+
Expect(err).To(BeNil())
3215+
Expect(e.Reason).To(Equal(reason))
3216+
})
3217+
3218+
It("plugin that times out should be shut down with a reason", func() {
3219+
var (
3220+
reason = nri.ShutdownRequestTimeout
3221+
timeout = 2 * time.Second
3222+
)
3223+
3224+
pod := &api.PodSandbox{
3225+
Id: "pod0",
3226+
Name: "pod0",
3227+
Uid: "uid0",
3228+
Namespace: "default",
3229+
}
3230+
podReq := &api.RunPodSandboxRequest{Pod: pod}
3231+
3232+
s.plugins[0].runPodSandbox = func(_ *mockPlugin, _ *api.PodSandbox) error {
3233+
time.Sleep(3 * time.Second)
3234+
return nil
3235+
}
3236+
3237+
s.Startup()
3238+
Expect(s.runtime.RunPodSandbox(context.Background(), podReq)).To(Succeed())
3239+
3240+
e, err := s.plugins[0].EventQ().Wait(PluginShutdown, time.After(timeout))
3241+
Expect(err).To(BeNil())
3242+
Expect(e.Reason).To(Equal(reason))
3243+
})
3244+
3245+
})
3246+
31913247
func protoDiff(a, b proto.Message) string {
31923248
return cmp.Diff(a, b, protocmp.Transform())
31933249
}

0 commit comments

Comments
 (0)