@@ -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
5145export 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