Skip to content

Commit 631c5f9

Browse files
call cancel on plugin that is replaced by another plugin with the same name
1 parent 6cc3570 commit 631c5f9

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

pkg/kubelet/cm/dra/plugin/plugins_store.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,22 @@ func (s *pluginsStore) get(pluginName string) *Plugin {
4242

4343
// Set lets you save a DRA Plugin to the list and give it a specific name.
4444
// This method is protected by a mutex.
45-
func (s *pluginsStore) add(p *Plugin) (replaced bool) {
45+
func (s *pluginsStore) add(p *Plugin) (replacedPlugin *Plugin, replaced bool) {
4646
s.Lock()
4747
defer s.Unlock()
4848

4949
if s.store == nil {
5050
s.store = make(map[string]*Plugin)
5151
}
5252

53-
_, exists := s.store[p.name]
53+
replacedPlugin, exists := s.store[p.name]
5454
s.store[p.name] = p
55-
return exists
55+
56+
if replacedPlugin != nil && replacedPlugin.cancel != nil {
57+
replacedPlugin.cancel(errors.New("plugin got replaced"))
58+
}
59+
60+
return replacedPlugin, exists
5661
}
5762

5863
// Delete lets you delete a DRA Plugin by name.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package plugin
18+
19+
import (
20+
"fmt"
21+
"math/rand/v2"
22+
"testing"
23+
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestAddSameName(t *testing.T) {
28+
// name will have a random value to avoid conflicts
29+
pluginName := fmt.Sprintf("dummy-plugin-%d", rand.IntN(10000))
30+
31+
firstWasCancelled := false
32+
p := &Plugin{
33+
name: pluginName,
34+
cancel: func(err error) { firstWasCancelled = true },
35+
}
36+
37+
// ensure the plugin we are using is registered
38+
draPlugins.add(p)
39+
defer draPlugins.delete(p.name)
40+
41+
assert.False(t, firstWasCancelled, "should not cancel context after the first call")
42+
43+
secondWasCancelled := false
44+
p2 := &Plugin{
45+
name: pluginName,
46+
cancel: func(err error) { secondWasCancelled = true },
47+
}
48+
49+
draPlugins.add(p2)
50+
defer draPlugins.delete(p2.name)
51+
52+
assert.True(t, firstWasCancelled, "should cancel context after the second call")
53+
assert.False(t, secondWasCancelled, "should not cancel context of a new plugin")
54+
}
55+
56+
func TestDelete(t *testing.T) {
57+
pluginName := fmt.Sprintf("dummy-plugin-%d", rand.IntN(10000))
58+
59+
wasCancelled := false
60+
p := &Plugin{
61+
name: pluginName,
62+
cancel: func(err error) { wasCancelled = true },
63+
}
64+
65+
// ensure the plugin we are using is registered
66+
draPlugins.add(p)
67+
68+
draPlugins.delete(p.name)
69+
70+
assert.True(t, wasCancelled, "should cancel context after the second call")
71+
}

pkg/kubelet/cm/dra/plugin/registration.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,9 @@ func (h *RegistrationHandler) RegisterPlugin(pluginName string, endpoint string,
179179

180180
// Storing endpoint of newly registered DRA Plugin into the map, where plugin name will be the key
181181
// all other DRA components will be able to get the actual socket of DRA plugins by its name.
182-
if draPlugins.add(pluginInstance) {
183-
logger.V(1).Info("Already registered, previous plugin was replaced")
182+
183+
if oldPlugin, replaced := draPlugins.add(pluginInstance); replaced {
184+
logger.V(1).Info("DRA plugin already registered, the old plugin was replaced and will be forgotten by the kubelet till the next kubelet restart", "oldEndpoint", oldPlugin.endpoint)
184185
}
185186

186187
return nil

0 commit comments

Comments
 (0)