Skip to content

Commit

Permalink
fix(detector-container): properly detect container ID when using Podm…
Browse files Browse the repository at this point in the history
…an (#2448)

Co-authored-by: David Luna <[email protected]>
  • Loading branch information
sjinks and david-luna authored Oct 14, 2024
1 parent d11efb3 commit ad560df
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class ContainerDetector implements DetectorSync {
readonly DEFAULT_CGROUP_V2_PATH = '/proc/self/mountinfo';
readonly UTF8_UNICODE = 'utf8';
readonly HOSTNAME = 'hostname';
readonly MARKING_PREFIX = 'containers';
readonly MARKING_PREFIX = ['containers', 'overlay-containers'];
readonly CRIO = 'crio-';
readonly CRI_CONTAINERD = 'cri-containerd-';
readonly DOCKER = 'docker-';
Expand Down Expand Up @@ -105,7 +105,7 @@ export class ContainerDetector implements DetectorSync {
const strArray = str?.split('/') ?? [];
for (let i = 0; i < strArray.length - 1; i++) {
if (
strArray[i] === this.MARKING_PREFIX &&
this.MARKING_PREFIX.includes(strArray[i]) &&
strArray[i + 1]?.length === this.CONTAINER_ID_LENGTH
) {
return strArray[i + 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const DEFAULT_CGROUP_V1_PATH = '/proc/self/cgroup';
export const DEFAULT_CGROUP_V2_PATH = '/proc/self/mountinfo';
export const UTF8_UNICODE = 'utf8';
export const HOSTNAME = 'hostname';
export const MARKING_PREFIX = 'containers';
export const MARKING_PREFIX = ['containers', 'overlay-containers'];
export const CRIO = 'crio-';
export const CRI_CONTAINERD = 'cri-containerd-';
export const DOCKER = 'docker-';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ describe('ContainerDetector', () => {
const correctCgroupV2Data = `containers/tmhdefghijklmnopqrstuvwxyzafgrefghiugkmnopqrstuvwxyzabcdefghijkl/hostname
fhkjdshgfhsdfjhdsfkjhfkdshkjhfd/host
sahfhfjkhjhfhjdhfjkdhfkjdhfjkhhdsjfhdfhjdhfkj/somethingelse`;
const correctCgroupV2PodmanData =
'4245 4237 0:94 /containers/overlay-containers/4e9dc37d00ebd2daea029d84bb37764ce12d746a6f3a33c5969cee15c4fc4418/userdata/hostname /etc/hostname rw - tmpfs tmpfs rw';

const wrongCgroupV2Data =
'bcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklm/wrongkeyword';
Expand Down Expand Up @@ -85,6 +87,22 @@ describe('ContainerDetector', () => {
});
});

it('should return a resource with container ID with a valid container ID present for v2 (Podman)', async () => {
readStub = sinon.stub(ContainerDetector, 'readFileAsync' as any);

readStub.onFirstCall().resolves('');
readStub.onSecondCall().resolves(correctCgroupV2PodmanData);

const resource = containerDetector.detect();
await resource.waitForAsyncAttributes?.();
sinon.assert.calledTwice(readStub);

assert.ok(resource);
assertContainerResource(resource, {
id: '4e9dc37d00ebd2daea029d84bb37764ce12d746a6f3a33c5969cee15c4fc4418',
});
});

it('should return a empty resource with failed hostname check for v2', async () => {
readStub = sinon.stub(ContainerDetector, 'readFileAsync' as any);

Expand Down

0 comments on commit ad560df

Please sign in to comment.