Skip to content

Commit 31c1a01

Browse files
authored
Merge pull request #1050 from snyk/chore/add-more-debug-logs
RUN-2198 chore: add more logs for debug purpose
2 parents 1332b57 + a9be373 commit 31c1a01

File tree

4 files changed

+58
-11
lines changed

4 files changed

+58
-11
lines changed

src/common/process.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export function exec(
2828
const allArguments = processArgs.map((arg) => arg.body);
2929
return spawn(bin, allArguments, { env, capture: ['stdout', 'stderr'] }).catch(
3030
(error) => {
31-
const message = (error && error.stderr) || 'Unknown reason';
31+
const message =
32+
error?.stderr || error?.stdout || error?.message || 'Unknown reason';
3233
const loggableArguments = processArgs
3334
.filter((arg) => !arg.sanitise)
3435
.map((arg) => arg.body);

src/supervisor/metadata-extractor.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function buildImageMetadata(
3030
delete container.args;
3131
delete container.env;
3232
delete container.command;
33+
//! would container.envFrom also include sensitive data?
3334
containerNameToSpec[container.name] = container;
3435
}
3536

@@ -41,6 +42,17 @@ export function buildImageMetadata(
4142
const images: IWorkload[] = [];
4243
for (const containerStatus of containerStatuses) {
4344
if (!(containerStatus.name in containerNameToSpec)) {
45+
logger.debug(
46+
{
47+
workloadName: workloadMeta.objectMeta.name,
48+
workloadType: workloadMeta.kind,
49+
workloadNamespace: workloadMeta.objectMeta.namespace,
50+
containerName: containerStatus.name,
51+
image: containerStatus.image,
52+
imageId: containerStatus.imageID,
53+
},
54+
'container name is not in containerNameToSpec lists',
55+
);
4456
continue;
4557
}
4658
images.push({
@@ -143,6 +155,16 @@ export async function buildMetadataForWorkload(
143155
const isAssociatedWithParent = isPodAssociatedWithParent(pod);
144156

145157
if (!pod.metadata || pod.metadata.namespace === undefined || !pod.spec) {
158+
const logContext = {
159+
workloadName: pod.metadata?.name,
160+
workloadType: pod.kind,
161+
workloadNamespace: pod.metadata?.namespace,
162+
clusterName: pod.metadata?.clusterName,
163+
};
164+
logger.debug(
165+
logContext,
166+
'cannot build metadata for workload without pod information',
167+
);
146168
// Some required parameters are missing, we cannot process further
147169
return undefined;
148170
}

src/supervisor/watchers/handlers/pod.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,16 @@ export async function handleReadyPod(
3838
// ImageName may contain a tag. The image behind this tag can be mutated and can change over time.
3939
// We need to compare on ImageID which will reliably tell us if the image has changed.
4040
if (scanned === workload.imageId) {
41+
logger.debug(
42+
{ workloadToScan, imageId: workload.imageId },
43+
'image already scanned',
44+
);
4145
continue;
4246
}
47+
logger.debug(
48+
{ workloadToScan, imageId: workload.imageId },
49+
'image has not been scanned',
50+
);
4351
await setWorkloadImageAlreadyScanned(
4452
workload,
4553
workload.imageName,
@@ -59,16 +67,28 @@ export async function handleReadyPod(
5967
}
6068

6169
export function isPodReady(pod: V1Pod): boolean {
70+
const podStatus = pod.status !== undefined;
71+
const podStatusPhase = pod.status?.phase === PodPhase.Running;
72+
const podContainerStatuses = pod.status?.containerStatuses !== undefined;
73+
const containerReadyStatuses = pod.status?.containerStatuses?.some(
74+
(container) =>
75+
container.state !== undefined &&
76+
(container.state.running !== undefined ||
77+
container.state.waiting !== undefined),
78+
) as boolean;
79+
80+
const logContext = {
81+
podStatus,
82+
podStatusPhase,
83+
podContainerStatuses,
84+
containerReadyStatuses,
85+
};
86+
logger.debug(logContext, 'checking to see if pod is ready to process');
6287
return (
63-
pod.status !== undefined &&
64-
pod.status.phase === PodPhase.Running &&
65-
pod.status.containerStatuses !== undefined &&
66-
pod.status.containerStatuses.some(
67-
(container) =>
68-
container.state !== undefined &&
69-
(container.state.running !== undefined ||
70-
container.state.waiting !== undefined),
71-
)
88+
podStatus &&
89+
podStatusPhase &&
90+
podContainerStatuses &&
91+
containerReadyStatuses
7292
);
7393
}
7494

src/supervisor/workload-sanitization.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { KubernetesObject, V1ObjectMeta } from '@kubernetes/client-node';
2+
import { logger } from '../common/logger';
23

34
export function trimWorkloads<
45
T extends KubernetesObject & Partial<{ status: unknown; spec: unknown }>,
@@ -14,6 +15,7 @@ export function trimWorkloads<
1415
export function trimWorkload<
1516
T extends KubernetesObject & Partial<{ status: unknown; spec: unknown }>,
1617
>(workload: T): T & { metadata: V1ObjectMeta } {
18+
logger.debug(workload.metadata, 'workload metadata state before trimming');
1719
return {
1820
apiVersion: workload.apiVersion,
1921
kind: workload.kind,
@@ -24,7 +26,7 @@ export function trimWorkload<
2426
}
2527

2628
export function trimMetadata(metadata?: V1ObjectMeta): V1ObjectMeta {
27-
return {
29+
const trimmedMetadata = {
2830
name: metadata?.name,
2931
namespace: metadata?.namespace,
3032
annotations: metadata?.annotations,
@@ -34,4 +36,6 @@ export function trimMetadata(metadata?: V1ObjectMeta): V1ObjectMeta {
3436
resourceVersion: metadata?.resourceVersion,
3537
generation: metadata?.generation,
3638
};
39+
logger.debug(trimmedMetadata, 'workload metadata state after trimming');
40+
return trimmedMetadata;
3741
}

0 commit comments

Comments
 (0)