Skip to content

Commit

Permalink
fix: send datasync on remove fs events (#339)
Browse files Browse the repository at this point in the history
Looks like during a refactor, we accidentally removed the "meat" of the
remove handler.

In K8S, we never get write events, just remove. It's necessary to send
the data for these as well.

Also extracted some duplicated code into a func.

---------

Signed-off-by: Todd Baert <toddbaert@gmail.com>
  • Loading branch information
toddbaert authored Jan 28, 2023
1 parent 824cf1a commit 4c9aaac
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions pkg/sync/file/filepath_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,13 @@ func (fs *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error {
fs.Logger.Info(fmt.Sprintf("Filepath event: %s %s", event.Name, event.Op.String()))

switch event.Op {
case fsnotify.Create:
fs.Logger.Debug("New configuration created")
msg, err := fs.fetch(ctx)
if err != nil {
fs.Logger.Error(fmt.Sprintf("Error fetching after Create notification: %s", err.Error()))
continue
}

dataSync <- sync.DataSync{FlagData: msg, Source: fs.URI}
case fsnotify.Write:
fs.Logger.Debug("Configuration modified")
msg, err := fs.fetch(ctx)
if err != nil {
fs.Logger.Error(fmt.Sprintf("Error fetching after Write notification: %s", err.Error()))
continue
}

dataSync <- sync.DataSync{FlagData: msg, Source: fs.URI}
case fsnotify.Create, fsnotify.Write:
fs.sendDataSync(ctx, event, dataSync)
case fsnotify.Remove:
// Counterintuively, remove events are the only meanful ones seen in K8s.
// At the point the remove event is fired, we have our new data, so we can send it down the channel.
fs.sendDataSync(ctx, event, dataSync)

// K8s exposes config maps as symlinks.
// Updates cause a remove event, we need to re-add the watcher in this case.
err = watcher.Add(fs.URI)
Expand All @@ -97,6 +85,16 @@ func (fs *Sync) Sync(ctx context.Context, dataSync chan<- sync.DataSync) error {
}
}

func (fs *Sync) sendDataSync(ctx context.Context, eventType fsnotify.Event, dataSync chan<- sync.DataSync) {
fs.Logger.Debug(fmt.Sprintf("Configuration %s: %s", fs.URI, eventType.Op.String()))
msg, err := fs.fetch(ctx)
if err != nil {
fs.Logger.Error(fmt.Sprintf("Error fetching after %s notification: %s", eventType.Op.String(), err.Error()))
}

dataSync <- sync.DataSync{FlagData: msg, Source: fs.URI}
}

func (fs *Sync) fetch(_ context.Context) (string, error) {
if fs.URI == "" {
return "", errors.New("no filepath string set")
Expand Down

0 comments on commit 4c9aaac

Please sign in to comment.