From 3d00471210f167c684e367fb65e0f9266c531102 Mon Sep 17 00:00:00 2001 From: Adrian Ordonez Date: Sun, 24 Jan 2021 10:13:47 -0600 Subject: [PATCH 1/2] feat(config): add argument to preserve context from starting file --- src/config.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/config.ts b/src/config.ts index 01a3572a44..3abdff7098 100644 --- a/src/config.ts +++ b/src/config.ts @@ -231,8 +231,10 @@ export class KubeConfig { this.currentContext = contextName; } - public mergeConfig(config: KubeConfig): void { - this.currentContext = config.currentContext; + public mergeConfig(config: KubeConfig, preserveContext: boolean = false): void { + if (!preserveContext) { + this.currentContext = config.currentContext; + } config.clusters.forEach((cluster: Cluster) => { this.addCluster(cluster); }); @@ -280,14 +282,14 @@ export class KubeConfig { this.contexts.push(ctx); } - public loadFromDefault(opts?: Partial): void { + public loadFromDefault(opts?: Partial, contextFromStartingConfig: boolean = false): void { if (process.env.KUBECONFIG && process.env.KUBECONFIG.length > 0) { const files = process.env.KUBECONFIG.split(path.delimiter); this.loadFromFile(files[0], opts); for (let i = 1; i < files.length; i++) { const kc = new KubeConfig(); kc.loadFromFile(files[i], opts); - this.mergeConfig(kc); + this.mergeConfig(kc, contextFromStartingConfig); } return; } From e1d6a115704b95911afd451ebc88f6c0d50210ad Mon Sep 17 00:00:00 2001 From: Adrian Ordonez Date: Sun, 24 Jan 2021 10:52:00 -0600 Subject: [PATCH 2/2] test(config): add test for preserving starting file context --- src/config_test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/config_test.ts b/src/config_test.ts index 4d5e2b3ffa..0a0d79057b 100644 --- a/src/config_test.ts +++ b/src/config_test.ts @@ -1036,6 +1036,14 @@ describe('KubeConfig', () => { expect(kc.contexts.length).to.equal(4); expect(kc.getCurrentContext()).to.equal('contextA'); }); + it('should preserve starting file context', () => { + process.env.KUBECONFIG = kcFileName + path.delimiter + kc2FileName; + + const kc = new KubeConfig(); + kc.loadFromDefault({}, true); + + expect(kc.getCurrentContext()).to.equal('context2'); + }); it('should throw with duplicate clusters', () => { process.env.KUBECONFIG = kcFileName + path.delimiter + kcDupeCluster;