// If we are restarting, stop plugins from previous run. if restarting { err := stopPlugins(plugins) if err != nil { return fmt.Errorf("error stopping plugins from previous run: %v", err) } }
//如果启动失败,30s后再次启动 if restartPlugins { klog.Infof("Failed to start one or more plugins. Retrying in 30s...") restartTimeout = time.After(30 * time.Second) }
restarting = true
// Start an infinite loop, waiting for several indicators to either log // some messages, trigger a restart of the plugins, or exit the program. //无限循环,监听文件系统和信号 for { select { // If the restart timeout has expired, then restart the plugins case <-restartTimeout: goto restart
// Detect a kubelet restart by watching for a newly created // 'pluginapi.KubeletSocket' file. When this occurs, restart this loop, // restarting all of the plugins in the process. // 监听/var/lib/kubelet/device-plugins/kubelet.sock,如果创建重启 case event := <-watcher.Events: if event.Name == pluginapi.KubeletSocket && event.Op&fsnotify.Create == fsnotify.Create { klog.Infof("inotify: %s created, restarting.", pluginapi.KubeletSocket) goto restart }
// Watch for any other fs errors and log them. case err := <-watcher.Errors: klog.Infof("inotify: %s", err)
// Watch for any signals from the OS. On SIGHUP, restart this loop, // restarting all of the plugins in the process. On all other // signals, exit the loop and exit the program. case s := <-sigs: switch s { case syscall.SIGHUP: klog.Info("Received SIGHUP, restarting.") goto restart default: klog.Infof("Received signal \"%v\", shutting down.", s) goto exit } } }