Skip to content

Commit 6a7b70d

Browse files
authored
Recreate referenced files that were manually deleted on the data plane during a config apply (#1084)
1 parent 56c539a commit 6a7b70d

File tree

3 files changed

+24
-34
lines changed

3 files changed

+24
-34
lines changed

internal/file/file_manager_service.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (fms *FileManagerService) UpdateOverview(
195195
delta := files.ConvertToMapOfFiles(response.GetOverview().GetFiles())
196196

197197
if len(delta) != 0 {
198-
return fms.updateFiles(ctx, delta, instanceID, iteration)
198+
return fms.updateFiles(ctx, delta, request.GetOverview().GetFiles(), instanceID, iteration)
199199
}
200200

201201
return err
@@ -219,6 +219,7 @@ func (fms *FileManagerService) setupIdentifiers(ctx context.Context, iteration i
219219
func (fms *FileManagerService) updateFiles(
220220
ctx context.Context,
221221
delta map[string]*mpi.File,
222+
fileOverview []*mpi.File,
222223
instanceID string,
223224
iteration int,
224225
) error {
@@ -234,7 +235,7 @@ func (fms *FileManagerService) updateFiles(
234235
iteration++
235236
slog.Debug("Updating file overview", "attempt_number", iteration)
236237

237-
return fms.UpdateOverview(ctx, instanceID, diffFiles, iteration)
238+
return fms.UpdateOverview(ctx, instanceID, fileOverview, iteration)
238239
}
239240

240241
func (fms *FileManagerService) UpdateFile(
@@ -728,7 +729,7 @@ func (fms *FileManagerService) DetermineFileActions(
728729
fileDiff := make(map[string]*model.FileCache) // Files that have changed, key is file name
729730
fileContents := make(map[string][]byte) // contents of the file, key is file name
730731

731-
manifestFiles, filesMap, manifestFileErr := fms.manifestFile()
732+
_, filesMap, manifestFileErr := fms.manifestFile()
732733

733734
if manifestFileErr != nil {
734735
if errors.Is(manifestFileErr, os.ErrNotExist) {
@@ -769,14 +770,15 @@ func (fms *FileManagerService) DetermineFileActions(
769770
}
770771
// if file doesn't exist in the current files, file has been added
771772
// set file action
772-
if !ok {
773+
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())
773775
modifiedFile.Action = model.Add
774776
fileDiff[modifiedFile.File.GetFileMeta().GetName()] = modifiedFile
775777

776778
continue
777779
// if file currently exists and file hash is different, file has been updated
778780
// copy contents, set file action
779-
} else if modifiedFile.File.GetFileMeta().GetHash() != currentFile.GetFileMeta().GetHash() {
781+
} else if ok && modifiedFile.File.GetFileMeta().GetHash() != currentFile.GetFileMeta().GetHash() {
780782
fileContent, readErr := os.ReadFile(fileName)
781783
if readErr != nil {
782784
return nil, nil, fmt.Errorf("error reading file %s, error: %w", fileName, readErr)
@@ -785,24 +787,6 @@ func (fms *FileManagerService) DetermineFileActions(
785787
fileContents[fileName] = fileContent
786788
fileDiff[modifiedFile.File.GetFileMeta().GetName()] = modifiedFile
787789
}
788-
789-
// If the file is unreferenced we check if the file has been updated since the last time
790-
// Also check if the unreferenced file still exists
791-
if manifestFiles[modifiedFile.File.GetFileMeta().GetName()] != nil &&
792-
!manifestFiles[modifiedFile.File.GetFileMeta().GetName()].ManifestFileMeta.Referenced {
793-
if fileStats, err := os.Stat(modifiedFile.File.GetFileMeta().GetName()); errors.Is(err, os.ErrNotExist) {
794-
modifiedFile.Action = model.Add
795-
fileDiff[modifiedFile.File.GetFileMeta().GetName()] = modifiedFile
796-
} else if timestamppb.New(fileStats.ModTime()) != modifiedFile.File.GetFileMeta().GetModifiedTime() {
797-
fileContent, readErr := os.ReadFile(fileName)
798-
if readErr != nil {
799-
return nil, nil, fmt.Errorf("error reading file %s, error: %w", fileName, readErr)
800-
}
801-
modifiedFile.Action = model.Update
802-
fileContents[fileName] = fileContent
803-
fileDiff[modifiedFile.File.GetFileMeta().GetName()] = modifiedFile
804-
}
805-
}
806790
}
807791

808792
return fileDiff, fileContents, nil

internal/file/file_manager_service_test.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,14 +563,21 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
563563
updateErr := os.WriteFile(updateTestFile.Name(), updatedFileContent, 0o600)
564564
require.NoError(t, updateErr)
565565

566-
addTestFileName := tempDir + "/nginx_add.conf"
566+
addTestFileName := tempDir + "nginx_add.conf"
567567

568568
unmanagedFile := helpers.CreateFileWithErrorCheck(t, tempDir, "nginx_unmanaged.conf")
569569
defer helpers.RemoveFileWithErrorCheck(t, unmanagedFile.Name())
570570
unmanagedFileContent := []byte("test unmanaged file")
571571
unmanagedErr := os.WriteFile(unmanagedFile.Name(), unmanagedFileContent, 0o600)
572572
require.NoError(t, unmanagedErr)
573573

574+
addTestFile := helpers.CreateFileWithErrorCheck(t, tempDir, "nginx_add.conf")
575+
defer helpers.RemoveFileWithErrorCheck(t, addTestFile.Name())
576+
t.Logf("Adding file: %s", addTestFile.Name())
577+
addFileContent := []byte("test add file")
578+
addErr := os.WriteFile(addTestFile.Name(), addFileContent, 0o600)
579+
require.NoError(t, addErr)
580+
574581
tests := []struct {
575582
expectedError error
576583
modifiedFiles map[string]*model.FileCache
@@ -645,9 +652,9 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
645652
{
646653
name: "Test 2: Files same as on disk",
647654
modifiedFiles: map[string]*model.FileCache{
648-
addTestFileName: {
655+
addTestFile.Name(): {
649656
File: &mpi.File{
650-
FileMeta: protos.FileMeta(addTestFileName, files.GenerateHash(fileContent)),
657+
FileMeta: protos.FileMeta(addTestFile.Name(), files.GenerateHash(fileContent)),
651658
},
652659
},
653660
updateTestFile.Name(): {
@@ -668,8 +675,8 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
668675
updateTestFile.Name(): {
669676
FileMeta: protos.FileMeta(updateTestFile.Name(), files.GenerateHash(fileContent)),
670677
},
671-
addTestFileName: {
672-
FileMeta: protos.FileMeta(addTestFileName, files.GenerateHash(fileContent)),
678+
addTestFile.Name(): {
679+
FileMeta: protos.FileMeta(addTestFile.Name(), files.GenerateHash(fileContent)),
673680
},
674681
},
675682
expectedCache: make(map[string]*model.FileCache),
@@ -685,10 +692,11 @@ func TestFileManagerService_DetermineFileActions(t *testing.T) {
685692
defer manifestFile.Close()
686693
manifestDirPath = tempDir
687694
manifestFilePath = manifestFile.Name()
688-
t.Logf("path: %s", manifestFilePath)
695+
689696
fakeFileServiceClient := &v1fakes.FakeFileServiceClient{}
690697
fileManagerService := NewFileManagerService(fakeFileServiceClient, types.AgentConfig())
691698
require.NoError(tt, err)
699+
692700
diff, contents, fileActionErr := fileManagerService.DetermineFileActions(test.currentFiles,
693701
test.modifiedFiles)
694702
require.NoError(tt, fileActionErr)

internal/watcher/watcher_plugin.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,9 @@ func (w *Watcher) handleConfigApplySuccess(ctx context.Context, msg *bus.Message
198198

199199
// If the config apply had no changes to any files, it is results in a ConfigApplySuccessfulTopic with an empty
200200
// configContext being sent, there is no need to reparse the config as no change has occurred.
201-
if successMessage.ConfigContext.InstanceID == "" {
202-
slog.DebugContext(ctx, "NginxConfigContext is empty, no need to reparse config")
203-
return
201+
if successMessage.ConfigContext.InstanceID != "" {
202+
w.instanceWatcherService.HandleNginxConfigContextUpdate(ctx, instanceID, successMessage.ConfigContext)
204203
}
205-
w.instanceWatcherService.HandleNginxConfigContextUpdate(ctx, instanceID, successMessage.ConfigContext)
206204

207205
w.watcherMutex.Lock()
208206
w.instancesWithConfigApplyInProgress = slices.DeleteFunc(
@@ -213,8 +211,8 @@ func (w *Watcher) handleConfigApplySuccess(ctx context.Context, msg *bus.Message
213211
)
214212

215213
w.fileWatcherService.SetEnabled(true)
216-
w.watcherMutex.Unlock()
217214
w.instanceWatcherService.SetEnabled(true)
215+
w.watcherMutex.Unlock()
218216
}
219217

220218
func (w *Watcher) handleHealthRequest(ctx context.Context) {

0 commit comments

Comments
 (0)