Skip to content

Commit fa4fe69

Browse files
committed
fix: remove slashes from cluster names
1 parent b691347 commit fa4fe69

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

snyk-monitor/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ helm upgrade --install snyk-monitor snyk-charts/snyk-monitor --namespace snyk-mo
9898
To better organise the data scanned inside your cluster, the monitor requires a cluster name to be set.
9999
Replace the value of `clusterName` with the name of your cluster.
100100

101-
**Please note that we cannot process cluster names that include `/`. The workloads will not be imported.**
101+
**Please note that `/` in cluster name is disallowed. Any `/` in cluster names will be removed.**
102102

103103
## Upgrades ##
104104

src/common/config.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,27 @@ function loadExcludedNamespaces(): string[] | null {
1818
}
1919
}
2020

21+
function getClusterName(): string {
22+
if (!config.CLUSTER_NAME) {
23+
return 'Default cluster';
24+
}
25+
26+
if (config.CLUSTER_NAME.includes('/')) {
27+
// logger is not yet created so defaulting to console.log
28+
console.log(
29+
`removing disallowed character "/" from clusterName (${config.CLUSTER_NAME})`,
30+
);
31+
return config.CLUSTER_NAME.replace(/\//g, '');
32+
}
33+
34+
return config.CLUSTER_NAME;
35+
}
36+
2137
// NOTE: The agent identifier is replaced with a stable identifier once snyk-monitor starts up
2238
config.AGENT_ID = uuidv4();
2339

2440
config.INTEGRATION_ID = config.INTEGRATION_ID.trim();
25-
config.CLUSTER_NAME = config.CLUSTER_NAME || 'Default cluster';
41+
config.CLUSTER_NAME = getClusterName();
2642
config.IMAGE_STORAGE_ROOT = '/var/tmp';
2743
config.POLICIES_STORAGE_ROOT = '/tmp/policies';
2844
config.EXCLUDED_NAMESPACES = loadExcludedNamespaces();

test/common/config.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
describe('extractNamespaceName()', () => {
2+
beforeEach(() => {
3+
jest.resetModules();
4+
});
5+
6+
afterEach(() => {
7+
jest.restoreAllMocks();
8+
});
9+
10+
test.each([
11+
[
12+
'cluster name with /',
13+
{
14+
clusterNameEnvVar: 'cluster/name',
15+
wantClusterName: 'clustername',
16+
consoleLogCalledTimes: 1,
17+
},
18+
],
19+
[
20+
'cluster name with more than one /',
21+
{
22+
clusterNameEnvVar: 'cluster/name/slash',
23+
wantClusterName: 'clusternameslash',
24+
consoleLogCalledTimes: 1,
25+
},
26+
],
27+
[
28+
'cluster name without /',
29+
{
30+
clusterNameEnvVar: 'normal cluster name',
31+
wantClusterName: 'normal cluster name',
32+
consoleLogCalledTimes: 0,
33+
},
34+
],
35+
[
36+
'no cluster name set',
37+
{
38+
clusterNameEnvVar: '',
39+
wantClusterName: 'Default cluster',
40+
consoleLogCalledTimes: 0,
41+
},
42+
],
43+
])(
44+
'%s',
45+
(
46+
_testCaseName,
47+
{ clusterNameEnvVar, wantClusterName, consoleLogCalledTimes },
48+
) => {
49+
if (clusterNameEnvVar) {
50+
process.env.SNYK_CLUSTER_NAME = clusterNameEnvVar;
51+
}
52+
53+
const consoleSpy = jest.spyOn(console, 'log').mockReturnValue();
54+
55+
const { config } = require('../../src/common/config');
56+
expect(config.CLUSTER_NAME).toBe(wantClusterName);
57+
expect(consoleSpy).toHaveBeenCalledTimes(consoleLogCalledTimes);
58+
59+
delete process.env.SNYK_CLUSTER_NAME;
60+
},
61+
);
62+
});

0 commit comments

Comments
 (0)