80 lines
1.8 KiB
Go
80 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"syscall"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
|
|
)
|
|
|
|
func main() {
|
|
var (
|
|
mountsAllowed = 5
|
|
)
|
|
flag.IntVar(&mountsAllowed, "mounts_allowed", 5, "maximum times the fuse device can be mounted")
|
|
flag.Parse()
|
|
|
|
log.Println("Starting")
|
|
defer func() { log.Println("Stopped:") }()
|
|
|
|
log.Println("Starting FS watcher.")
|
|
watcher, err := newFSWatcher(pluginapi.DevicePluginPath)
|
|
if err != nil {
|
|
log.Println("Failed to created FS watcher.")
|
|
os.Exit(1)
|
|
}
|
|
defer watcher.Close()
|
|
|
|
log.Println("Starting OS watcher.")
|
|
sigs := newOSWatcher(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
|
|
|
restart := true
|
|
var devicePlugin *FuseDevicePlugin
|
|
|
|
L:
|
|
for {
|
|
if restart {
|
|
if devicePlugin != nil {
|
|
if err := devicePlugin.Stop(); err != nil {
|
|
log.Printf("Failed to stop plugin: %+v", err)
|
|
}
|
|
}
|
|
|
|
log.Printf("Max device count: %d", mountsAllowed)
|
|
devicePlugin = NewFuseDevicePlugin(mountsAllowed)
|
|
if err := devicePlugin.Serve(); err != nil {
|
|
log.Println("Could not contact Kubelet, retrying. Did you enable the device plugin feature gate?")
|
|
} else {
|
|
restart = false
|
|
}
|
|
}
|
|
|
|
select {
|
|
case event := <-watcher.Events:
|
|
if event.Name == pluginapi.KubeletSocket && event.Op&fsnotify.Create == fsnotify.Create {
|
|
log.Printf("Inotify: %s created, restarting.", pluginapi.KubeletSocket)
|
|
restart = true
|
|
}
|
|
|
|
case err := <-watcher.Errors:
|
|
log.Printf("Inotify: %s", err)
|
|
|
|
case s := <-sigs:
|
|
switch s {
|
|
case syscall.SIGHUP:
|
|
log.Println("Received SIGHUP, restarting.")
|
|
restart = true
|
|
default:
|
|
log.Printf("Received signal \"%v\", shutting down.", s)
|
|
if err := devicePlugin.Stop(); err != nil {
|
|
log.Printf("Failed to stop plugin: %+v", err)
|
|
}
|
|
break L
|
|
}
|
|
}
|
|
}
|
|
}
|