Skip to content

Commit ebc1db9

Browse files
[CONTP-1014] feat: Add UST docker labels to all container definitions (#48)
* feat: add ust docker labels * test: add ust docker label tests * chore: terraform fmt * feat: expose docker label config for datadog agent * chore: add docs and fix variable def * fix: update test assertion * chore: update example * chore: remove ust tagging for agent container * chore: terraform fmt * chore: remove ust env from cws container
1 parent c8e262a commit ebc1db9

File tree

13 files changed

+169
-22
lines changed

13 files changed

+169
-22
lines changed

examples/ecs_fargate/main.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ module "datadog_ecs_fargate_task" {
1313
# Configure Datadog
1414
dd_api_key = var.dd_api_key
1515
dd_site = var.dd_site
16-
dd_service = var.dd_service
1716
dd_tags = "team:cont-p, owner:container-monitoring"
1817
dd_essential = true
1918
dd_is_datadog_dependency_enabled = true
2019

20+
dd_service = var.dd_service
21+
dd_env = var.dd_env
22+
dd_version = var.dd_version
23+
2124
dd_environment = [
2225
{
2326
name = "DD_CUSTOM_FEATURE",

examples/ecs_fargate/variables.tf

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,19 @@ variable "dd_api_key_secret_arn" {
1616
}
1717

1818
variable "dd_service" {
19-
description = "Service name for resource filtering in Datadog"
19+
description = "The service name for resource filtering and UST tagging in Datadog"
20+
type = string
21+
default = null
22+
}
23+
24+
variable "dd_env" {
25+
description = "The environment for resource filtering and UST tagging in Datadog"
26+
type = string
27+
default = null
28+
}
29+
30+
variable "dd_version" {
31+
description = "The version for resource filtering and UST tagging in Datadog"
2032
type = string
2133
default = null
2234
}

modules/ecs_fargate/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ No modules.
245245
| <a name="input_dd_cluster_name"></a> [dd\_cluster\_name](#input\_dd\_cluster\_name) | Datadog cluster name | `string` | `null` | no |
246246
| <a name="input_dd_cpu"></a> [dd\_cpu](#input\_dd\_cpu) | Datadog Agent container CPU units | `number` | `null` | no |
247247
| <a name="input_dd_cws"></a> [dd\_cws](#input\_dd\_cws) | Configuration for Datadog Cloud Workload Security (CWS) | <pre>object({<br/> enabled = optional(bool, false)<br/> cpu = optional(number)<br/> memory_limit_mib = optional(number)<br/> })</pre> | <pre>{<br/> "enabled": false<br/>}</pre> | no |
248+
| <a name="input_dd_docker_labels"></a> [dd\_docker\_labels](#input\_dd\_docker\_labels) | Datadog Agent container docker labels | `map(string)` | `{}` | no |
248249
| <a name="input_dd_dogstatsd"></a> [dd\_dogstatsd](#input\_dd\_dogstatsd) | Configuration for Datadog DogStatsD | <pre>object({<br/> enabled = optional(bool, true)<br/> origin_detection_enabled = optional(bool, true)<br/> dogstatsd_cardinality = optional(string, "orchestrator")<br/> socket_enabled = optional(bool, true)<br/> })</pre> | <pre>{<br/> "dogstatsd_cardinality": "orchestrator",<br/> "enabled": true,<br/> "origin_detection_enabled": true,<br/> "socket_enabled": true<br/>}</pre> | no |
249250
| <a name="input_dd_env"></a> [dd\_env](#input\_dd\_env) | The task environment name. Used for tagging (UST) | `string` | `null` | no |
250251
| <a name="input_dd_environment"></a> [dd\_environment](#input\_dd\_environment) | Datadog Agent container environment variables. Highest precedence and overwrites other environment variables defined by the module. For example, `dd_environment = [ { name = 'DD_VAR', value = 'DD_VAL' } ]` | `list(map(string))` | <pre>[<br/> {}<br/>]</pre> | no |

modules/ecs_fargate/datadog.tf

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ locals {
119119
] : [],
120120
)
121121

122+
ust_docker_labels = merge(
123+
var.dd_env != null ? {
124+
"com.datadoghq.tags.env" = var.dd_env
125+
} : {},
126+
var.dd_service != null ? {
127+
"com.datadoghq.tags.service" = var.dd_service
128+
} : {},
129+
var.dd_version != null ? {
130+
"com.datadoghq.tags.version" = var.dd_version
131+
} : {},
132+
)
133+
122134
application_env_vars = concat(
123135
var.dd_apm.profiling != null ? [
124136
{
@@ -169,6 +181,11 @@ locals {
169181
local.ust_env_vars,
170182
local.application_env_vars,
171183
),
184+
# Merge UST docker labels with any existing docker labels.
185+
dockerLabels = merge(
186+
lookup(container, "dockerLabels", {}),
187+
local.ust_docker_labels,
188+
),
172189
# Append new volume mounts to any existing mountPoints.
173190
mountPoints = concat(
174191
lookup(container, "mountPoints", []),
@@ -288,20 +305,20 @@ locals {
288305
local.dynamic_env,
289306
local.origin_detection_vars,
290307
local.cws_vars,
291-
local.ust_env_vars,
292308
local.dd_environment,
293309
)
294310

295311
# Datadog Agent container definition
296312
dd_agent_container = [
297313
merge(
298314
{
299-
name = "datadog-agent"
300-
image = "${var.dd_registry}:${var.dd_image_version}"
301-
essential = var.dd_essential
302-
environment = local.dd_agent_env
303-
cpu = var.dd_cpu
304-
memory = var.dd_memory_limit_mib
315+
name = "datadog-agent"
316+
image = "${var.dd_registry}:${var.dd_image_version}"
317+
essential = var.dd_essential
318+
environment = local.dd_agent_env
319+
dockerLabels = var.dd_docker_labels
320+
cpu = var.dd_cpu
321+
memory = var.dd_memory_limit_mib
305322
secrets = var.dd_api_key_secret != null ? [
306323
{
307324
name = "DD_API_KEY"
@@ -340,11 +357,6 @@ locals {
340357

341358
dd_log_environment = var.dd_log_collection.fluentbit_config.environment != null ? var.dd_log_collection.fluentbit_config.environment : []
342359

343-
dd_log_agent_env = concat(
344-
local.ust_env_vars,
345-
local.dd_log_environment
346-
)
347-
348360
# Datadog log router container definition
349361
dd_log_container = local.is_fluentbit_supported ? [
350362
merge(
@@ -366,7 +378,8 @@ locals {
366378
memory_limit_mib = var.dd_log_collection.fluentbit_config.memory_limit_mib
367379
user = "0"
368380
mountPoints = var.dd_log_collection.fluentbit_config.mountPoints
369-
environment = local.dd_log_agent_env
381+
environment = local.dd_log_environment
382+
dockerLabels = var.dd_docker_labels
370383
portMappings = []
371384
systemControls = []
372385
volumesFrom = []
@@ -396,7 +409,7 @@ locals {
396409
entryPoint = []
397410
command = ["/cws-instrumentation", "setup", "--cws-volume-mount", "/cws-instrumentation-volume"]
398411
mountPoints = local.cws_mount
399-
environment = local.ust_env_vars
412+
dockerLabels = var.dd_docker_labels
400413
portMappings = []
401414
systemControls = []
402415
volumesFrom = []

modules/ecs_fargate/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ variable "dd_environment" {
9696
nullable = false
9797
}
9898

99+
variable "dd_docker_labels" {
100+
description = "Datadog Agent container docker labels"
101+
type = map(string)
102+
default = {}
103+
}
104+
99105
variable "dd_tags" {
100106
description = "Datadog Agent global tags (eg. `key1:value1, key2:value2`)"
101107
type = string

smoke_tests/ecs_fargate/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ output "role-parsing-with-path" {
3737
output "role-parsing-without-path" {
3838
value = module.dd_task_role_parsing_without_path
3939
}
40+
41+
output "ust-docker-labels" {
42+
value = module.dd_task_ust_docker_labels
43+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Unless explicitly stated otherwise all files in this repository are licensed
2+
# under the Apache License Version 2.0.
3+
# This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
# Copyright 2025-present Datadog, Inc.
5+
6+
################################################################################
7+
# Task Definition: UST Docker Labels Test
8+
################################################################################
9+
10+
module "dd_task_ust_docker_labels" {
11+
source = "../../modules/ecs_fargate"
12+
13+
# Configure Datadog with UST tags
14+
dd_api_key = var.dd_api_key
15+
dd_site = var.dd_site
16+
dd_service = "ust-test-service"
17+
dd_env = "ust-test-env"
18+
dd_version = "1.2.3"
19+
dd_tags = "team:test"
20+
dd_essential = true
21+
22+
dd_is_datadog_dependency_enabled = true
23+
24+
dd_log_collection = {
25+
enabled = true,
26+
}
27+
28+
dd_cws = {
29+
enabled = true,
30+
}
31+
32+
dd_docker_labels = {
33+
"com.datadoghq.tags.service" : "docker-agent-service",
34+
"com.datadoghq.tags.env" : "agent-dev",
35+
"com.datadoghq.tags.version" : "v1.2.3"
36+
}
37+
38+
# Configure Task Definition with multiple containers
39+
family = "${var.test_prefix}-ust-docker-labels"
40+
container_definitions = jsonencode([
41+
{
42+
name = "dummy-app",
43+
image = "nginx:latest",
44+
essential = true,
45+
},
46+
])
47+
48+
requires_compatibilities = ["FARGATE"]
49+
}

tests/all_dd_disabled_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func (s *ECSFargateSuite) TestAllDDDisabled() {
4242
expectedAgentEnvVars := map[string]string{
4343
"DD_API_KEY": "test-api-key",
4444
"DD_SITE": "datadoghq.com",
45-
"DD_SERVICE": "test-service",
4645
"DD_TAGS": "team:cont-p, owner:container-monitoring",
4746
"DD_DOGSTATSD_TAG_CARDINALITY": "orchestrator",
4847
"DD_ECS_TASK_COLLECTION_ENABLED": "true",

tests/all_dd_inputs_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func (s *ECSFargateSuite) TestAllDDInputs() {
5151
"DD_API_KEY": "test-api-key",
5252
"DD_SITE": "datadoghq.com",
5353
"ECS_FARGATE": "true",
54-
"DD_SERVICE": "test-service",
5554
"DD_RUNTIME_SECURITY_CONFIG_EBPFLESS_ENABLED": "true",
5655
"DD_INSTALL_INFO_TOOL": "terraform",
5756
// "DD_INSTALL_INFO_INSTALLER_VERSION": "0.0.0",
@@ -61,7 +60,6 @@ func (s *ECSFargateSuite) TestAllDDInputs() {
6160
expectedLogOptions := map[string]string{
6261
"apikey": "test-api-key",
6362
"provider": "ecs",
64-
"dd_service": "dd-test",
6563
"Host": "http-intake.logs.datadoghq.com",
6664
"TLS": "on",
6765
"dd_source": "dd-test",

tests/apm_dsd_tcp_udp_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ func (s *ECSFargateSuite) TestApmDsdTcpUdp() {
4242
expectedAgentEnvVars := map[string]string{
4343
"DD_API_KEY": "test-api-key",
4444
"DD_SITE": "datadoghq.com",
45-
"DD_SERVICE": "test-service",
4645
"DD_TAGS": "team:cont-p, owner:container-monitoring",
4746
"DD_DOGSTATSD_TAG_CARDINALITY": "orchestrator",
4847
"DD_ECS_TASK_COLLECTION_ENABLED": "true",

0 commit comments

Comments
 (0)