Skip to content

Commit 9d2e35b

Browse files
committed
fix: internally track/cache watched namespaces
Collecting the namespace metadata so that later we can use it for easy querying. Will be useful for the namespaced annotated import.
1 parent d9c5411 commit 9d2e35b

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-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

0 commit comments

Comments
 (0)