-
Notifications
You must be signed in to change notification settings - Fork 94
Add read only file plugin #1128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: add-auxiliary-command-server-to-agent-config
Are you sure you want to change the base?
Changes from 7 commits
a6b6e52
8c422cc
8ef9c26
a8817ab
90cd7f9
8ef3c45
5773b42
4091223
e4423de
6d39669
0d86529
0a95a38
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ package file | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/nginx/agent/v3/pkg/files" | ||
|
@@ -32,12 +31,16 @@ type FilePlugin struct { | |
config *config.Config | ||
conn grpc.GrpcConnectionInterface | ||
fileManagerService fileManagerServiceInterface | ||
serverType model.ServerType | ||
} | ||
|
||
func NewFilePlugin(agentConfig *config.Config, grpcConnection grpc.GrpcConnectionInterface) *FilePlugin { | ||
func NewFilePlugin(agentConfig *config.Config, grpcConnection grpc.GrpcConnectionInterface, | ||
serverType model.ServerType, | ||
) *FilePlugin { | ||
return &FilePlugin{ | ||
config: agentConfig, | ||
conn: grpcConnection, | ||
config: agentConfig, | ||
conn: grpcConnection, | ||
serverType: serverType, | ||
} | ||
} | ||
|
||
|
@@ -61,31 +64,43 @@ func (fp *FilePlugin) Info() *bus.Info { | |
} | ||
} | ||
|
||
// nolint: cyclop, revive | ||
func (fp *FilePlugin) Process(ctx context.Context, msg *bus.Message) { | ||
switch msg.Topic { | ||
case bus.ConnectionResetTopic: | ||
fp.handleConnectionReset(ctx, msg) | ||
case bus.ConnectionCreatedTopic: | ||
slog.DebugContext(ctx, "File plugin received connection created message") | ||
fp.fileManagerService.SetIsConnected(true) | ||
case bus.NginxConfigUpdateTopic: | ||
fp.handleNginxConfigUpdate(ctx, msg) | ||
case bus.ConfigUploadRequestTopic: | ||
fp.handleConfigUploadRequest(ctx, msg) | ||
case bus.ConfigApplyRequestTopic: | ||
fp.handleConfigApplyRequest(ctx, msg) | ||
case bus.ConfigApplyCompleteTopic: | ||
fp.handleConfigApplyComplete(ctx, msg) | ||
case bus.ConfigApplySuccessfulTopic: | ||
fp.handleConfigApplySuccess(ctx, msg) | ||
case bus.ConfigApplyFailedTopic: | ||
fp.handleConfigApplyFailedRequest(ctx, msg) | ||
default: | ||
slog.DebugContext(ctx, "File plugin received unknown topic", "topic", msg.Topic) | ||
if logger.ServerType(ctx) == fp.serverType.String() || logger.ServerType(ctx) == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
switch msg.Topic { | ||
case bus.ConnectionResetTopic: | ||
fp.handleConnectionReset(ctx, msg) | ||
case bus.ConnectionCreatedTopic: | ||
slog.DebugContext(ctx, "File plugin received connection created message") | ||
fp.fileManagerService.SetIsConnected(true) | ||
case bus.NginxConfigUpdateTopic: | ||
fp.handleNginxConfigUpdate(ctx, msg) | ||
case bus.ConfigUploadRequestTopic: | ||
fp.handleConfigUploadRequest(ctx, msg) | ||
case bus.ConfigApplyRequestTopic: | ||
fp.handleConfigApplyRequest(ctx, msg) | ||
case bus.ConfigApplyCompleteTopic: | ||
fp.handleConfigApplyComplete(ctx, msg) | ||
case bus.ConfigApplySuccessfulTopic: | ||
fp.handleConfigApplySuccess(ctx, msg) | ||
case bus.ConfigApplyFailedTopic: | ||
fp.handleConfigApplyFailedRequest(ctx, msg) | ||
default: | ||
slog.DebugContext(ctx, "File plugin received unknown topic", "topic", msg.Topic) | ||
} | ||
} | ||
} | ||
|
||
func (fp *FilePlugin) Subscriptions() []string { | ||
if fp.serverType == model.Auxiliary { | ||
return []string{ | ||
bus.ConnectionResetTopic, | ||
bus.ConnectionCreatedTopic, | ||
bus.NginxConfigUpdateTopic, | ||
bus.ConfigUploadRequestTopic, | ||
} | ||
} | ||
|
||
return []string{ | ||
bus.ConnectionResetTopic, | ||
bus.ConnectionCreatedTopic, | ||
|
@@ -319,27 +334,10 @@ func (fp *FilePlugin) handleNginxConfigUpdate(ctx context.Context, msg *bus.Mess | |
return | ||
} | ||
|
||
updateError := fp.fileManagerService.UpdateCurrentFilesOnDisk( | ||
ctx, | ||
files.ConvertToMapOfFiles(nginxConfigContext.Files), | ||
true, | ||
) | ||
if updateError != nil { | ||
slog.ErrorContext(ctx, "Unable to update current files on disk", "error", updateError) | ||
} | ||
|
||
slog.InfoContext(ctx, "Updating overview after nginx config update") | ||
err := fp.fileManagerService.UpdateOverview(ctx, nginxConfigContext.InstanceID, nginxConfigContext.Files, 0) | ||
if err != nil { | ||
slog.ErrorContext( | ||
ctx, | ||
"Failed to update file overview", | ||
"instance_id", nginxConfigContext.InstanceID, | ||
"error", err, | ||
) | ||
} | ||
fp.fileManagerService.ConfigUpdate(ctx, nginxConfigContext) | ||
} | ||
|
||
// nolint: dupl | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To remove duplicate code between the file plugin and the read only file plugin, you could use embedding to take all the functionally from the read only file plugin and override whatever functions you need to change. This can be done by updateing the FilePlugin struct to the following:
|
||
func (fp *FilePlugin) handleConfigUploadRequest(ctx context.Context, msg *bus.Message) { | ||
slog.DebugContext(ctx, "File plugin received config upload request message") | ||
managementPlaneRequest, ok := msg.Data.(*mpi.ManagementPlaneRequest) | ||
|
@@ -357,36 +355,7 @@ func (fp *FilePlugin) handleConfigUploadRequest(ctx context.Context, msg *bus.Me | |
|
||
correlationID := logger.CorrelationID(ctx) | ||
|
||
var updatingFilesError error | ||
|
||
for _, file := range configUploadRequest.GetOverview().GetFiles() { | ||
err := fp.fileManagerService.UpdateFile( | ||
ctx, | ||
configUploadRequest.GetOverview().GetConfigVersion().GetInstanceId(), | ||
file, | ||
) | ||
if err != nil { | ||
slog.ErrorContext( | ||
ctx, | ||
"Failed to update file", | ||
"instance_id", configUploadRequest.GetOverview().GetConfigVersion().GetInstanceId(), | ||
"file_name", file.GetFileMeta().GetName(), | ||
"error", err, | ||
) | ||
|
||
response := fp.createDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_ERROR, | ||
fmt.Sprintf("Failed to update file %s", file.GetFileMeta().GetName()), | ||
configUploadRequest.GetOverview().GetConfigVersion().GetInstanceId(), | ||
err.Error(), | ||
) | ||
|
||
updatingFilesError = err | ||
|
||
fp.messagePipe.Process(ctx, &bus.Message{Topic: bus.DataPlaneResponseTopic, Data: response}) | ||
|
||
break | ||
} | ||
} | ||
updatingFilesError := fp.fileManagerService.ConfigUpload(ctx, configUploadRequest) | ||
|
||
response := &mpi.DataPlaneResponse{ | ||
MessageMeta: &mpi.MessageMeta{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the
Init
&Close
functions can you add the serverType to the context?