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
25 changes: 17 additions & 8 deletions internal/file/file_manager_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type (
Rollback(ctx context.Context, instanceID string) error
UpdateFile(ctx context.Context, instanceID string, fileToUpdate *mpi.File) error
ClearCache()
UpdateCurrentFilesOnDisk(updateFiles map[string]*mpi.File)
UpdateCurrentFilesOnDisk(ctx context.Context, updateFiles map[string]*mpi.File)
DetermineFileActions(currentFiles map[string]*mpi.File, modifiedFiles map[string]*model.FileCache) (
map[string]*model.FileCache, map[string][]byte, error)
IsConnected() bool
Expand Down Expand Up @@ -325,7 +325,7 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
}
fileOverviewFiles := files.ConvertToMapOfFiles(fileOverview.GetFiles())
// Update map of current files on disk
fms.UpdateCurrentFilesOnDisk(fileOverviewFiles)
fms.UpdateCurrentFilesOnDisk(ctx, fileOverviewFiles)
manifestFileErr := fms.UpdateManifestFile(fileOverviewFiles)
if manifestFileErr != nil {
return model.RollbackRequired, manifestFileErr
Expand Down Expand Up @@ -479,10 +479,14 @@ func (fms *FileManagerService) DetermineFileActions(currentFiles map[string]*mpi
fileDiff := make(map[string]*model.FileCache) // Files that have changed, key is file name
fileContents := make(map[string][]byte) // contents of the file, key is file name

manifestFiles, manifestFileErr := fms.manifestFile(currentFiles)
manifestFiles, manifestFileErr := fms.manifestFile()

if manifestFileErr != nil && manifestFiles == nil {
return nil, nil, manifestFileErr
if manifestFileErr != nil {
if errors.Is(manifestFileErr, os.ErrNotExist) {
manifestFiles = currentFiles
} else {
return nil, nil, manifestFileErr
}
}
// if file is in manifestFiles but not in modified files, file has been deleted
// copy contents, set file action
Expand Down Expand Up @@ -537,7 +541,7 @@ func (fms *FileManagerService) DetermineFileActions(currentFiles map[string]*mpi

// UpdateCurrentFilesOnDisk updates the FileManagerService currentFilesOnDisk slice which contains the files
// currently on disk
func (fms *FileManagerService) UpdateCurrentFilesOnDisk(currentFiles map[string]*mpi.File) {
func (fms *FileManagerService) UpdateCurrentFilesOnDisk(ctx context.Context, currentFiles map[string]*mpi.File) {
fms.filesMutex.Lock()
defer fms.filesMutex.Unlock()

Expand All @@ -546,6 +550,11 @@ func (fms *FileManagerService) UpdateCurrentFilesOnDisk(currentFiles map[string]
for _, currentFile := range currentFiles {
fms.currentFilesOnDisk[currentFile.GetFileMeta().GetName()] = currentFile
}

err := fms.UpdateManifestFile(currentFiles)
if err != nil {
slog.ErrorContext(ctx, "Failed to update manifest file", "error", err)
}
}

func (fms *FileManagerService) UpdateManifestFile(currentFiles map[string]*mpi.File) (err error) {
Expand Down Expand Up @@ -575,9 +584,9 @@ func (fms *FileManagerService) UpdateManifestFile(currentFiles map[string]*mpi.F
return nil
}

func (fms *FileManagerService) manifestFile(currentFiles map[string]*mpi.File) (map[string]*mpi.File, error) {
func (fms *FileManagerService) manifestFile() (map[string]*mpi.File, error) {
if _, err := os.Stat(manifestFilePath); err != nil {
return currentFiles, err // Return current files if manifest directory still doesn't exist
return nil, err
}

file, err := os.ReadFile(manifestFilePath)
Expand Down
17 changes: 14 additions & 3 deletions internal/file/file_manager_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestFileManagerService_ConfigApply_Update(t *testing.T) {
agentConfig := types.AgentConfig()
agentConfig.AllowedDirectories = []string{tempDir}
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig)
fileManagerService.UpdateCurrentFilesOnDisk(filesOnDisk)
fileManagerService.UpdateCurrentFilesOnDisk(ctx, filesOnDisk)

request := protos.CreateConfigApplyRequest(overview)

Expand Down Expand Up @@ -269,7 +269,7 @@ func TestFileManagerService_ConfigApply_Delete(t *testing.T) {
agentConfig := types.AgentConfig()
agentConfig.AllowedDirectories = []string{tempDir}
fileManagerService := NewFileManagerService(fakeFileServiceClient, agentConfig)
fileManagerService.UpdateCurrentFilesOnDisk(filesOnDisk)
fileManagerService.UpdateCurrentFilesOnDisk(ctx, filesOnDisk)

request := protos.CreateConfigApplyRequest(overview)

Expand All @@ -286,7 +286,18 @@ func TestFileManagerService_ConfigApply_Delete(t *testing.T) {
require.NoError(t, err)
assert.NoFileExists(t, tempFile.Name())
assert.Equal(t, fileManagerService.rollbackFileContents[tempFile.Name()], fileContent)
assert.Equal(t, fileManagerService.fileActions[tempFile.Name()].File, filesOnDisk[tempFile.Name()])
assert.Equal(t,
fileManagerService.fileActions[tempFile.Name()].File.GetFileMeta().GetName(),
filesOnDisk[tempFile.Name()].GetFileMeta().GetName(),
)
assert.Equal(t,
fileManagerService.fileActions[tempFile.Name()].File.GetFileMeta().GetHash(),
filesOnDisk[tempFile.Name()].GetFileMeta().GetHash(),
)
assert.Equal(t,
fileManagerService.fileActions[tempFile.Name()].File.GetFileMeta().GetSize(),
filesOnDisk[tempFile.Name()].GetFileMeta().GetSize(),
)
assert.Equal(t, model.OK, writeStatus)
}

Expand Down
5 changes: 4 additions & 1 deletion internal/file/file_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func (fp *FilePlugin) handleConfigApplyFailedRequest(ctx context.Context, msg *b
}

func (fp *FilePlugin) handleConfigApplyRequest(ctx context.Context, msg *bus.Message) {
slog.DebugContext(ctx, "File plugin received config apply request message")
var response *mpi.DataPlaneResponse
correlationID := logger.GetCorrelationID(ctx)

Expand All @@ -197,6 +198,7 @@ func (fp *FilePlugin) handleConfigApplyRequest(ctx context.Context, msg *bus.Mes

switch writeStatus {
case model.NoChange:
slog.DebugContext(ctx, "No changes required for config apply request")
dpResponse := fp.createDataPlaneResponse(
correlationID,
mpi.CommandResponse_COMMAND_STATUS_OK,
Expand Down Expand Up @@ -280,6 +282,7 @@ func (fp *FilePlugin) handleConfigApplyRequest(ctx context.Context, msg *bus.Mes

return
case model.OK:
slog.DebugContext(ctx, "Changes required for config apply request")
// Send WriteConfigSuccessfulTopic with Correlation and Instance ID for use by resource plugin
data := &model.ConfigApplyMessage{
CorrelationID: correlationID,
Expand All @@ -298,7 +301,7 @@ func (fp *FilePlugin) handleNginxConfigUpdate(ctx context.Context, msg *bus.Mess
return
}

fp.fileManagerService.UpdateCurrentFilesOnDisk(files.ConvertToMapOfFiles(nginxConfigContext.Files))
fp.fileManagerService.UpdateCurrentFilesOnDisk(ctx, files.ConvertToMapOfFiles(nginxConfigContext.Files))

err := fp.fileManagerService.UpdateOverview(ctx, nginxConfigContext.InstanceID, nginxConfigContext.Files, 0)
if err != nil {
Expand Down
22 changes: 12 additions & 10 deletions internal/file/filefakes/fake_file_manager_service_interface.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/watcher/watcher_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (*Watcher) Subscriptions() []string {
}

func (w *Watcher) handleConfigApplyRequest(ctx context.Context, msg *bus.Message) {
slog.Info("Watcher plugin received ConfigApplyRequest event")
slog.DebugContext(ctx, "Watcher plugin received ConfigApplyRequest event")
managementPlaneRequest, ok := msg.Data.(*mpi.ManagementPlaneRequest)
if !ok {
slog.ErrorContext(ctx, "Unable to cast message payload to *mpi.ManagementPlaneRequest",
Expand Down