11import { V1OwnerReference , V1Pod , V1Container , V1ContainerStatus } from '@kubernetes/client-node' ;
22import { IWorkload , ILocalWorkloadLocator } from '../transmitter/types' ;
33import { currentClusterName } from './cluster' ;
4- import { KubeObjectMetadata } from './types' ;
4+ import { IKubeObjectMetadata } from './types' ;
55import { getSupportedWorkload , getWorkloadReader } from './workload-reader' ;
66import logger = require( '../common/logger' ) ;
77
@@ -10,7 +10,7 @@ const loopingThreshold = 20;
1010// Constructs the workload metadata based on a variety of k8s properties.
1111// https://www.notion.so/snyk/Kubernetes-workload-fields-we-should-collect-c60c8f0395f241978282173f4c133a34
1212export function buildImageMetadata (
13- workloadMeta : KubeObjectMetadata ,
13+ workloadMeta : IKubeObjectMetadata ,
1414 containerStatuses : V1ContainerStatus [ ] ,
1515 ) : IWorkload [ ] {
1616 const { kind, objectMeta, specMeta, revision, podSpec } = workloadMeta ;
@@ -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,23 +40,24 @@ 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
4955async function findParentWorkload (
5056 ownerRefs : V1OwnerReference [ ] | undefined ,
5157 namespace : string ,
52- ) : Promise < KubeObjectMetadata | undefined > {
58+ ) : Promise < IKubeObjectMetadata | undefined > {
5359 let ownerReferences = ownerRefs ;
54- let parentMetadata : KubeObjectMetadata | undefined ;
60+ let parentMetadata : IKubeObjectMetadata | undefined ;
5561
5662 for ( let i = 0 ; i < loopingThreshold ; i ++ ) {
5763 // We are interested only in a subset of all workloads.
@@ -76,7 +82,7 @@ async function findParentWorkload(
7682 return undefined ;
7783}
7884
79- export function buildWorkloadMetadata ( kubernetesMetadata : KubeObjectMetadata ) : ILocalWorkloadLocator {
85+ export function buildWorkloadMetadata ( kubernetesMetadata : IKubeObjectMetadata ) : ILocalWorkloadLocator {
8086 if ( ! kubernetesMetadata . objectMeta ||
8187 kubernetesMetadata . objectMeta . namespace === undefined ||
8288 kubernetesMetadata . objectMeta . name === undefined ) {
@@ -126,7 +132,7 @@ export async function buildMetadataForWorkload(pod: V1Pod): Promise<IWorkload[]
126132 ) ;
127133 }
128134
129- const podOwner : KubeObjectMetadata | undefined = await findParentWorkload (
135+ const podOwner : IKubeObjectMetadata | undefined = await findParentWorkload (
130136 pod . metadata . ownerReferences , pod . metadata . namespace ) ;
131137
132138 return podOwner === undefined
0 commit comments