Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate the use of fsnotify to fswatcher in cert_watcher.go #4232

Merged
merged 2 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test for this func

Copy link
Contributor Author

@haanhvu haanhvu Feb 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I'll do it.

Is there any command that checks Codecov failure locally? I ran make test and as I can remember it didn't report the failure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to my knowledge. You can only compare package level coverage before and after the change with go test -cover.

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