Skip to content

Commit

Permalink
Merge pull request #496 from elezar/create-watcher-package
Browse files Browse the repository at this point in the history
Move fs and signal handling to internal pkg
  • Loading branch information
ArangoGutierrez authored Feb 2, 2024
2 parents 2fe4ca6 + 0d0e46b commit 7e6e376
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 45 deletions.
3 changes: 2 additions & 1 deletion cmd/gpu-feature-discovery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/NVIDIA/k8s-device-plugin/internal/logger"
"github.com/NVIDIA/k8s-device-plugin/internal/resource"
"github.com/NVIDIA/k8s-device-plugin/internal/vgpu"
"github.com/NVIDIA/k8s-device-plugin/internal/watch"
)

var nodeFeatureAPI bool
Expand Down Expand Up @@ -121,7 +122,7 @@ func start(c *cli.Context, flags []cli.Flag) error {
}()

klog.Info("Starting OS watcher.")
sigs := newOSWatcher(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sigs := watch.Signals(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

for {
// Load the configuration file
Expand Down
3 changes: 2 additions & 1 deletion cmd/gpu-feature-discovery/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/NVIDIA/k8s-device-plugin/internal/resource"
rt "github.com/NVIDIA/k8s-device-plugin/internal/resource/testing"
"github.com/NVIDIA/k8s-device-plugin/internal/vgpu"
"github.com/NVIDIA/k8s-device-plugin/internal/watch"
)

const (
Expand Down Expand Up @@ -184,7 +185,7 @@ func TestRunWithNoTimestamp(t *testing.T) {

func TestRunSleep(t *testing.T) {
log.Println("Starting OS watcher.")
sigs := newOSWatcher(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sigs := watch.Signals(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

nvmlMock := NewTestNvmlMock()
vgpuMock := NewTestVGPUMock()
Expand Down
7 changes: 4 additions & 3 deletions cmd/nvidia-device-plugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/NVIDIA/k8s-device-plugin/internal/logger"
"github.com/NVIDIA/k8s-device-plugin/internal/plugin"
"github.com/NVIDIA/k8s-device-plugin/internal/rm"
"github.com/NVIDIA/k8s-device-plugin/internal/watch"
)

func main() {
Expand Down Expand Up @@ -154,14 +155,14 @@ func loadConfig(c *cli.Context, flags []cli.Flag) (*spec.Config, error) {

func start(c *cli.Context, flags []cli.Flag) error {
klog.Info("Starting FS watcher.")
watcher, err := newFSWatcher(pluginapi.DevicePluginPath)
watcher, err := watch.Files(pluginapi.DevicePluginPath)
if err != nil {
return fmt.Errorf("failed to create FS watcher: %v", err)
return fmt.Errorf("failed to create FS watcher for %s: %v", pluginapi.DevicePluginPath, err)
}
defer watcher.Close()

klog.Info("Starting OS watcher.")
sigs := newOSWatcher(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sigs := watch.Signals(syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

var started bool
var restartTimeout <-chan time.Time
Expand Down
32 changes: 0 additions & 32 deletions cmd/nvidia-device-plugin/watchers.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
/*
# Copyright NVIDIA CORPORATION
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -12,18 +12,36 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package main
*/
package watch

import (
"os"
"os/signal"

"github.com/fsnotify/fsnotify"
)

// newOSWatcher creates a channel for receiving OS signals.
// TODO: This is currently duplicated from device-plugin
func newOSWatcher(sigs ...os.Signal) chan os.Signal {
// Files creates a Watcher for the specified files.
func Files(files ...string) (*fsnotify.Watcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}

for _, f := range files {
err = watcher.Add(f)
if err != nil {
watcher.Close()
return nil, err
}
}

return watcher, nil
}

// Signals creats a channel for the specified signals.
func Signals(sigs ...os.Signal) chan os.Signal {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, sigs...)

Expand Down

0 comments on commit 7e6e376

Please sign in to comment.