-
Notifications
You must be signed in to change notification settings - Fork 94
Allow multiple management planes #1124
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
Open
aphralG
wants to merge
2
commits into
add-auxiliary-command-server-proto
Choose a base branch
from
add-auxiliary-command-server-to-agent-config
base: add-auxiliary-command-server-proto
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,17 @@ var _ bus.Plugin = (*CommandPlugin)(nil) | |
|
||
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6@v6.8.1 -generate | ||
//counterfeiter:generate . commandService | ||
type ServerType int | ||
|
||
const ( | ||
Command ServerType = iota | ||
Auxiliary | ||
) | ||
|
||
var serverType = map[ServerType]string{ | ||
Command: "command", | ||
Auxiliary: "auxiliary", | ||
} | ||
|
||
type ( | ||
commandService interface { | ||
|
@@ -38,37 +49,45 @@ type ( | |
} | ||
|
||
CommandPlugin struct { | ||
messagePipe bus.MessagePipeInterface | ||
config *config.Config | ||
subscribeCancel context.CancelFunc | ||
conn grpc.GrpcConnectionInterface | ||
commandService commandService | ||
subscribeChannel chan *mpi.ManagementPlaneRequest | ||
subscribeMutex sync.Mutex | ||
messagePipe bus.MessagePipeInterface | ||
config *config.Config | ||
subscribeCancel context.CancelFunc | ||
conn grpc.GrpcConnectionInterface | ||
commandService commandService | ||
subscribeChannel chan *mpi.ManagementPlaneRequest | ||
commandServerType ServerType | ||
subscribeMutex sync.Mutex | ||
} | ||
) | ||
|
||
func NewCommandPlugin(agentConfig *config.Config, grpcConnection grpc.GrpcConnectionInterface) *CommandPlugin { | ||
func NewCommandPlugin(agentConfig *config.Config, grpcConnection grpc.GrpcConnectionInterface, | ||
commandServerType ServerType, | ||
) *CommandPlugin { | ||
return &CommandPlugin{ | ||
config: agentConfig, | ||
conn: grpcConnection, | ||
subscribeChannel: make(chan *mpi.ManagementPlaneRequest), | ||
config: agentConfig, | ||
conn: grpcConnection, | ||
subscribeChannel: make(chan *mpi.ManagementPlaneRequest), | ||
commandServerType: commandServerType, | ||
} | ||
} | ||
|
||
func (cp *CommandPlugin) Init(ctx context.Context, messagePipe bus.MessagePipeInterface) error { | ||
slog.DebugContext(ctx, "Starting command plugin") | ||
newCtx := context.WithValue( | ||
ctx, | ||
logger.ServerTypeContextKey, slog.Any(logger.ServerTypeKey, cp.commandServerType.String()), | ||
) | ||
slog.DebugContext(newCtx, "Starting command plugin", "command_server_type", cp.commandServerType.String()) | ||
|
||
cp.messagePipe = messagePipe | ||
cp.commandService = NewCommandService(cp.conn.CommandServiceClient(), cp.config, cp.subscribeChannel) | ||
|
||
go cp.monitorSubscribeChannel(ctx) | ||
go cp.monitorSubscribeChannel(newCtx) | ||
|
||
return nil | ||
} | ||
|
||
func (cp *CommandPlugin) Close(ctx context.Context) error { | ||
slog.InfoContext(ctx, "Closing command plugin") | ||
slog.InfoContext(ctx, "Closing command plugin", "command_server_type", cp.commandServerType.String()) | ||
|
||
cp.subscribeMutex.Lock() | ||
if cp.subscribeCancel != nil { | ||
|
@@ -81,32 +100,40 @@ func (cp *CommandPlugin) Close(ctx context.Context) error { | |
|
||
func (cp *CommandPlugin) Info() *bus.Info { | ||
return &bus.Info{ | ||
Name: "command", | ||
Name: cp.commandServerType.String(), | ||
} | ||
} | ||
|
||
func (cp *CommandPlugin) Process(ctx context.Context, msg *bus.Message) { | ||
switch msg.Topic { | ||
case bus.ConnectionResetTopic: | ||
cp.processConnectionReset(ctx, msg) | ||
case bus.ResourceUpdateTopic: | ||
cp.processResourceUpdate(ctx, msg) | ||
case bus.InstanceHealthTopic: | ||
cp.processInstanceHealth(ctx, msg) | ||
case bus.DataPlaneHealthResponseTopic: | ||
cp.processDataPlaneHealth(ctx, msg) | ||
case bus.DataPlaneResponseTopic: | ||
cp.processDataPlaneResponse(ctx, msg) | ||
default: | ||
slog.DebugContext(ctx, "Command plugin received unknown topic", "topic", msg.Topic) | ||
slog.DebugContext(ctx, "Processing command", "command_server_type", logger.ServerType(ctx)) | ||
|
||
if logger.ServerType(ctx) == cp.commandServerType.String() || logger.ServerType(ctx) == "" { | ||
switch msg.Topic { | ||
case bus.ConnectionResetTopic: | ||
cp.processConnectionReset(ctx, msg) | ||
case bus.ResourceUpdateTopic: | ||
cp.processResourceUpdate(ctx, msg) | ||
case bus.InstanceHealthTopic: | ||
cp.processInstanceHealth(ctx, msg) | ||
case bus.DataPlaneHealthResponseTopic: | ||
cp.processDataPlaneHealth(ctx, msg) | ||
case bus.DataPlaneResponseTopic: | ||
cp.processDataPlaneResponse(ctx, msg) | ||
default: | ||
slog.DebugContext(ctx, "Command plugin received unknown topic", "topic", msg.Topic) | ||
} | ||
} | ||
} | ||
|
||
func (cp *CommandPlugin) processResourceUpdate(ctx context.Context, msg *bus.Message) { | ||
slog.DebugContext(ctx, "Command plugin received resource update message") | ||
if resource, ok := msg.Data.(*mpi.Resource); ok { | ||
if !cp.commandService.IsConnected() { | ||
cp.createConnection(ctx, resource) | ||
newCtx := context.WithValue( | ||
ctx, | ||
logger.ServerTypeContextKey, slog.Any(logger.ServerTypeKey, cp.commandServerType.String()), | ||
) | ||
cp.createConnection(newCtx, resource) | ||
} else { | ||
statusErr := cp.commandService.UpdateDataPlaneStatus(ctx, resource) | ||
if statusErr != nil { | ||
|
@@ -145,13 +172,14 @@ func (cp *CommandPlugin) processDataPlaneHealth(ctx context.Context, msg *bus.Me | |
correlationID := logger.CorrelationID(ctx) | ||
if err != nil { | ||
slog.ErrorContext(ctx, "Unable to update data plane health", "error", err) | ||
cp.messagePipe.Process(ctx, &bus.Message{ | ||
|
||
cp.processDataPlaneResponse(ctx, &bus.Message{ | ||
Topic: bus.DataPlaneResponseTopic, | ||
Data: cp.createDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_FAILURE, | ||
"Failed to send the health status update", err.Error()), | ||
}) | ||
} | ||
cp.messagePipe.Process(ctx, &bus.Message{ | ||
cp.processDataPlaneResponse(ctx, &bus.Message{ | ||
Topic: bus.DataPlaneResponseTopic, | ||
Data: cp.createDataPlaneResponse(correlationID, mpi.CommandResponse_COMMAND_STATUS_OK, | ||
"Successfully sent health status update", ""), | ||
|
@@ -164,7 +192,8 @@ func (cp *CommandPlugin) processInstanceHealth(ctx context.Context, msg *bus.Mes | |
if instances, ok := msg.Data.([]*mpi.InstanceHealth); ok { | ||
err := cp.commandService.UpdateDataPlaneHealth(ctx, instances) | ||
if err != nil { | ||
slog.ErrorContext(ctx, "Unable to update data plane health", "error", err) | ||
slog.ErrorContext(ctx, "Unable to update data plane health", "error", err, | ||
"command_server_type", cp.commandServerType.String()) | ||
} | ||
} | ||
} | ||
|
@@ -208,6 +237,7 @@ func (cp *CommandPlugin) Subscriptions() []string { | |
} | ||
} | ||
|
||
// nolint: revive, cyclop | ||
func (cp *CommandPlugin) monitorSubscribeChannel(ctx context.Context) { | ||
for { | ||
select { | ||
|
@@ -226,12 +256,26 @@ func (cp *CommandPlugin) monitorSubscribeChannel(ctx context.Context) { | |
slog.InfoContext(ctx, "Received management plane config upload request") | ||
cp.handleConfigUploadRequest(newCtx, message) | ||
case *mpi.ManagementPlaneRequest_ConfigApplyRequest: | ||
if cp.commandServerType != Command { | ||
slog.WarnContext(newCtx, "Auxiliary command server can not perform config apply", | ||
"command_server_type", cp.commandServerType.String()) | ||
cp.handleInvalidRequest(newCtx, message) | ||
|
||
return | ||
} | ||
slog.InfoContext(ctx, "Received management plane config apply request") | ||
cp.handleConfigApplyRequest(newCtx, message) | ||
case *mpi.ManagementPlaneRequest_HealthRequest: | ||
slog.InfoContext(ctx, "Received management plane health request") | ||
cp.handleHealthRequest(newCtx) | ||
case *mpi.ManagementPlaneRequest_ActionRequest: | ||
if cp.commandServerType != Command { | ||
slog.WarnContext(newCtx, "Auxiliary command server can not perform api action", | ||
"command_server_type", cp.commandServerType.String()) | ||
cp.handleInvalidRequest(newCtx, message) | ||
|
||
return | ||
} | ||
slog.InfoContext(ctx, "Received management plane action request") | ||
cp.handleAPIActionRequest(newCtx, message) | ||
default: | ||
|
@@ -320,6 +364,21 @@ func (cp *CommandPlugin) handleHealthRequest(newCtx context.Context) { | |
cp.messagePipe.Process(newCtx, &bus.Message{Topic: bus.DataPlaneHealthRequestTopic}) | ||
} | ||
|
||
func (cp *CommandPlugin) handleInvalidRequest(ctx context.Context, message *mpi.ManagementPlaneRequest) { | ||
err := cp.commandService.SendDataPlaneResponse(ctx, &mpi.DataPlaneResponse{ | ||
MessageMeta: message.GetMessageMeta(), | ||
CommandResponse: &mpi.CommandResponse{ | ||
Status: mpi.CommandResponse_COMMAND_STATUS_FAILURE, | ||
Message: "Can not perform write action as auxiliary command server", | ||
Error: "request not allowed", | ||
}, | ||
InstanceId: message.GetActionRequest().GetInstanceId(), | ||
}) | ||
if err != nil { | ||
slog.ErrorContext(ctx, "Unable to send data plane response", "error", err) | ||
} | ||
} | ||
|
||
func (cp *CommandPlugin) createDataPlaneResponse(correlationID string, status mpi.CommandResponse_CommandStatus, | ||
message, err string, | ||
) *mpi.DataPlaneResponse { | ||
|
@@ -336,3 +395,7 @@ func (cp *CommandPlugin) createDataPlaneResponse(correlationID string, status mp | |
}, | ||
} | ||
} | ||
|
||
func (s ServerType) String() string { | ||
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. Can you move this to the top near the ServerType declaration? |
||
return serverType[s] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Same as comment above