Skip to content

Commit 0fa06a7

Browse files
author
Amir Moualem
authored
Merge pull request #303 from snyk/fix/things
Fix/things
2 parents 3b96a7d + 22f9c98 commit 0fa06a7

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import * as SourceMapSupport from 'source-map-support';
2+
3+
import * as state from './state';
24
import logger = require('./common/logger');
35
import { currentClusterName } from './supervisor/cluster';
46
import { beginWatchingWorkloads } from './supervisor/watchers';
57

68
process.on('uncaughtException', (err) => {
9+
if (state.shutdownInProgress) {
10+
return;
11+
}
12+
713
try {
814
logger.error({err}, 'UNCAUGHT EXCEPTION!');
915
} catch (ignore) {
@@ -13,8 +19,18 @@ process.on('uncaughtException', (err) => {
1319
}
1420
});
1521

16-
process.on('unhandledRejection', (reason, promise) => {
17-
logger.error({reason, promise}, 'unhandled rejection');
22+
process.on('unhandledRejection', (reason) => {
23+
if (state.shutdownInProgress) {
24+
return;
25+
}
26+
27+
try {
28+
logger.error({reason}, 'UNHANDLED REJECTION!');
29+
} catch (ignore) {
30+
console.log('UNHANDLED REJECTION!', reason);
31+
} finally {
32+
process.exit(1);
33+
}
1834
});
1935

2036
function monitor(): void {

src/state.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const workloadsLruCacheOptions = {
2121
};
2222

2323
const state = {
24+
shutdownInProgress: false,
2425
imagesAlreadyScanned: new lruCache<string, string>(imagesLruCacheOptions),
2526
workloadsAlreadyScanned: new lruCache<string, string>(workloadsLruCacheOptions),
2627
};

src/supervisor/watchers/handlers/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,17 @@ export function setupInformer(namespace: string, workloadKind: WorkloadKind) {
9595
const workloadMetadata = workloadWatchMetadata[workloadKind];
9696
const namespacedEndpoint = workloadMetadata.endpoint.replace('{namespace}', namespace);
9797

98-
const informer = makeInformer<KubernetesObject>(kubeConfig, namespacedEndpoint,
99-
workloadMetadata.listFactory(namespace));
98+
const listMethod = workloadMetadata.listFactory(namespace);
99+
const loggedListMethod = async () => {
100+
try {
101+
return await listMethod();
102+
} catch (err) {
103+
logger.error({err, namespace, workloadKind}, 'error while listing entities on namespace');
104+
throw err;
105+
}
106+
};
107+
108+
const informer = makeInformer<KubernetesObject>(kubeConfig, namespacedEndpoint, loggedListMethod);
100109

101110
for (const informerVerb of Object.keys(workloadMetadata.handlers)) {
102111
informer.on(informerVerb, async (watchedWorkload) => {

src/supervisor/watchers/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { makeInformer, ADD } from '@kubernetes/client-node';
22
import { V1Namespace } from '@kubernetes/client-node';
3+
34
import config = require('../../common/config');
45
import logger = require('../../common/logger');
56
import { WorkloadKind } from '../types';
@@ -51,7 +52,18 @@ export function isKubernetesInternalNamespace(namespace: string): boolean {
5152
}
5253

5354
function setupWatchesForCluster(): void {
54-
const informer = makeInformer(kubeConfig, '/api/v1/namespaces', () => k8sApi.coreClient.listNamespace());
55+
const informer = makeInformer(
56+
kubeConfig,
57+
'/api/v1/namespaces',
58+
async () => {
59+
try {
60+
return await k8sApi.coreClient.listNamespace();
61+
} catch (err) {
62+
logger.error({err}, 'error while listing namespaces');
63+
throw err;
64+
}
65+
},
66+
);
5567

5668
informer.on(ADD, (namespace: V1Namespace) => {
5769
try {

test/system/kind.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,10 @@ tap.test('Kubernetes-Monitor with KinD', async (t) => {
9797
// additional asserts?
9898
t.ok(nock.isDone(), 'all outgoing calls were made');
9999

100+
// instruct the Monitor to ignore errors from this point
101+
// eslint-disable-next-line @typescript-eslint/no-var-requires
102+
const kubernetesMonitorState = require('../../src/state');
103+
kubernetesMonitorState.shutdownInProgress = true;
104+
100105
// TODO cleanup the images we saved to /var/tmp?
101106
});

0 commit comments

Comments
 (0)