Skip to content

Commit

Permalink
fix: send datasync on remove fs events
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <[email protected]>
  • Loading branch information
toddbaert committed Jan 28, 2023
1 parent 824cf1a commit ae57cb8
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions pkg/sync/file/filepath_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,11 @@ 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:
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 +83,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 ae57cb8

Please sign in to comment.