@@ -196,6 +196,36 @@ export class KubeConfig {
196
196
this . currentContext = contextName ;
197
197
}
198
198
199
+ public loadFromDefault ( ) {
200
+ if ( process . env . KUBECONFIG ) {
201
+ this . loadFromFile ( process . env . KUBECONFIG ) ;
202
+ return ;
203
+ }
204
+
205
+ const config = path . join ( process . env . HOME , '.kube' , 'config' ) ;
206
+ if ( fs . existsSync ( config ) ) {
207
+ this . loadFromFile ( config ) ;
208
+ return ;
209
+ }
210
+
211
+ if ( fs . existsSync ( Config . SERVICEACCOUNT_TOKEN_PATH ) ) {
212
+ this . loadFromCluster ( ) ;
213
+ return ;
214
+ }
215
+
216
+ this . loadFromClusterAndUser (
217
+ { name : 'cluster' , server : 'http://localhost:8080' } as Cluster ,
218
+ { name : 'user' } as User ,
219
+ ) ;
220
+ }
221
+
222
+ public makeApiClient < T extends ApiType > ( apiClientType : { new ( server : string ) : T } ) {
223
+ const apiClient = new apiClientType ( this . getCurrentCluster ( ) . server ) ;
224
+ apiClient . setDefaultAuthentication ( this ) ;
225
+
226
+ return apiClient ;
227
+ }
228
+
199
229
private getCurrentContextObject ( ) {
200
230
return this . getContextObject ( this . currentContext ) ;
201
231
}
@@ -276,6 +306,11 @@ export class KubeConfig {
276
306
}
277
307
}
278
308
309
+ export interface ApiType {
310
+ setDefaultAuthentication ( config : KubeConfig ) ;
311
+ }
312
+
313
+ // This class is deprecated and will eventually be removed.
279
314
export class Config {
280
315
public static SERVICEACCOUNT_ROOT =
281
316
'/var/run/secrets/kubernetes.io/serviceaccount' ;
@@ -288,10 +323,7 @@ export class Config {
288
323
const kc = new KubeConfig ( ) ;
289
324
kc . loadFromFile ( filename ) ;
290
325
291
- const k8sApi = new client . Core_v1Api ( kc . getCurrentCluster ( ) . server ) ;
292
- k8sApi . setDefaultAuthentication ( kc ) ;
293
-
294
- return k8sApi ;
326
+ return kc . makeApiClient ( api . Core_v1Api ) ;
295
327
}
296
328
297
329
public static fromCluster ( ) : api . Core_v1Api {
@@ -317,19 +349,9 @@ export class Config {
317
349
}
318
350
319
351
public static defaultClient ( ) : api . Core_v1Api {
320
- if ( process . env . KUBECONFIG ) {
321
- return Config . fromFile ( process . env . KUBECONFIG ) ;
322
- }
323
-
324
- const config = path . join ( process . env . HOME , '.kube' , 'config' ) ;
325
- if ( fs . existsSync ( config ) ) {
326
- return Config . fromFile ( config ) ;
327
- }
328
-
329
- if ( fs . existsSync ( Config . SERVICEACCOUNT_TOKEN_PATH ) ) {
330
- return Config . fromCluster ( ) ;
331
- }
352
+ const kc = new KubeConfig ( ) ;
353
+ kc . loadFromDefault ( ) ;
332
354
333
- return new client . Core_v1Api ( 'http://localhost:8080' ) ;
355
+ return kc . makeApiClient ( api . Core_v1Api ) ;
334
356
}
335
357
}
0 commit comments