Skip to content

Commit dc1260c

Browse files
authored
Merge pull request #580 from adrianord/preserveContextFromStartingFile
Preserve context from starting file
2 parents a8d4f1c + e1d6a11 commit dc1260c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/config.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,10 @@ export class KubeConfig {
231231
this.currentContext = contextName;
232232
}
233233

234-
public mergeConfig(config: KubeConfig): void {
235-
this.currentContext = config.currentContext;
234+
public mergeConfig(config: KubeConfig, preserveContext: boolean = false): void {
235+
if (!preserveContext) {
236+
this.currentContext = config.currentContext;
237+
}
236238
config.clusters.forEach((cluster: Cluster) => {
237239
this.addCluster(cluster);
238240
});
@@ -280,14 +282,14 @@ export class KubeConfig {
280282
this.contexts.push(ctx);
281283
}
282284

283-
public loadFromDefault(opts?: Partial<ConfigOptions>): void {
285+
public loadFromDefault(opts?: Partial<ConfigOptions>, contextFromStartingConfig: boolean = false): void {
284286
if (process.env.KUBECONFIG && process.env.KUBECONFIG.length > 0) {
285287
const files = process.env.KUBECONFIG.split(path.delimiter);
286288
this.loadFromFile(files[0], opts);
287289
for (let i = 1; i < files.length; i++) {
288290
const kc = new KubeConfig();
289291
kc.loadFromFile(files[i], opts);
290-
this.mergeConfig(kc);
292+
this.mergeConfig(kc, contextFromStartingConfig);
291293
}
292294
return;
293295
}

src/config_test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,14 @@ describe('KubeConfig', () => {
10361036
expect(kc.contexts.length).to.equal(4);
10371037
expect(kc.getCurrentContext()).to.equal('contextA');
10381038
});
1039+
it('should preserve starting file context', () => {
1040+
process.env.KUBECONFIG = kcFileName + path.delimiter + kc2FileName;
1041+
1042+
const kc = new KubeConfig();
1043+
kc.loadFromDefault({}, true);
1044+
1045+
expect(kc.getCurrentContext()).to.equal('context2');
1046+
});
10391047
it('should throw with duplicate clusters', () => {
10401048
process.env.KUBECONFIG = kcFileName + path.delimiter + kcDupeCluster;
10411049

0 commit comments

Comments
 (0)