Skip to content
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

Upload nginx config #702

Merged
merged 9 commits into from
Jun 25, 2024
Merged
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
1 change: 1 addition & 0 deletions internal/bus/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ const (
InstanceHealthTopic = "instance-health"
ConfigUploadRequestTopic = "config-upload-request"
DataPlaneResponseTopic = "data-plane-response"
ConnectionCreatedTopic = "connection-created"
)
58 changes: 44 additions & 14 deletions internal/command/command_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ var _ bus.Plugin = (*CommandPlugin)(nil)

type (
commandService interface {
UpdateDataPlaneStatus(ctx context.Context, resource *mpi.Resource) error
UpdateDataPlaneStatus(ctx context.Context, resource *mpi.Resource) (*mpi.CreateConnectionResponse, error)
UpdateDataPlaneHealth(ctx context.Context, instanceHealths []*mpi.InstanceHealth) error
SendDataPlaneResponse(ctx context.Context, response *mpi.DataPlaneResponse) error
CancelSubscription(ctx context.Context)
}

Expand Down Expand Up @@ -67,32 +68,54 @@ func (cp *CommandPlugin) Info() *bus.Info {
}
}

// nolint: revive
func (cp *CommandPlugin) Process(ctx context.Context, msg *bus.Message) {
switch msg.Topic {
case bus.ResourceUpdateTopic:
if resource, ok := msg.Data.(*mpi.Resource); ok {
err := cp.commandService.UpdateDataPlaneStatus(ctx, resource)
if err != nil {
slog.ErrorContext(ctx, "Unable to update data plane status", "error", err)
}
}
cp.processResourceUpdate(ctx, msg)
case bus.InstanceHealthTopic:
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)
}
}
cp.processInstanceHealth(ctx, msg)
case bus.DataPlaneResponseTopic:
cp.processDataPlaneResponse(ctx, msg)
default:
slog.DebugContext(ctx, "Command plugin unknown topic", "topic", msg.Topic)
}
}

func (cp *CommandPlugin) processResourceUpdate(ctx context.Context, msg *bus.Message) {
if resource, ok := msg.Data.(*mpi.Resource); ok {
createConnectionResponse, err := cp.commandService.UpdateDataPlaneStatus(ctx, resource)
if err != nil {
slog.ErrorContext(ctx, "Unable to update data plane status", "error", err)
}
if createConnectionResponse != nil {
cp.messagePipe.Process(ctx, &bus.Message{Topic: bus.ConnectionCreatedTopic, Data: createConnectionResponse})
}
}
}

func (cp *CommandPlugin) processInstanceHealth(ctx context.Context, msg *bus.Message) {
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)
}
}
}

func (cp *CommandPlugin) processDataPlaneResponse(ctx context.Context, msg *bus.Message) {
if response, ok := msg.Data.(*mpi.DataPlaneResponse); ok {
err := cp.commandService.SendDataPlaneResponse(ctx, response)
if err != nil {
slog.ErrorContext(ctx, "Unable to send data plane response", "error", err)
}
}
}

func (cp *CommandPlugin) Subscriptions() []string {
return []string{
bus.ResourceUpdateTopic,
bus.InstanceHealthTopic,
bus.DataPlaneResponseTopic,
}
}

Expand All @@ -108,6 +131,13 @@ func (cp *CommandPlugin) monitorSubscribeChannel(ctx context.Context) {
slog.Any(logger.CorrelationIDKey, message.GetMessageMeta().GetCorrelationId()),
)
slog.DebugContext(newCtx, "Received management plane request", "request", message)

switch message.GetRequest().(type) {
case *mpi.ManagementPlaneRequest_ConfigUploadRequest:
cp.messagePipe.Process(ctx, &bus.Message{Topic: bus.ConfigUploadRequestTopic, Data: message})
default:
slog.DebugContext(newCtx, "Management plane request not implemented yet")
}
}
}
}
40 changes: 40 additions & 0 deletions internal/command/command_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

mpi "github.com/nginx/agent/v3/api/grpc/mpi/v1"
"github.com/nginx/agent/v3/internal/bus"
"github.com/nginx/agent/v3/internal/command/commandfakes"
"github.com/nginx/agent/v3/internal/grpc/grpcfakes"
Expand All @@ -38,6 +39,7 @@ func TestCommandPlugin_Subscriptions(t *testing.T) {
[]string{
bus.ResourceUpdateTopic,
bus.InstanceHealthTopic,
bus.DataPlaneResponseTopic,
},
subscriptions,
)
Expand Down Expand Up @@ -79,6 +81,44 @@ func TestCommandPlugin_Process(t *testing.T) {

commandPlugin.Process(ctx, &bus.Message{Topic: bus.InstanceHealthTopic, Data: protos.GetInstanceHealths()})
require.Equal(t, 1, fakeCommandService.UpdateDataPlaneHealthCallCount())

commandPlugin.Process(ctx, &bus.Message{Topic: bus.DataPlaneResponseTopic, Data: protos.OKDataPlaneResponse()})
require.Equal(t, 1, fakeCommandService.SendDataPlaneResponseCallCount())
}

func TestCommandPlugin_monitorSubscribeChannel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

messagePipe := bus.NewFakeMessagePipe()

commandPlugin := NewCommandPlugin(types.AgentConfig(), &grpcfakes.FakeGrpcConnectionInterface{})
err := commandPlugin.Init(ctx, messagePipe)
require.NoError(t, err)
defer commandPlugin.Close(ctx)

go commandPlugin.monitorSubscribeChannel(ctx)

commandPlugin.subscribeChannel <- &mpi.ManagementPlaneRequest{
Request: &mpi.ManagementPlaneRequest_ConfigUploadRequest{
ConfigUploadRequest: &mpi.ConfigUploadRequest{},
},
}

assert.Eventually(
t,
func() bool { return len(messagePipe.GetMessages()) == 1 },
2*time.Second,
10*time.Millisecond,
)

messages := messagePipe.GetMessages()
assert.Len(t, messages, 1)
assert.Equal(t, bus.ConfigUploadRequestTopic, messages[0].Topic)

request, ok := messages[0].Data.(*mpi.ManagementPlaneRequest)
assert.True(t, ok)
require.NotNil(t, request.GetConfigUploadRequest())
}

func TestMonitorSubscribeChannel(t *testing.T) {
Expand Down
Loading
Loading