Skip to content

Commit 79689b0

Browse files
committed
fix: buildImageMetadata when containers miss from the spec
this commit gives up supporting sidecar containers injected dynamically by collecting metadata for images only if they appear in both the spec and the status
1 parent 46050fd commit 79689b0

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/kube-scanner/metadata-extractor.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ export function buildImageMetadata(
2626
containerNameToStatus[containerStatus.name] = containerStatus;
2727
}
2828

29-
const images = containerStatuses.map(({ name: containerName }) => ({
29+
const images: IWorkload[] = [];
30+
for (const containerStatus of containerStatuses) {
31+
if (!(containerStatus.name in containerNameToSpec)) {
32+
continue
33+
}
34+
images.push({
3035
type: kind,
3136
name: name || 'unknown',
3237
namespace,
@@ -35,14 +40,15 @@ export function buildImageMetadata(
3540
uid,
3641
specLabels: specMeta.labels || {},
3742
specAnnotations: specMeta.annotations || {},
38-
containerName,
39-
imageName: containerNameToSpec[containerName].image,
40-
imageId: containerNameToStatus[containerName].imageID,
43+
containerName: containerStatus.name,
44+
imageName: containerNameToSpec[containerStatus.name].image,
45+
imageId: containerNameToStatus[containerStatus.name].imageID,
4146
cluster: currentClusterName,
4247
revision,
4348
podSpec,
44-
} as IWorkload),
45-
);
49+
} as IWorkload);
50+
}
51+
4652
return images;
4753
}
4854

test/unit/metadata-extractor.test.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,26 @@ tap.test('buildImageMetadata', async (t) => {
7272
podSpec: deploymentObject.spec!.template.spec!,
7373
}
7474

75-
t.throws(() => metadataExtractor.buildImageMetadata(
75+
const imageMetadataResult = metadataExtractor.buildImageMetadata(
7676
deploymentWeirdWrapper,
7777
podObject.status!.containerStatuses!,
78-
), 'buildImageMetadata can\'t handle discrepancies between spec and status');
78+
);
79+
80+
t.ok(Array.isArray(imageMetadataResult), 'returns an array');
81+
t.equals(
82+
imageMetadataResult.length,
83+
1,
84+
'the size of the container status array that also appears in the spec',
85+
);
86+
t.equals(imageMetadataResult[0].type, 'Deployment', 'with the workload type of the parent');
87+
t.equals(
88+
imageMetadataResult[0].imageId,
89+
'docker-pullable://eu.gcr.io/cookie/hello-world@sha256:1ac413b2756364b7b856c64d557fdedb97a4ba44ca16fc656e08881650848fe2',
90+
'the image ID of the first container'
91+
);
92+
t.equals(
93+
imageMetadataResult[0].imageName,
94+
'eu.gcr.io/cookie/hello-world:1.20191125.132107-4664980',
95+
'the image name of the first container'
96+
);
7997
});

0 commit comments

Comments
 (0)