Skip to content

Commit e2dd31f

Browse files
authored
Merge pull request #822 from snyk/fix/track-watched-namespaces
fix: internally track/cache watched namespaces
2 parents ccd5a5c + 2ec0112 commit e2dd31f

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed

src/state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { V1Namespace } from '@kubernetes/client-node';
12
import * as LruCache from 'lru-cache';
23

34
import { config } from './common/config';
@@ -26,6 +27,7 @@ const state = {
2627
workloadsAlreadyScanned: new LruCache<string, string>(
2728
workloadsLruCacheOptions,
2829
),
30+
watchedNamespaces: {} as Record<string, V1Namespace>,
2931
};
3032

3133
export { state };

src/supervisor/watchers/index.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,18 @@ import {
1212
kubernetesInternalNamespaces,
1313
openshiftInternalNamespaces,
1414
} from './internal-namespaces';
15+
import { state } from '../../state';
1516

16-
/**
17-
* This map keeps track of all currently watched namespaces.
18-
* Prevents duplicate watches being created if the same namespace is deleted
19-
* and then re-created. Once a watch is set up once, it doesn't have to be
20-
* tracked anymore as the kubernetes-client Informer API handles this internally.
21-
*/
22-
const watchedNamespaces = new Set<string>();
17+
async function setupWatchesForNamespace(namespace: V1Namespace): Promise<void> {
18+
const namespaceName = extractNamespaceName(namespace);
2319

24-
async function setupWatchesForNamespace(namespace: string): Promise<void> {
25-
if (watchedNamespaces.has(namespace)) {
20+
if (state.watchedNamespaces[namespaceName] !== undefined) {
2621
logger.info({ namespace }, 'already set up namespace watch, skipping');
2722
return;
2823
}
24+
state.watchedNamespaces[namespaceName] = namespace;
2925

30-
logger.info({ namespace }, 'setting up namespace watch');
26+
logger.info({ namespace: namespaceName }, 'setting up namespace watch');
3127

3228
for (const workloadKind of Object.values(WorkloadKind)) {
3329
// Disable handling events for k8s Jobs for debug purposes
@@ -36,16 +32,14 @@ async function setupWatchesForNamespace(namespace: string): Promise<void> {
3632
}
3733

3834
try {
39-
await setupInformer(namespace, workloadKind);
35+
await setupInformer(namespaceName, workloadKind);
4036
} catch (error) {
4137
logger.warn(
4238
{ namespace, workloadKind },
4339
'could not setup workload watch, skipping',
4440
);
4541
}
4642
}
47-
48-
watchedNamespaces.add(namespace);
4943
}
5044

5145
export function extractNamespaceName(namespace: V1Namespace): string {
@@ -105,7 +99,7 @@ async function setupWatchesForCluster(): Promise<void> {
10599
return;
106100
}
107101

108-
await setupWatchesForNamespace(namespaceName);
102+
await setupWatchesForNamespace(namespace);
109103
} catch (err) {
110104
logger.error({ err, namespace }, 'error handling a namespace event');
111105
return;
@@ -121,7 +115,11 @@ export async function beginWatchingWorkloads(): Promise<void> {
121115
{ namespace: config.WATCH_NAMESPACE },
122116
'kubernetes-monitor restricted to specific namespace',
123117
);
124-
await setupWatchesForNamespace(config.WATCH_NAMESPACE);
118+
const namespaceResponse = await k8sApi.coreClient.readNamespace(
119+
config.WATCH_NAMESPACE,
120+
);
121+
const namespace = namespaceResponse.body;
122+
await setupWatchesForNamespace(namespace);
125123
return;
126124
}
127125

src/transmitter/payload.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
IDependencyGraphPayload,
1414
IWorkloadEventsPolicyPayload,
1515
} from './types';
16+
import { state } from '../state';
1617

1718
export function constructDepGraph(
1819
scannedImages: IScanResult[],
@@ -99,6 +100,8 @@ export function constructWorkloadMetadata(
99100
specLabels: workload.specLabels,
100101
annotations: workload.annotations,
101102
specAnnotations: workload.specAnnotations,
103+
namespaceAnnotations:
104+
state.watchedNamespaces[workload.namespace]?.metadata?.annotations,
102105
revision: workload.revision,
103106
podSpec: workload.podSpec,
104107
};

src/transmitter/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface IWorkloadMetadata {
2222
specLabels: StringMap | undefined;
2323
annotations: StringMap | undefined;
2424
specAnnotations: StringMap | undefined;
25+
namespaceAnnotations: StringMap | undefined;
2526
revision: number | undefined;
2627
podSpec: V1PodSpec;
2728
}

0 commit comments

Comments
 (0)