Skip to content

Commit

Permalink
Fixing codeFactor "Complex Method"
Browse files Browse the repository at this point in the history
Signed-off-by: Webber Huang <webber.huang@suse.com>
  • Loading branch information
WebberHuang1118 committed Feb 19, 2024
1 parent e33390a commit 8a337ca
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pkg/deviceplugins/device_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ func (dp *PCIDevicePlugin) Allocate(_ context.Context, r *pluginapi.AllocateRequ
}

func (dp *PCIDevicePlugin) healthCheck() error {
logger := log.DefaultLogger()
monitoredDevices := map[string]string{}
monitoredDevices := make(map[string]string)
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("failed to creating a fsnotify watcher: %v", err)
Expand Down Expand Up @@ -349,33 +348,37 @@ func (dp *PCIDevicePlugin) healthCheck() error {
return fmt.Errorf("failed to watch device-plugin socket: %v", err)
}

return dp.performCheck(monitoredDevices, watcher)
}

func (dp *PCIDevicePlugin) performCheck(monitoredDevices map[string]string, watcher *fsnotify.Watcher) error {
for {
select {
case <-dp.stop:
return nil
case <-dp.done:
return nil
case err := <-watcher.Errors:
logger.Reason(err).Errorf("error watching devices and device plugin directory")
logrus.Errorf("error watching devices and device plugin directory: %v", err)
case event := <-watcher.Events:
logger.V(4).Infof("health Event: %v", event)
logrus.Infof("health Event: %v", event)
if monDevID, exist := monitoredDevices[event.Name]; exist {
// Health in this case is if the device path actually exists
if event.Op == fsnotify.Create {
logger.Infof("monitored device %s appeared", dp.resourceName)
logrus.Infof("monitored device %s appeared", dp.resourceName)
dp.health <- deviceHealth{
DevID: monDevID,
Health: pluginapi.Healthy,
}
} else if (event.Op == fsnotify.Remove) || (event.Op == fsnotify.Rename) {
logger.Infof("monitored device %s disappeared", dp.resourceName)
logrus.Infof("monitored device %s disappeared", dp.resourceName)
dp.health <- deviceHealth{
DevID: monDevID,
Health: pluginapi.Unhealthy,
}
}
} else if event.Name == dp.socketPath && event.Op == fsnotify.Remove {
logger.Infof("device socket file for device %s was removed, kubelet probably restarted.", dp.resourceName)
logrus.Infof("device socket file for device %s was removed, kubelet probably restarted.", dp.resourceName)
return nil
}
}
Expand Down

0 comments on commit 8a337ca

Please sign in to comment.