forbid duplicate cluster names in config

Signed-off-by: Jamie Klassen <jamie.klassen@broadcom.com>
This commit is contained in:
Jamie Klassen
2024-01-29 10:51:09 -05:00
parent 7411fd006d
commit 666eff5cc0
3 changed files with 33 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
---
'@backstage/plugin-kubernetes-backend': minor
---
**BREAKING** The backend will fail to start if two clusters in the app-config
have the same name. The requirement for unique names has been declared in the
docs for some time, but is now enforced.
@@ -405,4 +405,25 @@ describe('ConfigClusterLocator', () => {
`Invalid cluster 'cluster1': mock error`,
);
});
it('fails on duplicate cluster names', () => {
const config: Config = new ConfigReader({
clusters: [
{
name: 'cluster',
url: 'url',
authProvider: 'authProvider',
},
{
name: 'cluster',
url: 'url',
authProvider: 'authProvider',
},
],
});
expect(() => ConfigClusterLocator.fromConfig(config, authStrategy)).toThrow(
`Duplicate cluster name 'cluster'`,
);
});
});
@@ -35,12 +35,17 @@ export class ConfigClusterLocator implements KubernetesClustersSupplier {
config: Config,
authStrategy: AuthenticationStrategy,
): ConfigClusterLocator {
const clusterNames = new Set();
return new ConfigClusterLocator(
config.getConfigArray('clusters').map(c => {
const authMetadataBlock = c.getOptional<{
[ANNOTATION_KUBERNETES_AUTH_PROVIDER]?: string;
}>('authMetadata');
const name = c.getString('name');
if (clusterNames.has(name)) {
throw new Error(`Duplicate cluster name '${name}'`);
}
clusterNames.add(name);
const authProvider =
authMetadataBlock?.[ANNOTATION_KUBERNETES_AUTH_PROVIDER] ??
c.getOptionalString('authProvider');