Skip to content
Open
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
47 changes: 47 additions & 0 deletions internal/resource/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ func (r *ResourceService) ApplyConfig(ctx context.Context, instanceID string) (*
return nil, fmt.Errorf("failed to parse config %w", parseErr)
}

nginxConfigContext = r.updateConfigContextFiles(ctx, nginxConfigContext)

datasource.UpdateNginxInstanceRuntime(instance, nginxConfigContext)

slog.DebugContext(ctx, "Updated Instance Runtime after parsing config", "instance", instance.GetInstanceRuntime())
Expand Down Expand Up @@ -332,6 +334,51 @@ func (r *ResourceService) UpdateHTTPUpstreamServers(ctx context.Context, instanc
return added, updated, deleted, createPlusAPIError(updateError)
}

func (r *ResourceService) updateConfigContextFiles(ctx context.Context,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add test for this?

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

for _, manifestFile := range manifestFiles {
if manifestFile.ManifestFileMeta.Unmanaged {
for _, configFile := range nginxConfigContext.Files {
if configFile.GetFileMeta().GetName() == manifestFile.ManifestFileMeta.Name {
configFile.Unmanaged = true
}
}
}
}

return nginxConfigContext
}

func (r *ResourceService) manifestFile() (map[string]*model.ManifestFile, error) {
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 convertToUpstreamServer(upstreams []*structpb.Struct) []client.UpstreamServer {
var servers []client.UpstreamServer
res, err := json.Marshal(upstreams)
Expand Down
7 changes: 6 additions & 1 deletion internal/watcher/file/file_watcher_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func NewFileWatcherService(agentConfig *config.Config) *FileWatcherService {
}
}

//nolint:revive // cant simplify due to for loop
func (fws *FileWatcherService) Watch(ctx context.Context, ch chan<- FileUpdateMessage) {
monitoringFrequency := fws.agentConfig.Watchers.FileWatcher.MonitoringFrequency
slog.DebugContext(ctx, "Starting file watcher monitoring", "monitoring_frequency", monitoringFrequency)
Expand All @@ -83,7 +84,11 @@ func (fws *FileWatcherService) Watch(ctx context.Context, ch chan<- FileUpdateMe

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 Down
Loading