Skip to content

Commit 86c7542

Browse files
committed
fix: ensure log messages are not dynamic so they're easier to search
1 parent d32f9c0 commit 86c7542

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

src/supervisor/watchers/handlers/index.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,9 @@ export async function setupNamespacedInformer(
105105
informer.on(ERROR, (err) => {
106106
// Types from client library insists that callback is of type KubernetesObject
107107
const code = (err as any).code || '';
108+
logContext.code = code;
108109
if (RETRYABLE_NETWORK_ERRORS.includes(code)) {
109-
logger.debug(
110-
logContext,
111-
`informer ${code} occurred, restarting informer`,
112-
);
110+
logger.debug(logContext, 'informer error occurred, restarting informer');
113111

114112
// Restart informer after 1sec
115113
setTimeout(async () => {
@@ -182,11 +180,9 @@ export async function setupClusterInformer(
182180
informer.on(ERROR, (err) => {
183181
// Types from client library insists that callback is of type KubernetesObject
184182
const code = (err as any).code || '';
183+
logContext.code = code;
185184
if (RETRYABLE_NETWORK_ERRORS.includes(code)) {
186-
logger.debug(
187-
logContext,
188-
`informer ${code} occurred, restarting informer`,
189-
);
185+
logger.debug(logContext, 'informer error occurred, restarting informer');
190186

191187
// Restart informer after 1sec
192188
setTimeout(async () => {

src/supervisor/watchers/handlers/namespace.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { PAGE_SIZE } from './pagination';
3333
* This feature should be removed at some point!
3434
*/
3535
export async function trackNamespaces(): Promise<void> {
36-
const logContext = {};
36+
const logContext: Record<string, unknown> = {};
3737
const endpoint = '/api/v1/namespaces';
3838

3939
const loggedListMethod = async () => {
@@ -60,11 +60,9 @@ export async function trackNamespaces(): Promise<void> {
6060
informer.on(ERROR, (err) => {
6161
// Types from client library insists that callback is of type KubernetesObject
6262
const code = (err as any).code || '';
63+
logContext.code = code;
6364
if (RETRYABLE_NETWORK_ERRORS.includes(code)) {
64-
logger.debug(
65-
logContext,
66-
`informer ${code} occurred, restarting informer`,
67-
);
65+
logger.debug(logContext, 'informer error occurred, restarting informer');
6866

6967
// Restart informer after 1sec
7068
setTimeout(async () => {
@@ -93,7 +91,7 @@ export async function trackNamespaces(): Promise<void> {
9391
* This feature should be removed at some point!
9492
*/
9593
export async function trackNamespace(namespace: string): Promise<void> {
96-
const logContext = {};
94+
const logContext: Record<string, unknown> = {};
9795
const endpoint = `/api/v1/watch/namespaces/${namespace}`;
9896

9997
const loggedListMethod = async () => {
@@ -127,11 +125,9 @@ export async function trackNamespace(namespace: string): Promise<void> {
127125
informer.on(ERROR, (err) => {
128126
// Types from client library insists that callback is of type KubernetesObject
129127
const code = (err as any).code || '';
128+
logContext.code = code;
130129
if (RETRYABLE_NETWORK_ERRORS.includes(code)) {
131-
logger.debug(
132-
logContext,
133-
`informer ${code} occurred, restarting informer`,
134-
);
130+
logger.debug(logContext, 'informer error occurred, restarting informer');
135131

136132
// Restart informer after 1sec
137133
setTimeout(async () => {

test/unit/supervisor/watchers.spec.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { V1Namespace } from '@kubernetes/client-node';
22
import { config } from '../../../src/common/config';
33

4-
import * as watchers from '../../../src/supervisor/watchers';
54
import {
5+
extractNamespaceName,
6+
isExcludedNamespace,
67
kubernetesInternalNamespaces,
78
openshiftInternalNamespaces,
89
} from '../../../src/supervisor/watchers/internal-namespaces';
@@ -14,14 +15,14 @@ describe('extractNamespaceName()', () => {
1415
['undefined name', { metadata: { name: undefined } } as V1Namespace],
1516
['empty name', { metadata: { name: '' } } as V1Namespace],
1617
])('throws on %s', (_testCaseName, input) => {
17-
expect(() => watchers.extractNamespaceName(input)).toThrowError(
18+
expect(() => extractNamespaceName(input)).toThrowError(
1819
'Namespace missing metadata.name',
1920
);
2021
});
2122

2223
test('returns namespace.metadata.name', () => {
2324
expect(
24-
watchers.extractNamespaceName({
25+
extractNamespaceName({
2526
metadata: { name: 'literally anything else' },
2627
}),
2728
).toEqual('literally anything else');
@@ -35,7 +36,7 @@ describe('isExcludedNamespace() internal Kubernetes namespaces', () => {
3536

3637
for (const internalNamespace of kubernetesInternalNamespaces) {
3738
test(`isExcludedNamespace(${internalNamespace}) -> true`, () => {
38-
expect(watchers.isExcludedNamespace(internalNamespace)).toEqual(true);
39+
expect(isExcludedNamespace(internalNamespace)).toEqual(true);
3940
});
4041
}
4142

@@ -47,7 +48,7 @@ describe('isExcludedNamespace() internal Kubernetes namespaces', () => {
4748
[''],
4849
[undefined as unknown as string],
4950
])('isExcludedNamespace(%s) -> false', (input) => {
50-
expect(watchers.isExcludedNamespace(input)).toEqual(false);
51+
expect(isExcludedNamespace(input)).toEqual(false);
5152
});
5253
});
5354

@@ -58,7 +59,7 @@ describe('isExcludedNamespace() openshift internal namespaces', () => {
5859

5960
for (const internalNamespace of openshiftInternalNamespaces) {
6061
test(`isExcludedNamespace(${internalNamespace}) -> true`, () => {
61-
expect(watchers.isExcludedNamespace(internalNamespace)).toEqual(true);
62+
expect(isExcludedNamespace(internalNamespace)).toEqual(true);
6263
});
6364
}
6465

@@ -69,7 +70,7 @@ describe('isExcludedNamespace() openshift internal namespaces', () => {
6970
[''],
7071
[undefined as unknown as string],
7172
])('isExcludedNamespace(%s) -> false', (input) => {
72-
expect(watchers.isExcludedNamespace(input)).toEqual(false);
73+
expect(isExcludedNamespace(input)).toEqual(false);
7374
});
7475
});
7576

@@ -85,20 +86,20 @@ describe('isExcludedNamespace() excluded namespaces from config', () => {
8586

8687
excludedNamespacesFromConfig.forEach((namespace) => {
8788
test(`[excluded namespaces from config] isExcludedNamespace(${namespace}) -> true`, () => {
88-
expect(watchers.isExcludedNamespace(namespace)).toEqual(true);
89+
expect(isExcludedNamespace(namespace)).toEqual(true);
8990
});
9091
});
9192

9293
for (const internalNamespace of openshiftInternalNamespaces) {
9394
test(`[openshift internal namespaces] isExcludedNamespace(${internalNamespace}) -> true`, () => {
94-
expect(watchers.isExcludedNamespace(internalNamespace)).toEqual(true);
95+
expect(isExcludedNamespace(internalNamespace)).toEqual(true);
9596
});
9697
}
9798

9899
test.each([['kube-system']['egg'], [''], [undefined as unknown as string]])(
99100
'isExcludedNamespace(%s) -> false',
100101
(input) => {
101-
expect(watchers.isExcludedNamespace(input)).toEqual(false);
102+
expect(isExcludedNamespace(input)).toEqual(false);
102103
},
103104
);
104105
});

0 commit comments

Comments
 (0)