Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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 internal/file/file_manager_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func (fms *FileManagerService) ClearCache() {

clear(fms.fileActions)
clear(fms.previousManifestFiles)
slog.Debug("File manager service cleared successfully")
}

//nolint:revive,cyclop // cognitive-complexity of 13 max is 12, loop is needed cant be broken up
Expand Down Expand Up @@ -441,6 +442,12 @@ func (fms *FileManagerService) UpdateManifestFile(ctx context.Context,
slog.DebugContext(ctx, "Updating manifest file", "current_files", currentFiles, "referenced", referenced)
currentManifestFiles, _, readError := fms.manifestFile()

slog.DebugContext(ctx, "Current manifest file")
for _, file := range currentManifestFiles {
slog.DebugContext(ctx, "Manifest File: ", "file", file.ManifestFileMeta.Name, "hash",
file.ManifestFileMeta.Hash, "unmanaged", file.ManifestFileMeta.Unmanaged)
}

// When agent is first started the manifest is updated when an NGINX instance is found, but the manifest file
// will be empty leading to previousManifestFiles being empty. This was causing issues if the first config
// apply failed leading to the manifest file being rolled back to an empty file.
Expand All @@ -461,10 +468,17 @@ func (fms *FileManagerService) UpdateManifestFile(ctx context.Context,
updatedFiles := make(map[string]*model.ManifestFile)

manifestFiles := fms.convertToManifestFileMap(currentFiles, referenced)
slog.DebugContext(ctx, "Files being updated")
for _, file := range currentFiles {
slog.DebugContext(ctx, "Updated File: ", "file", file.GetFileMeta().GetName(), "hash",
file.GetFileMeta().GetHash(), "unmanaged", file.GetUnmanaged())
}

// During a config apply every file is set to unreferenced
// When a new NGINX config context is detected
// we update the files in the manifest by setting the referenced bool to true
if currentManifestFiles != nil && referenced {
slog.DebugContext(ctx, "Referenced set to ture, add current files to updated files ")
for _, currentManifestFile := range currentManifestFiles {
// if file from manifest file is unreferenced add it to updatedFiles map
if !currentManifestFile.ManifestFileMeta.Referenced {
Expand Down
1 change: 1 addition & 0 deletions internal/file/file_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func (fp *FilePlugin) handleConfigApplyComplete(ctx context.Context, msg *bus.Me

fp.messagePipe.Process(ctx, &bus.Message{Topic: bus.DataPlaneResponseTopic, Data: response})
fp.fileManagerService.ClearCache()
slog.DebugContext(ctx, "File manager service cleared successfully, enabling watchers after config apply complete")
fp.enableWatchers(ctx, &model.NginxConfigContext{}, response.GetInstanceId())
}

Expand Down
10 changes: 9 additions & 1 deletion internal/watcher/file/file_watcher_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
}
}

func (fws *FileWatcherService) Watch(ctx context.Context, ch chan<- FileUpdateMessage) {

Check failure on line 60 in internal/watcher/file/file_watcher_service.go

View workflow job for this annotation

GitHub Actions / Lint

cognitive-complexity: function (*FileWatcherService).Watch has cognitive complexity 15 (> max enabled 12) (revive)
monitoringFrequency := fws.agentConfig.Watchers.FileWatcher.MonitoringFrequency
slog.DebugContext(ctx, "Starting file watcher monitoring", "monitoring_frequency", monitoringFrequency)

Expand All @@ -83,7 +83,11 @@

return
case <-instanceWatcherTicker.C:
fws.checkForUpdates(ctx, ch)
if fws.enabled.Load() {
fws.checkForUpdates(ctx, ch)
} else {
slog.DebugContext(ctx, "Skipping check for file updates, file watcher is disabled")
}
}

if fws.watcher != nil {
Expand All @@ -110,6 +114,7 @@
}
}
fws.enabled.Store(false)
slog.DebugContext(ctx, "Successfully disabled file watcher")
}

func (fws *FileWatcherService) EnableWatcher(ctx context.Context) {
Expand All @@ -118,6 +123,7 @@
fws.addWatchers(ctx)
}
fws.enabled.Store(true)
slog.DebugContext(ctx, "Successfully Enabled file watcher")
}

func (fws *FileWatcherService) Update(ctx context.Context, nginxConfigContext *model.NginxConfigContext) {
Expand Down Expand Up @@ -166,6 +172,7 @@
if err != nil {
slog.DebugContext(ctx, "Failed to add file watcher", "directory", directory, "error", err)
} else {
slog.DebugContext(ctx, "Successfully added file watcher", "directory", directory)
fws.filesChanged.Store(true)
}
}
Expand All @@ -185,6 +192,7 @@
fws.filesChanged.Store(true)
} else if _, ok := fws.directoriesToWatch[directoryBeingWatched]; !ok {
fws.removeWatcher(ctx, directoryBeingWatched)
slog.DebugContext(ctx, "Successfully removed file watcher", "directory", directoryBeingWatched)
fws.filesChanged.Store(true)
}
}
Expand Down
3 changes: 3 additions & 0 deletions internal/watcher/watcher_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func (w *Watcher) handleEnableWatchers(ctx context.Context, msg *bus.Message) {
// if config apply ended in a reload there is no need to reparse the config so an empty config context is sent
// from the file plugin
if configContext.InstanceID != "" {
slog.DebugContext(ctx, "Empty instance ID in enable watchers")
w.instanceWatcherService.HandleNginxConfigContextUpdate(ctx, instanceID, configContext)
}

Expand Down Expand Up @@ -213,8 +214,10 @@ func (w *Watcher) handleConfigApplyRequest(ctx context.Context, msg *bus.Message
defer w.watcherMutex.Unlock()
w.instancesWithConfigApplyInProgress = append(w.instancesWithConfigApplyInProgress, instanceID)

slog.DebugContext(ctx, "Watcher plugin disabling watchers for config apply request")
w.fileWatcherService.DisableWatcher(ctx)
w.instanceWatcherService.SetEnabled(false)
slog.DebugContext(ctx, "Watcher plugin successfully disabled watchers for config apply request")
}

func (w *Watcher) handleHealthRequest(ctx context.Context) {
Expand Down
30 changes: 15 additions & 15 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# see https://github.com/evilmartians/lefthook for references
pre-push:
follow: true
piped: true # Stop if one of the steps fail
commands:
1_generate:
run: make generate
2_lint:
run: make lint
3_format:
run: make format
4_check_mod_change:
run: go mod tidy
5_check_local_changes:
run: make no-local-changes
## see https://github.com/evilmartians/lefthook for references
#pre-push:
# follow: true
# piped: true # Stop if one of the steps fail
# commands:
# 1_generate:
# run: make generate
# 2_lint:
# run: make lint
# 3_format:
# run: make format
# 4_check_mod_change:
# run: go mod tidy
# 5_check_local_changes:
# run: make no-local-changes
Loading