Repository navigation
fix(ecs): [SVLS-9885] convert comma-separated globalTags for Agent DD_TAGS #758
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 all commits
a6db383
3f27934
e103951
bdae3bc
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 |
|---|---|---|
|
|
@@ -6,13 +6,14 @@ | |
| * Copyright 2020-2026 Datadog, Inc. | ||
| */ | ||
|
|
||
| import { Tags } from "aws-cdk-lib"; | ||
| import { Annotations, Fn, Tags, Token } from "aws-cdk-lib"; | ||
| import * as ecs from "aws-cdk-lib/aws-ecs"; | ||
| import * as iam from "aws-cdk-lib/aws-iam"; | ||
| import * as secretsmanager from "aws-cdk-lib/aws-secretsmanager"; | ||
| import * as ssm from "aws-cdk-lib/aws-ssm"; | ||
| import { Construct } from "constructs"; | ||
| import log from "loglevel"; | ||
| import { AgentTagsCommaWarningId, GlobalTagsWhitespaceWarningId } from "./constants"; | ||
| import { DatadogECSBaseProps } from "./interfaces"; | ||
| import * as versionJson from "../../version.json"; | ||
| import { invalidSiteError, siteList, TagKeys } from "../constants"; | ||
|
|
@@ -111,3 +112,53 @@ export function addCdkConstructVersionTag(task: ecs.TaskDefinition): void { | |
| includeResourceTypes: ["AWS::ECS::TaskDefinition"], | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true for a JSON array of tags, which the Agent also accepts in DD_TAGS. | ||
| */ | ||
| function isJsonArrayTags(tags: string): boolean { | ||
| return tags.trimStart().startsWith("["); | ||
| } | ||
|
|
||
| /** | ||
| * Converts comma-separated global tags to the whitespace-separated format that the Agent reads from DD_TAGS. | ||
| * A JSON array of tags is passed through unchanged. For a token, CloudFormation does the conversion at deploy time. | ||
| */ | ||
| export function toAgentTags(globalTags: string): string { | ||
| if (isJsonArrayTags(globalTags)) { | ||
| return globalTags; | ||
| } | ||
| if (Token.isUnresolved(globalTags)) { | ||
| return Fn.join(" ", Fn.split(",", globalTags)); | ||
| } | ||
| return globalTags.replace(/,/g, " "); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, addressed in bdae3bc |
||
| } | ||
|
|
||
| /** | ||
| * Warns about global tags and DD_TAGS values that the construct can't format correctly for the Agent. | ||
| */ | ||
| export function addGlobalTagsWarnings(scope: Construct, props: DatadogECSBaseProps): void { | ||
| const globalTags = props.globalTags; | ||
| if (globalTags === undefined) { | ||
| const agentTags = props.environmentVariables?.DD_TAGS; | ||
| if (agentTags !== undefined && agentTags.includes(",") && !isJsonArrayTags(agentTags)) { | ||
| Annotations.of(scope).addWarningV2( | ||
| AgentTagsCommaWarningId, | ||
| "DD_TAGS in `environmentVariables` contains commas, but the Datadog Agent separates tags with whitespace. Set `globalTags` instead, which accepts comma-separated tags.", | ||
| ); | ||
| } | ||
| return; | ||
| } | ||
| if (isJsonArrayTags(globalTags)) { | ||
| return; | ||
| } | ||
| for (const tag of globalTags.split(",")) { | ||
| if (/\S\s+\S/.test(tag)) { | ||
| Annotations.of(scope).addWarningV2( | ||
| GlobalTagsWhitespaceWarningId, | ||
| `\`globalTags\` item "${tag.trim()}" contains whitespace. Separate tags with commas. Tag values can't contain whitespace.`, | ||
| ); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
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.
nit: the Agent also accepts
DD_TAGSas a JSON array, soglobalTags: '["env:prod","team:pay"]'used to give correct Agent tags. After this change it becomes["env:prod" "team:pay"]and splits into broken tags. Probably rare, but could values starting with[pass through unchanged, or get a warning?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.
Good catch, I didn't realize this was also an accepted format. Updated to pass through unchanged in bdae3bc