Skip to content

Commit

Permalink
Review service container aliases and hostname (#1850)
Browse files Browse the repository at this point in the history
Use specific hostnames for service containers adding alias
networks when connecting the container to the docker
network.
  • Loading branch information
mrodm authored May 28, 2024
1 parent 81dfea3 commit 3ee5da5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
10 changes: 6 additions & 4 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,16 @@ func InspectNetwork(network string) ([]NetworkDescription, error) {

// ConnectToNetwork function connects the container to the selected Docker network.
func ConnectToNetwork(containerID, network string) error {
return ConnectToNetworkWithAlias(containerID, network, "")
return ConnectToNetworkWithAlias(containerID, network, []string{})
}

// ConnectToNetworkWithAlias function connects the container to the selected Docker network.
func ConnectToNetworkWithAlias(containerID, network, alias string) error {
func ConnectToNetworkWithAlias(containerID, network string, aliases []string) error {
args := []string{"network", "connect", network, containerID}
if alias != "" {
args = append(args, "--alias", alias)
if len(aliases) > 0 {
for _, alias := range aliases {
args = append(args, "--alias", alias)
}
}
cmd := exec.Command("docker", args...)
errOutput := new(bytes.Buffer)
Expand Down
17 changes: 14 additions & 3 deletions internal/servicedeployer/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,26 +136,37 @@ func (d *DockerComposeServiceDeployer) SetUp(ctx context.Context, svcInfo Servic
return nil, fmt.Errorf("service is unhealthy: %w", err)
}

// Added a specific alias when connecting the service to the network.
// - There could be container names too long that could not be resolved by the local DNS
// - Not used serviceName directly as alias container, since there could be packages defining
// kibana or elasticsearch services and those DNS names are already present in the Elastic stack.
// This is mainly applicable when the Elastic Agent of the stack is used for testing.
// - Keep the same alias for both implementations for consistency
aliasContainer := fmt.Sprintf("svc-%s", serviceName)
if d.runTearDown || d.runTestsOnly {
logger.Debug("Skipping connect container to network (non setup steps)")
} else {
aliases := []string{
aliasContainer,
}
if d.deployIndependentAgent {
// Connect service network with agent network
err = docker.ConnectToNetworkWithAlias(p.ContainerName(serviceName), svcInfo.AgentNetworkName, svcInfo.Name)
err = docker.ConnectToNetworkWithAlias(p.ContainerName(serviceName), svcInfo.AgentNetworkName, aliases)

if err != nil {
return nil, fmt.Errorf("can't attach service container to the agent network: %w", err)
}
} else {
// Connect service network with stack network (for the purpose of metrics collection)
err = docker.ConnectToNetwork(p.ContainerName(serviceName), stack.Network(d.profile))
err = docker.ConnectToNetworkWithAlias(p.ContainerName(serviceName), stack.Network(d.profile), aliases)
if err != nil {
return nil, fmt.Errorf("can't attach service container to the stack network: %w", err)
}
}
}

// Build service container name
svcInfo.Hostname = p.ContainerName(serviceName)
svcInfo.Hostname = aliasContainer

logger.Debugf("adding service container %s internal ports to context", p.ContainerName(serviceName))
serviceComposeConfig, err := p.Config(ctx, compose.CommandOptions{
Expand Down
3 changes: 1 addition & 2 deletions internal/testrunner/runners/system/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1962,8 +1962,7 @@ func filterAgents(allAgents []kibana.Agent, svcInfo servicedeployer.ServiceInfo)
continue // For some reason Kibana doesn't always return a valid policy revision (eventually it will be present and valid)
}

hasServicePrefix := strings.HasPrefix(agent.LocalMetadata.Host.Name, svcInfo.Agent.Host.NamePrefix)
if svcInfo.Agent.Host.NamePrefix != "" && !hasServicePrefix {
if svcInfo.Agent.Host.NamePrefix != "" && !strings.HasPrefix(agent.LocalMetadata.Host.Name, svcInfo.Agent.Host.NamePrefix) {
continue
}
filtered = append(filtered, agent)
Expand Down
22 changes: 10 additions & 12 deletions scripts/test-system-test-flags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

set -euxo pipefail

DEFAULT_AGENT_CONTAINER_NAME="elastic-package-service-[0-9]{5}-docker-custom-agent"

cleanup() {
local r=$?
local container_id=""
Expand Down Expand Up @@ -270,6 +268,14 @@ run_tests_for_package() {
}


# Set variables depending on whether or not independent Elastic Agents are running
DEFAULT_AGENT_CONTAINER_NAME="elastic-package-service-[0-9]{5}-docker-custom-agent"
service_deployer_type="docker"
service_prefix='elastic-package-service-[0-9]{5}'
if [[ "${ELASTIC_PACKAGE_TEST_ENABLE_INDEPENDENT_AGENT:-"false"}" == "true" ]]; then
service_deployer_type="agent"
fi

# to be set the specific value in run_tests_for_package , required to be global
# so cleanup function could delete the container if is still running
SERVICE_CONTAINER_NAME=""
Expand All @@ -285,18 +291,10 @@ elastic-package stack status

FOLDER_PATH="${HOME}/.elastic-package/profiles/default/stack/state"

# Check also if independent Elastic Agents are running too
# depending on the environment variable
service_deployer_type="docker"
service_preffix='elastic-package-service-[0-9]{5}'
if [[ "${ELASTIC_PACKAGE_TEST_ENABLE_INDEPENDENT_AGENT:-"false"}" == "true" ]]; then
service_deployer_type="agent"
fi

# docker service deployer
if ! run_tests_for_package \
"test/packages/parallel/nginx" \
"${service_preffix}-nginx" \
"${service_prefix}-nginx" \
"data_stream/access/_dev/test/system/test-default-config.yml" \
"no variant" \
"${service_deployer_type}" ; then
Expand All @@ -306,7 +304,7 @@ fi

if ! run_tests_for_package \
"test/packages/parallel/sql_input" \
"${service_preffix}-sql_input" \
"${service_prefix}-sql_input" \
"_dev/test/system/test-default-config.yml" \
"mysql_8_0_13" \
"${service_deployer_type}" ; then
Expand Down

0 comments on commit 3ee5da5

Please sign in to comment.