File tree Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Expand file tree Collapse file tree 2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -231,8 +231,10 @@ export class KubeConfig {
231
231
this . currentContext = contextName ;
232
232
}
233
233
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
+ }
236
238
config . clusters . forEach ( ( cluster : Cluster ) => {
237
239
this . addCluster ( cluster ) ;
238
240
} ) ;
@@ -280,14 +282,14 @@ export class KubeConfig {
280
282
this . contexts . push ( ctx ) ;
281
283
}
282
284
283
- public loadFromDefault ( opts ?: Partial < ConfigOptions > ) : void {
285
+ public loadFromDefault ( opts ?: Partial < ConfigOptions > , contextFromStartingConfig : boolean = false ) : void {
284
286
if ( process . env . KUBECONFIG && process . env . KUBECONFIG . length > 0 ) {
285
287
const files = process . env . KUBECONFIG . split ( path . delimiter ) ;
286
288
this . loadFromFile ( files [ 0 ] , opts ) ;
287
289
for ( let i = 1 ; i < files . length ; i ++ ) {
288
290
const kc = new KubeConfig ( ) ;
289
291
kc . loadFromFile ( files [ i ] , opts ) ;
290
- this . mergeConfig ( kc ) ;
292
+ this . mergeConfig ( kc , contextFromStartingConfig ) ;
291
293
}
292
294
return ;
293
295
}
Original file line number Diff line number Diff line change @@ -1036,6 +1036,14 @@ describe('KubeConfig', () => {
1036
1036
expect ( kc . contexts . length ) . to . equal ( 4 ) ;
1037
1037
expect ( kc . getCurrentContext ( ) ) . to . equal ( 'contextA' ) ;
1038
1038
} ) ;
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
+ } ) ;
1039
1047
it ( 'should throw with duplicate clusters' , ( ) => {
1040
1048
process . env . KUBECONFIG = kcFileName + path . delimiter + kcDupeCluster ;
1041
1049
You can’t perform that action at this time.
0 commit comments