Skip to content

Commit c716a4c

Browse files
authored
fix container ID parsing for cgroup v1 in EKS-Fargate (#11110)
fix container ID parsing for cgroup v1 in EKS-Fargate simplify code by centralizing logic Co-authored-by: raphael.vandon <raphael.vandon@datadoghq.com>
1 parent c3d2ee3 commit c716a4c

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

utils/container-utils/src/main/java/datadog/common/container/ContainerInfo.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,37 @@ public static ContainerInfo parse(final String cgroupsContent) throws ParseExcep
252252
containerInfo.setPodId(cGroupInfo.getPodId());
253253
}
254254

255-
if (cGroupInfo.getContainerId() != null) {
256-
containerInfo.setContainerId(cGroupInfo.getContainerId());
257-
}
255+
replaceContainerIdIfBetter(containerInfo, cGroupInfo);
258256
}
259257

260258
return containerInfo;
261259
}
262260

261+
/**
262+
* Keeps the best available container id candidate across cgroup lines.
263+
*
264+
* <p>Some environments expose both detailed cgroup-v1 controller paths and a cgroup-v2 membership
265+
* path (0::...) where the tail can be task-level and less specific.
266+
*/
267+
private static void replaceContainerIdIfBetter(
268+
ContainerInfo currentContainerInfo, CGroupInfo candidate) {
269+
String candidateContainerId = candidate.getContainerId();
270+
if (candidateContainerId == null) return;
271+
272+
String currentContainerId = currentContainerInfo.getContainerId();
273+
if (currentContainerId != null
274+
&& currentContainerId.matches(CONTAINER_REGEX)
275+
&& candidateContainerId.matches(TASK_REGEX)
276+
&& currentContainerInfo.getPodId() != null
277+
&& candidate.getPodId() == null) {
278+
// Do not replace a pod-scoped container id with a task-scoped id from a less specific path.
279+
// This protects hybrid cgroup output where trailing 0:: lines contain only task ids.
280+
return;
281+
}
282+
283+
currentContainerInfo.setContainerId(candidateContainerId);
284+
}
285+
263286
static CGroupInfo parseLine(final String line) throws ParseException {
264287
final Matcher matcher = LINE_PATTERN.matcher(line);
265288

utils/container-utils/src/test/groovy/datadog/common/container/ContainerInfoTest.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ class ContainerInfoTest extends DDSpecification {
166166
2:memory:/ecs/55091c13-b8cf-4801-b527-f4601742204d/432624d2150b349fe35ba397284dea788c2bf66b885d14dfc1569b01890ca7da
167167
1:name=systemd:/ecs/34dc0b5e626f2c5c4c5170e34b10e765-1234567890"""
168168

169+
// EKS Fargate cgroup with trailing cgroup v2 membership entry (0::...)
170+
"cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc" | "defa568d-ff14-43d9-9a63-9e39ee9b39b4" | 13 | """12:misc:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
171+
11:cpuset:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
172+
10:perf_event:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
173+
9:blkio:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
174+
8:net_cls,net_prio:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
175+
7:memory:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
176+
6:cpu,cpuacct:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
177+
5:pids:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
178+
4:devices:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
179+
3:hugetlb:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
180+
2:freezer:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
181+
1:name=systemd:/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393/kubepods/burstable/poddefa568d-ff14-43d9-9a63-9e39ee9b39b4/cf1241bbf80ea91eebdd28bf719057380997ca4b0cea16869393b905fb6d52bc
182+
0::/ecs/545b896a072744d186c7fb09a45ec172/545b896a072744d186c7fb09a45ec172-3057940393"""
183+
169184
// PCF file
170185
"6f265890-5165-7fab-6b52-18d1" | null | 12 | """12:rdma:/
171186
11:net_cls,net_prio:/garden/6f265890-5165-7fab-6b52-18d1

0 commit comments

Comments
 (0)