Skip to content

Commit 2f273c4

Browse files
committed
clean up and check if manifest needs rollback
1 parent 1b3590a commit 2f273c4

File tree

10 files changed

+63
-178
lines changed

10 files changed

+63
-178
lines changed

internal/command/command_plugin.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,14 @@ func (cp *CommandPlugin) processConnectionReset(ctx context.Context, msg *bus.Me
232232
slog.DebugContext(ctx, "Command plugin received connection reset message")
233233

234234
if newConnection, ok := msg.Data.(grpc.GrpcConnectionInterface); ok {
235-
slog.InfoContext(ctx, "Resetting Subscribe")
235+
slog.DebugContext(ctx, "Canceling Subscribe after connection reset")
236236
ctxWithMetadata := cp.config.NewContextWithLabels(ctx)
237237
cp.subscribeMutex.Lock()
238238
defer cp.subscribeMutex.Unlock()
239239

240240
if cp.subscribeCancel != nil {
241241
cp.subscribeCancel()
242-
slog.InfoContext(ctxWithMetadata, "Successfully Reset Subscribe")
242+
slog.DebugContext(ctxWithMetadata, "Successfully canceled subscribe after connection reset")
243243
}
244244

245245
connectionErr := cp.conn.Close(ctx)
@@ -254,6 +254,7 @@ func (cp *CommandPlugin) processConnectionReset(ctx context.Context, msg *bus.Me
254254
return
255255
}
256256

257+
slog.DebugContext(ctxWithMetadata, "Starting new subscribe after connection reset")
257258
subscribeCtx, cp.subscribeCancel = context.WithCancel(ctxWithMetadata)
258259
go cp.commandService.Subscribe(subscribeCtx)
259260

internal/file/file_manager_service.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type (
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

@@ -68,7 +68,7 @@ type (
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 {
@@ -86,7 +86,7 @@ type (
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

129132
func (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
188193
func (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

435448
func (fms *FileManagerService) manifestFile() (map[string]*model.ManifestFile, map[string]*mpi.File, error) {

internal/file/file_operator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ func (fo *FileOperator) ReadChunk(
149149
return chunk, err
150150
}
151151

152-
func (fo *FileOperator) WriteManifestFile(updatedFiles map[string]*model.ManifestFile, manifestDir,
152+
func (fo *FileOperator) WriteManifestFile(ctx context.Context, updatedFiles map[string]*model.ManifestFile, manifestDir,
153153
manifestPath string,
154154
) (writeError error) {
155-
slog.Info("Writing manifest file")
155+
slog.DebugContext(ctx, "Writing manifest file", "updated_files", updatedFiles)
156156
manifestJSON, err := json.MarshalIndent(updatedFiles, "", " ")
157157
if err != nil {
158158
return fmt.Errorf("unable to marshal manifest file json: %w", err)

internal/file/file_plugin.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ func (fp *FilePlugin) handleConnectionReset(ctx context.Context, msg *bus.Messag
149149
fp.conn = newConnection
150150

151151
reconnect = fp.fileManagerService.IsConnected()
152-
fp.fileManagerService.ResetClient(fp.conn.FileServiceClient())
153-
// fp.fileManagerService = NewFileManagerService(fp.conn.FileServiceClient(), fp.config, fp.manifestLock)
152+
fp.fileManagerService.ResetClient(ctx, fp.conn.FileServiceClient())
154153
fp.fileManagerService.SetIsConnected(reconnect)
155154

156155
slog.DebugContext(ctx, "File manager service client reset successfully")

internal/file/file_service_operator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ func NewFileServiceOperator(agentConfig *config.Config, fileServiceClient mpi.Fi
5656
}
5757
}
5858

59-
func (fso *FileServiceOperator) UpdateClient(fileServiceClient mpi.FileServiceClient) {
59+
func (fso *FileServiceOperator) UpdateClient(ctx context.Context, fileServiceClient mpi.FileServiceClient) {
6060
fso.fileServiceClient = fileServiceClient
61+
slog.DebugContext(ctx, "File service operator updated client")
6162
}
6263

6364
func (fso *FileServiceOperator) SetIsConnected(isConnected bool) {

internal/file/filefakes/fake_file_manager_service_interface.go

Lines changed: 12 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/file/filefakes/fake_file_operator.go

Lines changed: 14 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/grpc/grpc.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,6 @@ func DialOptions(agentConfig *config.Config, commandConfig *config.Command, reso
231231
opts = addPerRPCCredentials(commandConfig, resourceID, opts)
232232
}
233233

234-
// opts = append(opts,
235-
// grpc.WithTransportCredentials(defaultCredentials),
236-
// )
237-
238234
return opts
239235
}
240236

0 commit comments

Comments
 (0)