5252 reader * bufio.Reader ,
5353 chunkID uint32 ,
5454 ) (mpi.FileDataChunk_Content , error )
55- WriteManifestFile (updatedFiles map [string ]* model.ManifestFile ,
55+ WriteManifestFile (ctx context. Context , updatedFiles map [string ]* model.ManifestFile ,
5656 manifestDir , manifestPath string ) (writeError error )
5757 }
5858
6868 fileToUpdate * mpi.File ,
6969 ) error
7070 SetIsConnected (isConnected bool )
71- UpdateClient (fileServiceClient mpi.FileServiceClient )
71+ UpdateClient (ctx context. Context , fileServiceClient mpi.FileServiceClient )
7272 }
7373
7474 fileManagerServiceInterface interface {
8686 ) (map [string ]* model.FileCache , map [string ][]byte , error )
8787 IsConnected () bool
8888 SetIsConnected (isConnected bool )
89- ResetClient (fileServiceClient mpi.FileServiceClient )
89+ ResetClient (ctx context. Context , fileServiceClient mpi.FileServiceClient )
9090 }
9191)
9292
@@ -103,6 +103,7 @@ type FileManagerService struct {
103103 currentFilesOnDisk map [string ]* mpi.File // key is file path
104104 previousManifestFiles map [string ]* model.ManifestFile
105105 manifestFilePath string
106+ rollbackManifest bool
106107 filesMutex sync.RWMutex
107108}
108109
@@ -118,12 +119,14 @@ func NewFileManagerService(fileServiceClient mpi.FileServiceClient, agentConfig
118119 currentFilesOnDisk : make (map [string ]* mpi.File ),
119120 previousManifestFiles : make (map [string ]* model.ManifestFile ),
120121 manifestFilePath : agentConfig .ManifestDir + "/manifest.json" ,
122+ rollbackManifest : true ,
121123 manifestLock : manifestLock ,
122124 }
123125}
124126
125- func (fms * FileManagerService ) ResetClient (fileServiceClient mpi.FileServiceClient ) {
126- fms .fileServiceOperator .UpdateClient (fileServiceClient )
127+ func (fms * FileManagerService ) ResetClient (ctx context.Context , fileServiceClient mpi.FileServiceClient ) {
128+ fms .fileServiceOperator .UpdateClient (ctx , fileServiceClient )
129+ slog .DebugContext (ctx , "File manager service reset client successfully" )
127130}
128131
129132func (fms * FileManagerService ) IsConnected () bool {
@@ -167,6 +170,7 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
167170
168171 fileErr := fms .executeFileActions (ctx )
169172 if fileErr != nil {
173+ fms .rollbackManifest = false
170174 return model .RollbackRequired , fileErr
171175 }
172176 fileOverviewFiles := files .ConvertToMapOfFiles (fileOverview .GetFiles ())
@@ -185,6 +189,7 @@ func (fms *FileManagerService) ClearCache() {
185189 clear (fms .previousManifestFiles )
186190}
187191
192+ //nolint:revive // cognitive-complexity of 13 max is 12, loop is needed cant be broken up
188193func (fms * FileManagerService ) Rollback (ctx context.Context , instanceID string ) error {
189194 slog .InfoContext (ctx , "Rolling back config for instance" , "instance_id" , instanceID )
190195
@@ -218,12 +223,13 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
218223 }
219224 }
220225
221- slog .InfoContext (ctx , "Rolling back config for instance, writing manifest file" ,
222- "manifest_previous" , fms .previousManifestFiles )
223- manifestFileErr := fms .fileOperator .WriteManifestFile (fms .previousManifestFiles ,
224- fms .agentConfig .ManifestDir , fms .manifestFilePath )
225- if manifestFileErr != nil {
226- return manifestFileErr
226+ if fms .rollbackManifest {
227+ slog .DebugContext (ctx , "Rolling back manifest file" , "manifest_previous" , fms .previousManifestFiles )
228+ manifestFileErr := fms .fileOperator .WriteManifestFile (ctx , fms .previousManifestFiles ,
229+ fms .agentConfig .ManifestDir , fms .manifestFilePath )
230+ if manifestFileErr != nil {
231+ return manifestFileErr
232+ }
227233 }
228234
229235 return nil
@@ -382,7 +388,7 @@ func (fms *FileManagerService) UpdateCurrentFilesOnDisk(
382388 fms .currentFilesOnDisk [currentFile .GetFileMeta ().GetName ()] = currentFile
383389 }
384390
385- err := fms .UpdateManifestFile (currentFiles , referenced )
391+ err := fms .UpdateManifestFile (ctx , currentFiles , referenced )
386392 if err != nil {
387393 return fmt .Errorf ("failed to update manifest file: %w" , err )
388394 }
@@ -393,17 +399,24 @@ func (fms *FileManagerService) UpdateCurrentFilesOnDisk(
393399// seems to be a control flag, avoid control coupling
394400//
395401//nolint:revive // referenced is a required flag
396- func (fms * FileManagerService ) UpdateManifestFile (currentFiles map [string ]* mpi.File , referenced bool ) (err error ) {
397- slog .Debug ("Updating manifest file" , "current_files" , currentFiles , "referenced" , referenced )
402+ func (fms * FileManagerService ) UpdateManifestFile (ctx context.Context ,
403+ currentFiles map [string ]* mpi.File , referenced bool ,
404+ ) (err error ) {
405+ slog .DebugContext (ctx , "Updating manifest file" , "current_files" , currentFiles , "referenced" , referenced )
398406 currentManifestFiles , _ , readError := fms .manifestFile ()
399407
408+ // When agent is first started the manifest is updated when an NGINX instance is found, but the manifest file
409+ // will be empty leading to previousManifestFiles being empty. This was causing issues if the first config
410+ // apply failed leading to the manifest file being rolled back to an empty file.
411+ // If the currentManifestFiles is empty then we can assume the Agent has just started and this is the first
412+ // write of the Manifest file, so set previousManifestFiles to be the currentFiles.
400413 if len (currentManifestFiles ) == 0 {
401414 currentManifestFiles = fms .convertToManifestFileMap (currentFiles , referenced )
402415 }
403416
404417 fms .previousManifestFiles = currentManifestFiles
405418 if readError != nil && ! errors .Is (readError , os .ErrNotExist ) {
406- slog .Debug ( "Error reading manifest file" , "current_manifest_files" ,
419+ slog .DebugContext ( ctx , "Error reading manifest file" , "current_manifest_files" ,
407420 currentManifestFiles , "updated_files" , currentFiles , "referenced" , referenced )
408421
409422 return fmt .Errorf ("unable to read manifest file: %w" , readError )
@@ -429,7 +442,7 @@ func (fms *FileManagerService) UpdateManifestFile(currentFiles map[string]*mpi.F
429442 updatedFiles = manifestFiles
430443 }
431444
432- return fms .fileOperator .WriteManifestFile (updatedFiles , fms .agentConfig .ManifestDir , fms .manifestFilePath )
445+ return fms .fileOperator .WriteManifestFile (ctx , updatedFiles , fms .agentConfig .ManifestDir , fms .manifestFilePath )
433446}
434447
435448func (fms * FileManagerService ) manifestFile () (map [string ]* model.ManifestFile , map [string ]* mpi.File , error ) {
0 commit comments