Skip to content

Commit

Permalink
Add default config for command (#872)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurley authored Oct 10, 2024
1 parent b3be362 commit f3ec0d7
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 156 deletions.
2 changes: 1 addition & 1 deletion docs/features/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@ Proper security measures and validation ensure the integrity and
reliability of the feature management process.


[def]: #conflictingcombinations
[def]: #conflictingcombinations
59 changes: 55 additions & 4 deletions internal/command/command_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/nginx/agent/v3/internal/config"
"github.com/nginx/agent/v3/internal/grpc"
"github.com/nginx/agent/v3/internal/logger"
pkgConfig "github.com/nginx/agent/v3/pkg/config"
)

var _ bus.Plugin = (*CommandPlugin)(nil)
Expand All @@ -27,7 +28,7 @@ type (
UpdateDataPlaneHealth(ctx context.Context, instanceHealths []*mpi.InstanceHealth) error
SendDataPlaneResponse(ctx context.Context, response *mpi.DataPlaneResponse) error
CancelSubscription(ctx context.Context)
CheckConnection() bool
IsConnected() bool
CreateConnection(ctx context.Context, resource *mpi.Resource) (*mpi.CreateConnectionResponse, error)
}

Expand Down Expand Up @@ -85,7 +86,7 @@ func (cp *CommandPlugin) Process(ctx context.Context, msg *bus.Message) {

func (cp *CommandPlugin) processResourceUpdate(ctx context.Context, msg *bus.Message) {
if resource, ok := msg.Data.(*mpi.Resource); ok {
if !cp.commandService.CheckConnection() {
if !cp.commandService.IsConnected() && cp.config.IsFeatureEnabled(pkgConfig.FeatureConnection) {
cp.createConnection(ctx, resource)
} else {
statusErr := cp.commandService.UpdateDataPlaneStatus(ctx, resource)
Expand Down Expand Up @@ -150,12 +151,62 @@ func (cp *CommandPlugin) monitorSubscribeChannel(ctx context.Context) {

switch message.GetRequest().(type) {
case *mpi.ManagementPlaneRequest_ConfigUploadRequest:
cp.messagePipe.Process(newCtx, &bus.Message{Topic: bus.ConfigUploadRequestTopic, Data: message})
cp.handleConfigUploadRequest(newCtx, message)
case *mpi.ManagementPlaneRequest_ConfigApplyRequest:
cp.messagePipe.Process(newCtx, &bus.Message{Topic: bus.ConfigApplyRequestTopic, Data: message})
cp.handleConfigApplyRequest(newCtx, message)
default:
slog.DebugContext(newCtx, "Management plane request not implemented yet")
}
}
}
}

func (cp *CommandPlugin) handleConfigApplyRequest(newCtx context.Context, message *mpi.ManagementPlaneRequest) {
if cp.config.IsFeatureEnabled(pkgConfig.FeatureConfiguration) {
cp.messagePipe.Process(newCtx, &bus.Message{Topic: bus.ConfigApplyRequestTopic, Data: message})
} else {
slog.WarnContext(
newCtx,
"Configuration feature disabled. Unable to process config apply request",
"request", message,
)

err := cp.commandService.SendDataPlaneResponse(newCtx, &mpi.DataPlaneResponse{
MessageMeta: message.GetMessageMeta(),
CommandResponse: &mpi.CommandResponse{
Status: mpi.CommandResponse_COMMAND_STATUS_FAILURE,
Message: "Config apply failed",
Error: "Configuration feature is disabled",
},
InstanceId: message.GetConfigUploadRequest().GetOverview().GetConfigVersion().GetInstanceId(),
})
if err != nil {
slog.ErrorContext(newCtx, "Unable to send data plane response", "error", err)
}
}
}

func (cp *CommandPlugin) handleConfigUploadRequest(newCtx context.Context, message *mpi.ManagementPlaneRequest) {
if cp.config.IsFeatureEnabled(pkgConfig.FeatureConfiguration) {
cp.messagePipe.Process(newCtx, &bus.Message{Topic: bus.ConfigUploadRequestTopic, Data: message})
} else {
slog.WarnContext(
newCtx,
"Configuration feature disabled. Unable to process config upload request",
"request", message,
)

err := cp.commandService.SendDataPlaneResponse(newCtx, &mpi.DataPlaneResponse{
MessageMeta: message.GetMessageMeta(),
CommandResponse: &mpi.CommandResponse{
Status: mpi.CommandResponse_COMMAND_STATUS_FAILURE,
Message: "Config upload failed",
Error: "Configuration feature is disabled",
},
InstanceId: message.GetConfigUploadRequest().GetOverview().GetConfigVersion().GetInstanceId(),
})
if err != nil {
slog.ErrorContext(newCtx, "Unable to send data plane response", "error", err)
}
}
}
6 changes: 3 additions & 3 deletions internal/command/command_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ func TestCommandPlugin_Process(t *testing.T) {
defer commandPlugin.Close(ctx)

// Check CreateConnection
fakeCommandService.CheckConnectionReturnsOnCall(0, false)
fakeCommandService.IsConnectedReturnsOnCall(0, false)

// Check UpdateDataPlaneStatus
fakeCommandService.CheckConnectionReturnsOnCall(1, true)
fakeCommandService.CheckConnectionReturnsOnCall(2, true)
fakeCommandService.IsConnectedReturnsOnCall(1, true)
fakeCommandService.IsConnectedReturnsOnCall(2, true)

commandPlugin.commandService = fakeCommandService

Expand Down
2 changes: 1 addition & 1 deletion internal/command/command_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewCommandService(
return commandService
}

func (cs *CommandService) CheckConnection() bool {
func (cs *CommandService) IsConnected() bool {
return cs.isConnected.Load()
}

Expand Down
130 changes: 65 additions & 65 deletions internal/command/commandfakes/fake_command_service.go

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

Loading

0 comments on commit f3ec0d7

Please sign in to comment.