1
1
import { V1OwnerReference , V1Pod , V1Container , V1ContainerStatus } from '@kubernetes/client-node' ;
2
2
import { IWorkload , ILocalWorkloadLocator } from '../transmitter/types' ;
3
3
import { currentClusterName } from './cluster' ;
4
- import { KubeObjectMetadata } from './types' ;
4
+ import { IKubeObjectMetadata } from './types' ;
5
5
import { getSupportedWorkload , getWorkloadReader } from './workload-reader' ;
6
6
import logger = require( '../common/logger' ) ;
7
7
@@ -10,7 +10,7 @@ const loopingThreshold = 20;
10
10
// Constructs the workload metadata based on a variety of k8s properties.
11
11
// https://www.notion.so/snyk/Kubernetes-workload-fields-we-should-collect-c60c8f0395f241978282173f4c133a34
12
12
export function buildImageMetadata (
13
- workloadMeta : KubeObjectMetadata ,
13
+ workloadMeta : IKubeObjectMetadata ,
14
14
containerStatuses : V1ContainerStatus [ ] ,
15
15
) : IWorkload [ ] {
16
16
const { kind, objectMeta, specMeta, revision, podSpec } = workloadMeta ;
@@ -26,7 +26,12 @@ export function buildImageMetadata(
26
26
containerNameToStatus [ containerStatus . name ] = containerStatus ;
27
27
}
28
28
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 ( {
30
35
type : kind ,
31
36
name : name || 'unknown' ,
32
37
namespace,
@@ -35,23 +40,24 @@ export function buildImageMetadata(
35
40
uid,
36
41
specLabels : specMeta . labels || { } ,
37
42
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 ,
41
46
cluster : currentClusterName ,
42
47
revision,
43
48
podSpec,
44
- } as IWorkload ) ,
45
- ) ;
49
+ } as IWorkload ) ;
50
+ }
51
+
46
52
return images ;
47
53
}
48
54
49
55
async function findParentWorkload (
50
56
ownerRefs : V1OwnerReference [ ] | undefined ,
51
57
namespace : string ,
52
- ) : Promise < KubeObjectMetadata | undefined > {
58
+ ) : Promise < IKubeObjectMetadata | undefined > {
53
59
let ownerReferences = ownerRefs ;
54
- let parentMetadata : KubeObjectMetadata | undefined ;
60
+ let parentMetadata : IKubeObjectMetadata | undefined ;
55
61
56
62
for ( let i = 0 ; i < loopingThreshold ; i ++ ) {
57
63
// We are interested only in a subset of all workloads.
@@ -76,7 +82,7 @@ async function findParentWorkload(
76
82
return undefined ;
77
83
}
78
84
79
- export function buildWorkloadMetadata ( kubernetesMetadata : KubeObjectMetadata ) : ILocalWorkloadLocator {
85
+ export function buildWorkloadMetadata ( kubernetesMetadata : IKubeObjectMetadata ) : ILocalWorkloadLocator {
80
86
if ( ! kubernetesMetadata . objectMeta ||
81
87
kubernetesMetadata . objectMeta . namespace === undefined ||
82
88
kubernetesMetadata . objectMeta . name === undefined ) {
@@ -126,7 +132,7 @@ export async function buildMetadataForWorkload(pod: V1Pod): Promise<IWorkload[]
126
132
) ;
127
133
}
128
134
129
- const podOwner : KubeObjectMetadata | undefined = await findParentWorkload (
135
+ const podOwner : IKubeObjectMetadata | undefined = await findParentWorkload (
130
136
pod . metadata . ownerReferences , pod . metadata . namespace ) ;
131
137
132
138
return podOwner === undefined
0 commit comments