Skip to content

Commit 8175a7b

Browse files
committed
Add signal handler to handle termination more gracefully
Change-Id: I537f81ab494cef7fe8ecc0b38ae091b8aaf60ac1
1 parent 8162c06 commit 8175a7b

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

internal/pkg/plugin/plugin.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ import (
2121
"bufio"
2222
"fmt"
2323
"os"
24+
"os/signal"
2425
"path/filepath"
2526
"regexp"
2627
"strconv"
28+
"syscall"
2729

2830
"github.com/ROCm/k8s-device-plugin/internal/pkg/amdgpu"
2931
"github.com/ROCm/k8s-device-plugin/internal/pkg/hwloc"
@@ -37,6 +39,7 @@ import (
3739
type AMDGPUPlugin struct {
3840
AMDGPUs map[string]map[string]int
3941
Heartbeat chan bool
42+
signal chan os.Signal
4043
}
4144

4245
// Start is an optional interface that could be implemented by plugin.
@@ -45,6 +48,9 @@ type AMDGPUPlugin struct {
4548
// method could be used to prepare resources before they are offered
4649
// to Kubernetes.
4750
func (p *AMDGPUPlugin) Start() error {
51+
p.signal = make(chan os.Signal, 1)
52+
signal.Notify(p.signal, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
53+
4854
return nil
4955
}
5056

@@ -172,6 +178,7 @@ func (p *AMDGPUPlugin) ListAndWatch(e *pluginapi.Empty, s pluginapi.DevicePlugin
172178

173179
s.Send(&pluginapi.ListAndWatchResponse{Devices: devs})
174180

181+
loop:
175182
for {
176183
select {
177184
case <-p.Heartbeat:
@@ -187,9 +194,14 @@ func (p *AMDGPUPlugin) ListAndWatch(e *pluginapi.Empty, s pluginapi.DevicePlugin
187194
devs[i].Health = health
188195
}
189196
s.Send(&pluginapi.ListAndWatchResponse{Devices: devs})
197+
case <-p.signal:
198+
glog.Infof("Received signal, exiting")
199+
break loop
190200
}
191201
}
192202
// returning a value with this function will unregister the plugin from k8s
203+
204+
return nil
193205
}
194206

195207
// GetPreferredAllocation returns a preferred set of devices to allocate
@@ -245,6 +257,7 @@ func (p *AMDGPUPlugin) Allocate(ctx context.Context, r *pluginapi.AllocateReques
245257
type AMDGPULister struct {
246258
ResUpdateChan chan dpm.PluginNameList
247259
Heartbeat chan bool
260+
Signal chan os.Signal
248261
}
249262

250263
// GetResourceNamespace must return namespace (vendor ID) of implemented Lister. e.g. for

0 commit comments

Comments
 (0)