diff --git a/.changeset/great-rats-collect.md b/.changeset/great-rats-collect.md new file mode 100644 index 0000000000..24b01c0fe3 --- /dev/null +++ b/.changeset/great-rats-collect.md @@ -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. diff --git a/plugins/kubernetes-backend/src/cluster-locator/ConfigClusterLocator.test.ts b/plugins/kubernetes-backend/src/cluster-locator/ConfigClusterLocator.test.ts index ad3203a16d..d36f10bb13 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/ConfigClusterLocator.test.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/ConfigClusterLocator.test.ts @@ -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'`, + ); + }); }); diff --git a/plugins/kubernetes-backend/src/cluster-locator/ConfigClusterLocator.ts b/plugins/kubernetes-backend/src/cluster-locator/ConfigClusterLocator.ts index 19ba8634ad..6fd5f3b1cb 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/ConfigClusterLocator.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/ConfigClusterLocator.ts @@ -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');