Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -280,14 +282,14 @@ export class KubeConfig {
this.contexts.push(ctx);
}

public loadFromDefault(opts?: Partial<ConfigOptions>): void {
public loadFromDefault(opts?: Partial<ConfigOptions>, 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;
}
Expand Down
8 changes: 8 additions & 0 deletions src/config_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down