File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -224,6 +224,9 @@ export class KubeConfig {
224
224
}
225
225
226
226
public addCluster ( cluster : Cluster ) {
227
+ if ( ! this . clusters ) {
228
+ this . clusters = [ ] ;
229
+ }
227
230
this . clusters . forEach ( ( c : Cluster , ix : number ) => {
228
231
if ( c . name === cluster . name ) {
229
232
throw new Error ( `Duplicate cluster: ${ c . name } ` ) ;
@@ -233,6 +236,9 @@ export class KubeConfig {
233
236
}
234
237
235
238
public addUser ( user : User ) {
239
+ if ( ! this . users ) {
240
+ this . users = [ ] ;
241
+ }
236
242
this . users . forEach ( ( c : User , ix : number ) => {
237
243
if ( c . name === user . name ) {
238
244
throw new Error ( `Duplicate user: ${ c . name } ` ) ;
@@ -242,6 +248,9 @@ export class KubeConfig {
242
248
}
243
249
244
250
public addContext ( ctx : Context ) {
251
+ if ( ! this . contexts ) {
252
+ this . contexts = [ ] ;
253
+ }
245
254
this . contexts . forEach ( ( c : Context , ix : number ) => {
246
255
if ( c . name === ctx . name ) {
247
256
throw new Error ( `Duplicate context: ${ c . name } ` ) ;
Original file line number Diff line number Diff line change @@ -1123,6 +1123,31 @@ describe('KubeConfig', () => {
1123
1123
} ) ;
1124
1124
} ) ;
1125
1125
1126
+ describe ( 'Programmatic' , ( ) => {
1127
+ it ( 'should be able to generate a valid config from code' , ( ) => {
1128
+ const kc = new KubeConfig ( ) ;
1129
+ kc . addCluster ( {
1130
+ name : 'testCluster' ,
1131
+ server : `https://localhost:9889` ,
1132
+ skipTLSVerify : true ,
1133
+ } ) ;
1134
+ kc . addUser ( {
1135
+ token : 'token' ,
1136
+ username : 'username' ,
1137
+ name : 'testUser' ,
1138
+ } ) ;
1139
+ kc . addContext ( {
1140
+ cluster : 'testCluster' ,
1141
+ name : 'test' ,
1142
+ user : 'testUser' ,
1143
+ } ) ;
1144
+ kc . setCurrentContext ( 'test' ) ;
1145
+
1146
+ expect ( kc . getCurrentCluster ( ) ! . name ) . to . equal ( 'testCluster' ) ;
1147
+ expect ( kc . getCurrentUser ( ) ! . username ) . to . equal ( 'username' ) ;
1148
+ } ) ;
1149
+ } ) ;
1150
+
1126
1151
describe ( 'BufferOrFile' , ( ) => {
1127
1152
it ( 'should load from root if present' , ( ) => {
1128
1153
const data = 'some data for file' ;
You can’t perform that action at this time.
0 commit comments