Skip to content
Closed
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
15 changes: 15 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 @@

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 @@ -265,8 +266,9 @@
func (fms *FileManagerService) ConfigUpdate(ctx context.Context,
nginxConfigContext *model.NginxConfigContext,
) {

updateError := fms.UpdateCurrentFilesOnDisk(

Check failure on line 270 in internal/file/file_manager_service.go

View workflow job for this annotation

GitHub Actions / Lint

empty-lines: extra empty line at the start of a block (revive)
ctx,

Check failure on line 271 in internal/file/file_manager_service.go

View workflow job for this annotation

GitHub Actions / Lint

File is not properly formatted (gofumpt)
files.ConvertToMapOfFiles(nginxConfigContext.Files),
true,
)
Expand Down Expand Up @@ -441,6 +443,12 @@
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 +469,17 @@
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
6 changes: 6 additions & 0 deletions internal/file/file_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@

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 All @@ -199,6 +200,11 @@
fp.enableWatchers(ctx, successMessage.ConfigContext, successMessage.DataPlaneResponse.GetInstanceId())

if successMessage.ConfigContext.Files != nil {
slog.InfoContext(ctx, "New handleReloadSuccess", "config", successMessage.ConfigContext.Files)
for _, file := range successMessage.ConfigContext.Files {
slog.DebugContext(ctx, "Config context Updating file", "file", file.GetFileMeta().GetName(), "unmanaged", file.GetUnmanaged())

Check failure on line 205 in internal/file/file_plugin.go

View workflow job for this annotation

GitHub Actions / Lint

The line is 138 characters long, which exceeds the maximum of 120 characters. (lll)
}

slog.DebugContext(ctx, "Changes made during config apply, update files on disk")
updateError := fp.fileManagerService.UpdateCurrentFilesOnDisk(
ctx,
Expand Down
57 changes: 57 additions & 0 deletions internal/resource/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
return r.resource
}

func (r *ResourceService) ApplyConfig(ctx context.Context, instanceID string) (*model.NginxConfigContext, error) {

Check failure on line 199 in internal/resource/resource_service.go

View workflow job for this annotation

GitHub Actions / Lint

cognitive complexity 22 of func `(*ResourceService).ApplyConfig` is high (> 20) (gocognit)
var instance *mpi.Instance
operator := r.instanceOperators[instanceID]

Expand All @@ -216,6 +216,35 @@
return nil, fmt.Errorf("failed to parse config %w", parseErr)
}

slog.DebugContext(ctx, "Before Config Context -- ConfigUpdate")
for _, configFile := range nginxConfigContext.Files {
slog.DebugContext(ctx, "Before Config file -- ConfigUpdate", "file", configFile.GetFileMeta().GetName(),
"unmanaged", configFile.GetUnmanaged(),
)
}

manifestFiles, manifestErr := r.manifestFile()
if manifestErr != nil {
slog.ErrorContext(ctx, "Error getting manifest files", "error", manifestErr)
}

for _, manifestFiles := range manifestFiles {
if manifestFiles.ManifestFileMeta.Unmanaged == true {

Check failure on line 232 in internal/resource/resource_service.go

View workflow job for this annotation

GitHub Actions / Lint

bool-literal-in-expr: omit Boolean literal in expression (revive)
for _, configFile := range nginxConfigContext.Files {
if configFile.GetFileMeta().GetName() == manifestFiles.ManifestFileMeta.Name {
configFile.Unmanaged = true
}
}
}
}

slog.DebugContext(ctx, "Changed Config Context -- ConfigUpdate")
for _, configFile := range nginxConfigContext.Files {
slog.DebugContext(ctx, "After Config file -- ConfigUpdate", "file", configFile.GetFileMeta().GetName(),
"unmanaged", configFile.GetUnmanaged(),
)
}

datasource.UpdateNginxInstanceRuntime(instance, nginxConfigContext)

slog.DebugContext(ctx, "Updated Instance Runtime after parsing config", "instance", instance.GetInstanceRuntime())
Expand All @@ -239,9 +268,37 @@

slog.DebugContext(ctx, "Updated Instance Runtime after reloading NGINX", "instance", instance.GetInstanceRuntime())

slog.InfoContext(ctx, "New Nginx config Context in Apply Config", "config", nginxConfigContext.Files)
for _, file := range nginxConfigContext.Files {
slog.DebugContext(ctx, "Config context Updating file", "file", file.GetFileMeta().GetName(), "unmanaged", file.GetUnmanaged())

Check failure on line 273 in internal/resource/resource_service.go

View workflow job for this annotation

GitHub Actions / Lint

The line is 134 characters long, which exceeds the maximum of 120 characters. (lll)
}
return nginxConfigContext, nil

Check failure on line 275 in internal/resource/resource_service.go

View workflow job for this annotation

GitHub Actions / Lint

return with no blank line before (nlreturn)
}

func (r *ResourceService) manifestFile() (map[string]*model.ManifestFile, error) {

Check failure on line 278 in internal/resource/resource_service.go

View workflow job for this annotation

GitHub Actions / Lint

unexported method "manifestFile" for struct "ResourceService" should be placed after the exported method "UpdateHTTPUpstreamServers" (funcorder)
if _, err := os.Stat(r.agentConfig.LibDir + "/manifest.json"); err != nil {
return nil, err
}

file, err := os.ReadFile(r.agentConfig.LibDir + "/manifest.json")
if err != nil {
return nil, fmt.Errorf("failed to read manifest file: %w", err)
}

var manifestFiles map[string]*model.ManifestFile

err = json.Unmarshal(file, &manifestFiles)
if err != nil {
if len(file) == 0 {
return nil, fmt.Errorf("manifest file is empty: %w", err)
}

return nil, fmt.Errorf("failed to parse manifest file: %w", err)
}

return manifestFiles, nil
}

func (r *ResourceService) GetHTTPUpstreamServers(ctx context.Context, instance *mpi.Instance,
upstream string,
) ([]client.UpstreamServer, error) {
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