Skip to content

Commit

Permalink
Migrate the use of fsnotify to fswatcher in cert_watcher.go (jaegertr…
Browse files Browse the repository at this point in the history
…acing#4232)

Signed-off-by: haanhvu <haanh6594@gmail.com>

## Which problem is this PR solving?
First step for jaegertracing#3924 as discussed
[here](jaegertracing#3924 (comment))

---------

Signed-off-by: haanhvu <haanh6594@gmail.com>
  • Loading branch information
haanhvu authored and shubbham1215 committed Feb 22, 2023
1 parent c73d35b commit 7dc7ba6
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
14 changes: 14 additions & 0 deletions cmd/query/app/mocks/Watcher.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions pkg/config/tlscfg/cert_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (

"github.com/fsnotify/fsnotify"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/pkg/fswatcher"
)

// certWatcher watches filesystem changes on certificates supplied via Options
Expand All @@ -37,7 +39,7 @@ type certWatcher struct {
mu sync.RWMutex
opts Options
logger *zap.Logger
watcher *fsnotify.Watcher
watcher fswatcher.Watcher
cert *tls.Certificate
caHash string
clientCAHash string
Expand All @@ -58,7 +60,7 @@ func newCertWatcher(opts Options, logger *zap.Logger) (*certWatcher, error) {
cert = &c
}

watcher, err := fsnotify.NewWatcher()
watcher, err := fswatcher.NewWatcher()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -148,7 +150,7 @@ func (w *certWatcher) setupWatchedPaths() error {
func (w *certWatcher) watchChangesLoop(rootCAs, clientCAs *x509.CertPool) {
for {
select {
case event, ok := <-w.watcher.Events:
case event, ok := <-w.watcher.Events():
if !ok {
return // channel closed means the watcher is closed
}
Expand All @@ -158,7 +160,7 @@ func (w *certWatcher) watchChangesLoop(rootCAs, clientCAs *x509.CertPool) {
event.Op&fsnotify.Remove == fsnotify.Remove {
w.attemptReload(rootCAs, clientCAs)
}
case err, ok := <-w.watcher.Errors:
case err, ok := <-w.watcher.Errors():
if !ok {
return // channel closed means the watcher is closed
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/config/tlscfg/cert_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import (
"testing"
"time"

"github.com/fsnotify/fsnotify"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"

"github.com/jaegertracing/jaeger/pkg/fswatcher"
)

const (
Expand Down Expand Up @@ -340,7 +341,7 @@ func createTimestampDir(t *testing.T, dir string, ca, cert, key string) {
}

func TestAddCertsToWatch_err(t *testing.T) {
watcher, err := fsnotify.NewWatcher()
watcher, err := fswatcher.NewWatcher()
require.NoError(t, err)
defer watcher.Close()
w := &certWatcher{
Expand Down
6 changes: 6 additions & 0 deletions pkg/fswatcher/fs_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import "github.com/fsnotify/fsnotify"
// Primarily used for mocking the fsnotify lib.
type Watcher interface {
Add(name string) error
Close() error
Events() chan fsnotify.Event
Errors() chan error
}
Expand All @@ -34,6 +35,11 @@ func (f *fsnotifyWatcherWrapper) Add(name string) error {
return f.fsnotifyWatcher.Add(name)
}

// Close closes the watcher.
func (f *fsnotifyWatcherWrapper) Close() error {
return f.fsnotifyWatcher.Close()
}

// Events returns the fsnotify.Watcher's Events chan.
func (f *fsnotifyWatcherWrapper) Events() chan fsnotify.Event {
return f.fsnotifyWatcher.Events
Expand Down
3 changes: 3 additions & 0 deletions pkg/fswatcher/fs_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func TestFsWatcher(t *testing.T) {
err = w.Add("../../cmd/query/app/fixture/ui-config.json")
assert.NoError(t, err)

err = w.Close()
assert.NoError(t, err)

events := w.Events()
assert.NotZero(t, events)

Expand Down

0 comments on commit 7dc7ba6

Please sign in to comment.