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 {
224224 }
225225
226226 public addCluster ( cluster : Cluster ) {
227+ if ( ! this . clusters ) {
228+ this . clusters = [ ] ;
229+ }
227230 this . clusters . forEach ( ( c : Cluster , ix : number ) => {
228231 if ( c . name === cluster . name ) {
229232 throw new Error ( `Duplicate cluster: ${ c . name } ` ) ;
@@ -233,6 +236,9 @@ export class KubeConfig {
233236 }
234237
235238 public addUser ( user : User ) {
239+ if ( ! this . users ) {
240+ this . users = [ ] ;
241+ }
236242 this . users . forEach ( ( c : User , ix : number ) => {
237243 if ( c . name === user . name ) {
238244 throw new Error ( `Duplicate user: ${ c . name } ` ) ;
@@ -242,6 +248,9 @@ export class KubeConfig {
242248 }
243249
244250 public addContext ( ctx : Context ) {
251+ if ( ! this . contexts ) {
252+ this . contexts = [ ] ;
253+ }
245254 this . contexts . forEach ( ( c : Context , ix : number ) => {
246255 if ( c . name === ctx . name ) {
247256 throw new Error ( `Duplicate context: ${ c . name } ` ) ;
Original file line number Diff line number Diff line change @@ -1123,6 +1123,31 @@ describe('KubeConfig', () => {
11231123 } ) ;
11241124 } ) ;
11251125
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+
11261151 describe ( 'BufferOrFile' , ( ) => {
11271152 it ( 'should load from root if present' , ( ) => {
11281153 const data = 'some data for file' ;
You can’t perform that action at this time.
0 commit comments