@@ -14,49 +14,6 @@ import { Cluster, Context, newClusters, newContexts, newUsers, User } from './co
14
14
import { ExecAuth } from './exec_auth' ;
15
15
16
16
export class KubeConfig {
17
- // Only public for testing.
18
- public static findHomeDir ( ) : string | null {
19
- if ( process . env . HOME ) {
20
- try {
21
- fs . accessSync ( process . env . HOME ) ;
22
- return process . env . HOME ;
23
- // tslint:disable-next-line:no-empty
24
- } catch ( ignore ) { }
25
- }
26
- if ( process . platform !== 'win32' ) {
27
- return null ;
28
- }
29
- if ( process . env . HOMEDRIVE && process . env . HOMEPATH ) {
30
- const dir = path . join ( process . env . HOMEDRIVE , process . env . HOMEPATH ) ;
31
- try {
32
- fs . accessSync ( dir ) ;
33
- return dir ;
34
- // tslint:disable-next-line:no-empty
35
- } catch ( ignore ) { }
36
- }
37
- if ( process . env . USERPROFILE ) {
38
- try {
39
- fs . accessSync ( process . env . USERPROFILE ) ;
40
- return process . env . USERPROFILE ;
41
- // tslint:disable-next-line:no-empty
42
- } catch ( ignore ) { }
43
- }
44
- return null ;
45
- }
46
-
47
- // Only really public for testing...
48
- public static findObject ( list : any [ ] , name : string , key : string ) {
49
- for ( const obj of list ) {
50
- if ( obj . name === name ) {
51
- if ( obj [ key ] ) {
52
- return obj [ key ] ;
53
- }
54
- return obj ;
55
- }
56
- }
57
- return null ;
58
- }
59
-
60
17
private static authenticators : Authenticator [ ] = [
61
18
new CloudAuth ( ) ,
62
19
new ExecAuth ( ) ,
@@ -106,7 +63,7 @@ export class KubeConfig {
106
63
if ( ! this . contexts ) {
107
64
return null ;
108
65
}
109
- return KubeConfig . findObject ( this . contexts , name , 'context' ) ;
66
+ return findObject ( this . contexts , name , 'context' ) ;
110
67
}
111
68
112
69
public getCurrentCluster ( ) : Cluster | null {
@@ -117,16 +74,20 @@ export class KubeConfig {
117
74
return this . getCluster ( context . cluster ) ;
118
75
}
119
76
120
- public getCluster ( name : string ) : Cluster {
121
- return KubeConfig . findObject ( this . clusters , name , 'cluster' ) ;
77
+ public getCluster ( name : string ) : Cluster | null {
78
+ return findObject ( this . clusters , name , 'cluster' ) ;
122
79
}
123
80
124
- public getCurrentUser ( ) {
125
- return this . getUser ( this . getCurrentContextObject ( ) . user ) ;
81
+ public getCurrentUser ( ) : User | null {
82
+ const ctx = this . getCurrentContextObject ( ) ;
83
+ if ( ! ctx ) {
84
+ return null ;
85
+ }
86
+ return this . getUser ( ctx . user ) ;
126
87
}
127
88
128
- public getUser ( name : string ) : User {
129
- return KubeConfig . findObject ( this . users , name , 'user' ) ;
89
+ public getUser ( name : string ) : User | null {
90
+ return findObject ( this . users , name , 'user' ) ;
130
91
}
131
92
132
93
public loadFromFile ( file : string ) {
@@ -138,7 +99,7 @@ export class KubeConfig {
138
99
139
100
this . applyOptions ( opts ) ;
140
101
141
- if ( user . username ) {
102
+ if ( user && user . username ) {
142
103
opts . auth = `${ user . username } :${ user . password } ` ;
143
104
}
144
105
}
@@ -233,7 +194,7 @@ export class KubeConfig {
233
194
this . loadFromFile ( process . env . KUBECONFIG ) ;
234
195
return ;
235
196
}
236
- const home = KubeConfig . findHomeDir ( ) ;
197
+ const home = findHomeDir ( ) ;
237
198
if ( home ) {
238
199
const config = path . join ( home , '.kube' , 'config' ) ;
239
200
try {
@@ -260,8 +221,8 @@ export class KubeConfig {
260
221
} catch ( ignore ) { }
261
222
262
223
this . loadFromClusterAndUser (
263
- { name : 'cluster' , server : 'http://localhost:8080' } as Cluster ,
264
- { name : 'user' } as User ,
224
+ { name : 'cluster' , server : 'http://localhost:8080' } as Cluster ,
225
+ { name : 'user' } as User ,
265
226
) ;
266
227
}
267
228
@@ -280,31 +241,21 @@ export class KubeConfig {
280
241
return this . getContextObject ( this . currentContext ) ;
281
242
}
282
243
283
- private bufferFromFileOrString ( file ?: string , data ?: string ) : Buffer | null {
284
- if ( file ) {
285
- return fs . readFileSync ( file ) ;
286
- }
287
- if ( data ) {
288
- return Buffer . from ( base64 . decode ( data ) , 'utf-8' ) ;
289
- }
290
- return null ;
291
- }
292
-
293
244
private applyHTTPSOptions ( opts : request . Options | https . RequestOptions ) {
294
245
const cluster = this . getCurrentCluster ( ) ;
295
246
const user = this . getCurrentUser ( ) ;
296
247
if ( ! user ) {
297
248
return ;
298
249
}
299
- const ca = cluster != null ? this . bufferFromFileOrString ( cluster . caFile , cluster . caData ) : null ;
250
+ const ca = cluster != null ? bufferFromFileOrString ( cluster . caFile , cluster . caData ) : null ;
300
251
if ( ca ) {
301
252
opts . ca = ca ;
302
253
}
303
- const cert = this . bufferFromFileOrString ( user . certFile , user . certData ) ;
254
+ const cert = bufferFromFileOrString ( user . certFile , user . certData ) ;
304
255
if ( cert ) {
305
256
opts . cert = cert ;
306
257
}
307
- const key = this . bufferFromFileOrString ( user . keyFile , user . keyData ) ;
258
+ const key = bufferFromFileOrString ( user . keyFile , user . keyData ) ;
308
259
if ( key ) {
309
260
opts . key = key ;
310
261
}
@@ -349,17 +300,17 @@ export interface ApiType {
349
300
}
350
301
351
302
export interface ApiConstructor < T extends ApiType > {
352
- new ( server : string ) : T ;
303
+ new ( server : string ) : T ;
353
304
}
354
305
355
306
// This class is deprecated and will eventually be removed.
356
307
export class Config {
357
308
public static SERVICEACCOUNT_ROOT =
358
- '/var/run/secrets/kubernetes.io/serviceaccount' ;
309
+ '/var/run/secrets/kubernetes.io/serviceaccount' ;
359
310
public static SERVICEACCOUNT_CA_PATH =
360
- Config . SERVICEACCOUNT_ROOT + '/ca.crt' ;
311
+ Config . SERVICEACCOUNT_ROOT + '/ca.crt' ;
361
312
public static SERVICEACCOUNT_TOKEN_PATH =
362
- Config . SERVICEACCOUNT_ROOT + '/token' ;
313
+ Config . SERVICEACCOUNT_ROOT + '/token' ;
363
314
364
315
public static fromFile ( filename : string ) : api . Core_v1Api {
365
316
return Config . apiFromFile ( filename , api . Core_v1Api ) ;
@@ -400,3 +351,61 @@ export class Config {
400
351
return kc . makeApiClient ( apiClientType ) ;
401
352
}
402
353
}
354
+
355
+ // This is public really only for testing.
356
+ export function bufferFromFileOrString ( file ?: string , data ?: string ) : Buffer | null {
357
+ if ( file ) {
358
+ return fs . readFileSync ( file ) ;
359
+ }
360
+ if ( data ) {
361
+ return Buffer . from ( base64 . decode ( data ) , 'utf-8' ) ;
362
+ }
363
+ return null ;
364
+ }
365
+
366
+ // Only public for testing.
367
+ export function findHomeDir ( ) : string | null {
368
+ if ( process . env . HOME ) {
369
+ try {
370
+ fs . accessSync ( process . env . HOME ) ;
371
+ return process . env . HOME ;
372
+ // tslint:disable-next-line:no-empty
373
+ } catch ( ignore ) { }
374
+ }
375
+ if ( process . platform !== 'win32' ) {
376
+ return null ;
377
+ }
378
+ if ( process . env . HOMEDRIVE && process . env . HOMEPATH ) {
379
+ const dir = path . join ( process . env . HOMEDRIVE , process . env . HOMEPATH ) ;
380
+ try {
381
+ fs . accessSync ( dir ) ;
382
+ return dir ;
383
+ // tslint:disable-next-line:no-empty
384
+ } catch ( ignore ) { }
385
+ }
386
+ if ( process . env . USERPROFILE ) {
387
+ try {
388
+ fs . accessSync ( process . env . USERPROFILE ) ;
389
+ return process . env . USERPROFILE ;
390
+ // tslint:disable-next-line:no-empty
391
+ } catch ( ignore ) { }
392
+ }
393
+ return null ;
394
+ }
395
+
396
+ export interface Named {
397
+ name : string ;
398
+ }
399
+
400
+ // Only really public for testing...
401
+ export function findObject < T extends Named > ( list : T [ ] , name : string , key : string ) : T | null {
402
+ for ( const obj of list ) {
403
+ if ( obj . name === name ) {
404
+ if ( obj [ key ] ) {
405
+ return obj [ key ] ;
406
+ }
407
+ return obj ;
408
+ }
409
+ }
410
+ return null ;
411
+ }
0 commit comments