Skip to content

Commit 4ae7a6a

Browse files
committed
simplify code by centralizing logic
1 parent a2efd83 commit 4ae7a6a

1 file changed

Lines changed: 14 additions & 22 deletions

File tree

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

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

255-
if (cGroupInfo.getContainerId() != null
256-
&& shouldReplaceContainerId(
257-
containerInfo.getContainerId(),
258-
containerInfo.getPodId(),
259-
cGroupInfo.getContainerId(),
260-
cGroupInfo.getPodId())) {
261-
containerInfo.setContainerId(cGroupInfo.getContainerId());
262-
}
255+
replaceContainerIdIfBetter(containerInfo, cGroupInfo);
263256
}
264257

265258
return containerInfo;
@@ -271,24 +264,23 @@ && shouldReplaceContainerId(
271264
* <p>Some environments expose both detailed cgroup-v1 controller paths and a cgroup-v2 membership
272265
* path (0::...) where the tail can be task-level and less specific.
273266
*/
274-
private static boolean shouldReplaceContainerId(
275-
@Nullable String currentContainerId,
276-
@Nullable String currentPodId,
277-
String candidateContainerId,
278-
@Nullable String candidatePodId) {
279-
if (currentContainerId == null) {
280-
return true;
281-
}
282-
283-
if (currentContainerId.matches(CONTAINER_REGEX) && candidateContainerId.matches(TASK_REGEX)) {
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) {
284278
// Do not replace a pod-scoped container id with a task-scoped id from a less specific path.
285279
// This protects hybrid cgroup output where trailing 0:: lines contain only task ids.
286-
if (currentPodId != null && candidatePodId == null) {
287-
return false;
288-
}
280+
return;
289281
}
290282

291-
return true;
283+
currentContainerInfo.setContainerId(candidateContainerId);
292284
}
293285

294286
static CGroupInfo parseLine(final String line) throws ParseException {

0 commit comments

Comments
 (0)