-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
105 lines (90 loc) · 2.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// When compiled for an Alpine container use
// CGO_ENABLED=0 go build
package main
import (
"log"
"syscall"
"strings"
"flag"
"github.com/fsnotify/fsnotify"
api "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
)
type vfioInstance struct {
devicePlugin *vfioDevicePlugin
resourceName string
iommuGroups []string
socketName string
}
func main() {
var instances []vfioInstance
var configFile string
flag.StringVar(&configFile, "config", "/root/config/config.yml", "path to the configuration file")
flag.Parse()
log.Print("Starting VFIO device plugin for Kubernetes")
config := readConfigFile(configFile)
devices := scanDevices()
groups := groupDevices(devices, config)
for _,group := range groups {
var instance vfioInstance
instance.devicePlugin = nil
instance.iommuGroups = group.iommuGroups
instance.resourceName = group.resourceName
instance.socketName = api.DevicePluginPath + strings.ReplaceAll(group.resourceName, "/", "-") + ".sock"
instances = append(instances, instance)
}
log.Print("Starting new FS watcher")
watcher, err := newFSWatcher(api.DevicePluginPath)
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
log.Print("Starting new OS watcher")
sigs := newOSWatcher(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
restart := true
L:
for {
if restart {
var err error
for _,instance := range instances {
if instance.devicePlugin != nil {
instance.devicePlugin.Stop()
}
}
for _,instance := range instances {
instance.devicePlugin = NewDevicePlugin(instance.iommuGroups, instance.resourceName, instance.socketName)
err = instance.devicePlugin.Serve()
if err != nil {
log.Print("Failed to contact Kubelet, retrying")
break
}
}
if err != nil {
continue
}
restart = false
}
select {
case event := <-watcher.Events:
if (event.Name == api.KubeletSocket) && (event.Op & fsnotify.Create) == fsnotify.Create {
log.Printf("inotify: %s created, restarting", api.KubeletSocket)
restart = true
}
case err := <-watcher.Errors:
log.Printf("inotify: %s", err)
case s := <-sigs:
switch s {
case syscall.SIGHUP:
log.Print("Received SIGHUP, restarting.")
restart = true
default:
log.Printf("Received signal '%v', shutting down", s)
for _,instance := range instances {
if instance.devicePlugin != nil {
instance.devicePlugin.Stop()
}
}
break L
}
}
}
}