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

Add support for Agent connection to mpi regardless of NGINX Instance status #916

Merged
merged 9 commits into from
Nov 8, 2024
Merged
4 changes: 1 addition & 3 deletions internal/command/command_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,8 @@ func (cs *CommandService) CreateConnection(
resource *mpi.Resource,
) (*mpi.CreateConnectionResponse, error) {
correlationID := logger.GetCorrelationID(ctx)

// Only send a resource update message if instances other than the agent instance are found
if len(resource.GetInstances()) <= 1 {
return nil, errors.New("waiting for data plane instances to be found before sending create connection request")
slog.InfoContext(ctx, "No Data Plane Instance found")
}

request := &mpi.CreateConnectionRequest{
Expand Down
17 changes: 17 additions & 0 deletions internal/command/command_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,23 @@ func TestCommandService_UpdateDataPlaneStatusSubscribeError(t *testing.T) {
}
}

func TestCommandService_CreateConnection(t *testing.T) {
ctx := context.Background()
commandServiceClient := &v1fakes.FakeCommandServiceClient{}

commandService := NewCommandService(
ctx,
commandServiceClient,
types.AgentConfig(),
make(chan *mpi.ManagementPlaneRequest),
)

// connection created when no nginx instance found
resource := protos.GetHostResource()
_, err := commandService.CreateConnection(ctx, resource)
require.NoError(t, err)
}

func TestCommandService_UpdateDataPlaneHealth(t *testing.T) {
ctx := context.Background()
commandServiceClient := &v1fakes.FakeCommandServiceClient{}
Expand Down
34 changes: 34 additions & 0 deletions test/docker/nginxless-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

set -euxo pipefail

handle_term()
{
echo "received TERM signal"
echo "stopping nginx-agent ..."
kill -TERM "${agent_pid}" 2>/dev/null
}

trap 'handle_term' TERM

cat /etc/nginx-agent/nginx-agent.conf;

# start nginx-agent, pass args
echo "starting nginx-agent ..."
nginx-agent "$@" &

agent_pid=$!

if [ $? != 0 ]; then
echo "couldn't start the agent, please check the log file"
exit 1
fi

wait_term()
{
wait ${agent_pid}
}

wait_term

echo "nginx-agent process has stopped, exiting."
69 changes: 69 additions & 0 deletions test/helpers/test_containers_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,75 @@ func StartAgentlessContainer(
return container
}

func StartNginxLessContainer(
ctx context.Context,
tb testing.TB,
containerNetwork *testcontainers.DockerNetwork,
parameters *Parameters,
) testcontainers.Container {
tb.Helper()

packageName := Env(tb, "PACKAGE_NAME")
packageRepo := Env(tb, "PACKAGES_REPO")
baseImage := Env(tb, "BASE_IMAGE")
buildTarget := Env(tb, "BUILD_TARGET")
osRelease := Env(tb, "OS_RELEASE")
osVersion := Env(tb, "OS_VERSION")
dockerfilePath := Env(tb, "DOCKERFILE_PATH")
tag := Env(tb, "TAG")
imagePath := Env(tb, "IMAGE_PATH")
containerRegistry := Env(tb, "CONTAINER_NGINX_IMAGE_REGISTRY")

req := testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Context: "../../",
Dockerfile: dockerfilePath,
KeepImage: false,
PrintBuildLog: true,
BuildArgs: map[string]*string{
"PACKAGE_NAME": ToPtr(packageName),
"PACKAGES_REPO": ToPtr(packageRepo),
"BASE_IMAGE": ToPtr(baseImage),
"OS_RELEASE": ToPtr(osRelease),
"OS_VERSION": ToPtr(osVersion),
"ENTRY_POINT": ToPtr("./test/docker/nginxless-entrypoint.sh"),
"CONTAINER_NGINX_IMAGE_REGISTRY": ToPtr(containerRegistry),
"IMAGE_PATH": ToPtr(imagePath),
"TAG": ToPtr(tag),
},
BuildOptionsModifier: func(buildOptions *types.ImageBuildOptions) {
buildOptions.Target = buildTarget
},
},
ExposedPorts: []string{"9094/tcp"},
WaitingFor: wait.ForLog(parameters.LogMessage),
Networks: []string{
containerNetwork.Name,
},
NetworkAliases: map[string][]string{
containerNetwork.Name: {
"agent",
},
},
Files: []testcontainers.ContainerFile{
{
HostFilePath: parameters.NginxAgentConfigPath,
ContainerFilePath: "/etc/nginx-agent/nginx-agent.conf",
FileMode: configFilePermissions,
},
},
}

container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
})

require.NoError(tb, err)

return container
}

func StartMockManagementPlaneGrpcContainer(
ctx context.Context,
tb testing.TB,
Expand Down
Loading
Loading