-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
86 lines (69 loc) · 2.01 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
package main
import (
"flag"
"os"
"syscall"
log "github.com/Sirupsen/logrus"
"github.com/fsnotify/fsnotify"
"github.com/mpreu/k8s-device-plugin-v4l2loopback/v4l2l"
api "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
)
func main() {
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
flagLogLevel := flag.String("log-level", "info", "Define the logging level: info, debug.")
flag.Parse()
switch *flagLogLevel {
case "debug":
log.SetLevel(log.DebugLevel)
case "info":
log.SetLevel(log.InfoLevel)
}
log.Println("Starting k8s-device-plugin-v4l2loopback")
log.Println("Searching for devices ...")
devices := v4l2l.GetDeviceList()
if len(devices) == 0 {
log.Println("No devices found")
return
}
log.Debugf("Devices found: %v", devices)
log.Println("Starting OS signals watcher")
sig := newOSSignalWatcher(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
log.Println("Starting filesystem event watcher")
watcher, err := newFSWatcher(api.DevicePluginPath)
if err != nil {
log.Errorf("Could not create filesystem watcher: %v", err)
return
}
defer watcher.Close()
log.Println("Starting device plugin server")
devicePlugin := NewV4l2lDevicePlugin()
err = devicePlugin.Serve()
if err != nil {
log.Errorf("Plugin server error: %v", err)
return
}
for {
// Wait for channels
select {
// Termination signals
case s := <-sig:
log.Debugf("Termination signal received: %v", s)
devicePlugin.StopServer()
// Filesystem events
case event := <-watcher.Events:
if event.Name == api.KubeletSocket && event.Op&fsnotify.Create == fsnotify.Create {
log.Infof("fsnotify: %s created", api.KubeletSocket)
devicePlugin := NewV4l2lDevicePlugin()
err = devicePlugin.Serve()
if err != nil {
log.Errorf("Plugin server error: %v", err)
return
}
}
if event.Name == api.KubeletSocket && event.Op&fsnotify.Remove == fsnotify.Remove {
log.Infof("fsnotify: %s removed", pluginSocket)
devicePlugin.StopServer()
}
}
}
}