k8s.io/kubernetes@v1.29.3/pkg/util/filesystem/watcher.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package filesystem
    18  
    19  import (
    20  	"github.com/fsnotify/fsnotify"
    21  )
    22  
    23  // FSWatcher is a callback-based filesystem watcher abstraction for fsnotify.
    24  type FSWatcher interface {
    25  	// Initializes the watcher with the given watch handlers.
    26  	// Called before all other methods.
    27  	Init(FSEventHandler, FSErrorHandler) error
    28  
    29  	// Starts listening for events and errors.
    30  	// When an event or error occurs, the corresponding handler is called.
    31  	Run()
    32  
    33  	// Add a filesystem path to watch
    34  	AddWatch(path string) error
    35  }
    36  
    37  // FSEventHandler is called when a fsnotify event occurs.
    38  type FSEventHandler func(event fsnotify.Event)
    39  
    40  // FSErrorHandler is called when a fsnotify error occurs.
    41  type FSErrorHandler func(err error)
    42  
    43  type fsnotifyWatcher struct {
    44  	watcher      *fsnotify.Watcher
    45  	eventHandler FSEventHandler
    46  	errorHandler FSErrorHandler
    47  }
    48  
    49  var _ FSWatcher = &fsnotifyWatcher{}
    50  
    51  // NewFsnotifyWatcher returns an implementation of FSWatcher that continuously listens for
    52  // fsnotify events and calls the event handler as soon as an event is received.
    53  func NewFsnotifyWatcher() FSWatcher {
    54  	return &fsnotifyWatcher{}
    55  }
    56  
    57  func (w *fsnotifyWatcher) AddWatch(path string) error {
    58  	return w.watcher.Add(path)
    59  }
    60  
    61  func (w *fsnotifyWatcher) Init(eventHandler FSEventHandler, errorHandler FSErrorHandler) error {
    62  	var err error
    63  	w.watcher, err = fsnotify.NewWatcher()
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	w.eventHandler = eventHandler
    69  	w.errorHandler = errorHandler
    70  	return nil
    71  }
    72  
    73  func (w *fsnotifyWatcher) Run() {
    74  	go func() {
    75  		defer w.watcher.Close()
    76  		for {
    77  			select {
    78  			case event := <-w.watcher.Events:
    79  				if w.eventHandler != nil {
    80  					w.eventHandler(event)
    81  				}
    82  			case err := <-w.watcher.Errors:
    83  				if w.errorHandler != nil {
    84  					w.errorHandler(err)
    85  				}
    86  			}
    87  		}
    88  	}()
    89  }