-
Notifications
You must be signed in to change notification settings - Fork 11
[CONTP-1014] feat: Add UST docker labels to all container definitions #48
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
Changes from 7 commits
1646017
c996652
3e319c3
28d5d98
54f4a30
55ed512
1419f73
6ee0a3b
c3e0bf7
7c5af11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -119,6 +119,18 @@ locals { | |
| ] : [], | ||
| ) | ||
|
|
||
| ust_docker_labels = merge( | ||
| var.dd_env != null ? { | ||
| "com.datadoghq.tags.env" = var.dd_env | ||
| } : {}, | ||
| var.dd_service != null ? { | ||
| "com.datadoghq.tags.service" = var.dd_service | ||
| } : {}, | ||
| var.dd_version != null ? { | ||
| "com.datadoghq.tags.version" = var.dd_version | ||
| } : {}, | ||
| ) | ||
|
|
||
| application_env_vars = concat( | ||
| var.dd_apm.profiling != null ? [ | ||
| { | ||
|
|
@@ -169,6 +181,12 @@ locals { | |
| local.ust_env_vars, | ||
| local.application_env_vars, | ||
| ), | ||
| # Merge UST docker labels with any existing docker labels. | ||
| dockerLabels = merge( | ||
| local.ust_docker_labels, | ||
| // Placing this after local.ust_docker_labels ensures user defined UST labels are not overwritten. | ||
| lookup(container, "dockerLabels", {}), | ||
| ), | ||
| # Append new volume mounts to any existing mountPoints. | ||
| mountPoints = concat( | ||
| lookup(container, "mountPoints", []), | ||
|
|
@@ -292,16 +310,22 @@ locals { | |
| local.dd_environment, | ||
| ) | ||
|
|
||
| dd_agent_docker_labels = merge( | ||
| local.ust_docker_labels, | ||
| var.dd_docker_labels, | ||
|
||
| ) | ||
|
|
||
| # Datadog Agent container definition | ||
| dd_agent_container = [ | ||
| merge( | ||
| { | ||
| name = "datadog-agent" | ||
| image = "${var.dd_registry}:${var.dd_image_version}" | ||
| essential = var.dd_essential | ||
| environment = local.dd_agent_env | ||
| cpu = var.dd_cpu | ||
| memory = var.dd_memory_limit_mib | ||
| name = "datadog-agent" | ||
| image = "${var.dd_registry}:${var.dd_image_version}" | ||
| essential = var.dd_essential | ||
| environment = local.dd_agent_env | ||
| dockerLabels = local.dd_agent_docker_labels | ||
| cpu = var.dd_cpu | ||
| memory = var.dd_memory_limit_mib | ||
| secrets = var.dd_api_key_secret != null ? [ | ||
| { | ||
| name = "DD_API_KEY" | ||
|
|
@@ -367,6 +391,7 @@ locals { | |
| user = "0" | ||
| mountPoints = var.dd_log_collection.fluentbit_config.mountPoints | ||
| environment = local.dd_log_agent_env | ||
| dockerLabels = local.dd_agent_docker_labels | ||
| portMappings = [] | ||
| systemControls = [] | ||
| volumesFrom = [] | ||
|
|
@@ -397,6 +422,7 @@ locals { | |
| command = ["/cws-instrumentation", "setup", "--cws-volume-mount", "/cws-instrumentation-volume"] | ||
| mountPoints = local.cws_mount | ||
| environment = local.ust_env_vars | ||
| dockerLabels = local.dd_agent_docker_labels | ||
| portMappings = [] | ||
| systemControls = [] | ||
| volumesFrom = [] | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Unless explicitly stated otherwise all files in this repository are licensed | ||
| # under the Apache License Version 2.0. | ||
| # This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| # Copyright 2025-present Datadog, Inc. | ||
|
|
||
| ################################################################################ | ||
| # Task Definition: UST Docker Labels Test | ||
| ################################################################################ | ||
|
|
||
| module "dd_task_ust_docker_labels" { | ||
| source = "../../modules/ecs_fargate" | ||
|
|
||
| # Configure Datadog with UST tags | ||
| dd_api_key = var.dd_api_key | ||
| dd_site = var.dd_site | ||
| dd_service = "ust-test-service" | ||
| dd_env = "ust-test-env" | ||
| dd_version = "1.2.3" | ||
| dd_tags = "team:test" | ||
| dd_essential = true | ||
|
|
||
| dd_is_datadog_dependency_enabled = true | ||
|
|
||
| dd_log_collection = { | ||
| enabled = true, | ||
| } | ||
|
|
||
| dd_cws = { | ||
| enabled = true, | ||
| } | ||
|
|
||
| dd_docker_labels = { | ||
| "com.datadoghq.tags.service" : "docker-agent-service", | ||
| } | ||
|
|
||
| # Configure Task Definition with multiple containers | ||
| family = "${var.test_prefix}-ust-docker-labels" | ||
| container_definitions = jsonencode([ | ||
| { | ||
| name = "dummy-app", | ||
| image = "nginx:latest", | ||
| essential = true, | ||
| }, | ||
| { | ||
| name = "app-overwritten-ust", | ||
| image = "nginx:latest", | ||
| essential = false, | ||
| dockerLabels = { | ||
| "com.datadoghq.tags.service" : "overwritten_name", | ||
| "custom.label" = "custom-value" | ||
| } | ||
| } | ||
| ]) | ||
|
|
||
| requires_compatibilities = ["FARGATE"] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2025-present Datadog, Inc. | ||
|
|
||
| package test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "log" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/service/ecs/types" | ||
| "github.com/gruntwork-io/terratest/modules/terraform" | ||
| ) | ||
|
|
||
| // TestUSTDockerLabels tests that UST docker labels are propagated to all container definitions | ||
| // when dd_service, dd_env, and dd_version are set | ||
| func (s *ECSFargateSuite) TestUSTDockerLabels() { | ||
| log.Println("TestUSTDockerLabels: Running test...") | ||
|
|
||
| // Retrieve the task output for the "ust-docker-labels" module | ||
| var containers []types.ContainerDefinition | ||
| task := terraform.OutputMap(s.T(), s.terraformOptions, "ust-docker-labels") | ||
| s.Equal(s.testPrefix+"-ust-docker-labels", task["family"], "Unexpected task family name") | ||
|
|
||
| err := json.Unmarshal([]byte(task["container_definitions"]), &containers) | ||
| s.NoError(err, "Failed to parse container definitions") | ||
| s.Equal(5, len(containers), "Expected 4 containers in the task definition (3 app containers + 1 agent)") | ||
|
|
||
| // Expected UST docker labels that should be present on all application containers | ||
| expectedUSTLabels := map[string]string{ | ||
| "com.datadoghq.tags.service": "ust-test-service", | ||
| "com.datadoghq.tags.env": "ust-test-env", | ||
| "com.datadoghq.tags.version": "1.2.3", | ||
| } | ||
|
|
||
| dummyApp, found := GetContainer(containers, "dummy-app") | ||
| s.True(found, "Container dummy-app not found in definitions") | ||
| AssertDockerLabels(s.T(), dummyApp, expectedUSTLabels) | ||
|
|
||
| // Expect UST docker labels to be present on all Datadog containers with | ||
| // overwritten labels when UST docker labels are specified. | ||
| datadogContainers := []string{"datadog-agent", "datadog-log-router", "cws-instrumentation-init"} | ||
| expectedUSTLabels["com.datadoghq.tags.service"] = "docker-agent-service" | ||
| for _, containerName := range datadogContainers { | ||
| container, found := GetContainer(containers, containerName) | ||
| s.True(found, "Container %s not found in definitions", containerName) | ||
| AssertDockerLabels(s.T(), container, expectedUSTLabels) | ||
| } | ||
|
|
||
| // Expect UST docker labels to be overwritten on application container if docker labels | ||
| // are specified in the container definition. | ||
| overwrittenLabels, found := GetContainer(containers, "app-overwritten-ust") | ||
| s.True(found, "Container app-overwritten-ust not found in definitions") | ||
| expectedUSTLabels["com.datadoghq.tags.service"] = "overwritten_name" | ||
| AssertDockerLabels(s.T(), overwrittenLabels, expectedUSTLabels) | ||
|
|
||
| } |
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.
Other application container configured options like environment variables, mount points, and container dependencies give more precedence to the values defined by the Datadog Terraform module.
I think we should be fine to maintain that level of precedence here and assume that if a user defined the UST values for env, service, and version then those values would take precedence as well.
terraform-aws-ecs-datadog/modules/ecs_fargate/variables.tf
Lines 117 to 133 in 1419f73
We can leave a bugfix note in the changelog for the next release to notify users that the UST tagging has been fixed.
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.
Understandable. I guess if they wanted unique UST values, then they shouldn't set the global env,service,and version. They should then manually set it per container definition.
I initially wanted to prioritize the user's docker labels over ours because the customer's problem description implied the need for different UST values.