Skip to content

Commit 7ddcf17

Browse files
authored
Update manifest when file deleted on disk (#1089)
1 parent 6b72b7b commit 7ddcf17

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

internal/file/file_manager_service.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,6 @@ func (fms *FileManagerService) DetermineFileActions(
771771
// if file doesn't exist in the current files, file has been added
772772
// set file action
773773
if _, statErr := os.Stat(modifiedFile.File.GetFileMeta().GetName()); errors.Is(statErr, os.ErrNotExist) {
774-
slog.Info("File is not present on disk", "file", modifiedFile.File.GetFileMeta().GetName())
775774
modifiedFile.Action = model.Add
776775
fileDiff[modifiedFile.File.GetFileMeta().GetName()] = modifiedFile
777776

@@ -824,20 +823,31 @@ func (fms *FileManagerService) UpdateManifestFile(currentFiles map[string]*mpi.F
824823
return fmt.Errorf("unable to read manifest file: %w", readError)
825824
}
826825

827-
manifestFiles := fms.convertToManifestFileMap(currentFiles, referenced)
826+
updatedFiles := make(map[string]*model.ManifestFile)
828827

828+
manifestFiles := fms.convertToManifestFileMap(currentFiles, referenced)
829829
// During a config apply every file is set to unreferenced
830830
// When a new NGINX config context is detected
831831
// we update the files in the manifest by setting the referenced bool to true
832832
if currentManifestFiles != nil && referenced {
833+
for _, currentManifestFile := range currentManifestFiles {
834+
// if file from manifest file is unreferenced add it to updatedFiles map
835+
if !currentManifestFile.ManifestFileMeta.Referenced {
836+
updatedFiles[currentManifestFile.ManifestFileMeta.Name] = currentManifestFile
837+
}
838+
}
833839
for manifestFileName, manifestFile := range manifestFiles {
834-
currentManifestFiles[manifestFileName] = manifestFile
840+
updatedFiles[manifestFileName] = manifestFile
835841
}
836842
} else {
837-
currentManifestFiles = manifestFiles
843+
updatedFiles = manifestFiles
838844
}
839845

840-
manifestJSON, err := json.MarshalIndent(currentManifestFiles, "", " ")
846+
return fms.writeManifestFile(updatedFiles)
847+
}
848+
849+
func (fms *FileManagerService) writeManifestFile(updatedFiles map[string]*model.ManifestFile) error {
850+
manifestJSON, err := json.MarshalIndent(updatedFiles, "", " ")
841851
if err != nil {
842852
return fmt.Errorf("unable to marshal manifest file json: %w", err)
843853
}

internal/file/file_plugin.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,17 @@ func (fp *FilePlugin) handleConfigApplySuccess(ctx context.Context, msg *bus.Mes
136136
}
137137

138138
fp.fileManagerService.ClearCache()
139-
updateError := fp.fileManagerService.UpdateCurrentFilesOnDisk(
140-
ctx,
141-
files.ConvertToMapOfFiles(successMessage.ConfigContext.Files),
142-
true,
143-
)
144-
if updateError != nil {
145-
slog.ErrorContext(ctx, "Unable to update current files on disk", "error", updateError)
139+
140+
if successMessage.ConfigContext.Files != nil {
141+
slog.DebugContext(ctx, "Changes made during config apply, update files on disk")
142+
updateError := fp.fileManagerService.UpdateCurrentFilesOnDisk(
143+
ctx,
144+
files.ConvertToMapOfFiles(successMessage.ConfigContext.Files),
145+
true,
146+
)
147+
if updateError != nil {
148+
slog.ErrorContext(ctx, "Unable to update current files on disk", "error", updateError)
149+
}
146150
}
147151
fp.messagePipe.Process(ctx, &bus.Message{Topic: bus.DataPlaneResponseTopic, Data: successMessage.DataPlaneResponse})
148152
}

0 commit comments

Comments
 (0)