@@ -217,7 +217,7 @@ export class KubeConfig {
217217 ) ;
218218 }
219219
220- public makeApiClient < T extends ApiType > ( apiClientType : { new ( server : string ) : T } ) {
220+ public makeApiClient < T extends ApiType > ( apiClientType : ApiConstructor < T > ) {
221221 const apiClient = new apiClientType ( this . getCurrentCluster ( ) . server ) ;
222222 apiClient . setDefaultAuthentication ( this ) ;
223223
@@ -306,7 +306,11 @@ export class KubeConfig {
306306}
307307
308308export interface ApiType {
309- setDefaultAuthentication ( config : KubeConfig ) ;
309+ setDefaultAuthentication ( config : api . Authentication ) ;
310+ }
311+
312+ export interface ApiConstructor < T extends ApiType > {
313+ new ( server : string ) : T ;
310314}
311315
312316// This class is deprecated and will eventually be removed.
@@ -319,26 +323,36 @@ export class Config {
319323 Config . SERVICEACCOUNT_ROOT + '/token' ;
320324
321325 public static fromFile ( filename : string ) : api . Core_v1Api {
326+ return Config . apiFromFile ( filename , api . Core_v1Api ) ;
327+ }
328+
329+ public static fromCluster ( ) : api . Core_v1Api {
330+ return Config . apiFromCluster ( api . Core_v1Api ) ;
331+ }
332+
333+ public static defaultClient ( ) : api . Core_v1Api {
334+ return Config . apiFromDefaultClient ( api . Core_v1Api ) ;
335+ }
336+
337+ public static apiFromFile < T extends ApiType > ( filename : string , apiClientType : ApiConstructor < T > ) : T {
322338 const kc = new KubeConfig ( ) ;
323339 kc . loadFromFile ( filename ) ;
324-
325- return kc . makeApiClient ( api . Core_v1Api ) ;
340+ return kc . makeApiClient ( apiClientType ) ;
326341 }
327342
328- public static fromCluster ( ) : api . Core_v1Api {
343+ public static apiFromCluster < T extends ApiType > ( apiClientType : ApiConstructor < T > ) : T {
329344 const kc = new KubeConfig ( ) ;
330345 kc . loadFromCluster ( ) ;
331346
332- const k8sApi = new api . Core_v1Api ( kc . getCurrentCluster ( ) . server ) ;
347+ const k8sApi = new apiClientType ( kc . getCurrentCluster ( ) . server ) ;
333348 k8sApi . setDefaultAuthentication ( kc ) ;
334349
335350 return k8sApi ;
336351 }
337352
338- public static defaultClient ( ) : api . Core_v1Api {
353+ public static apiFromDefaultClient < T extends ApiType > ( apiClientType : ApiConstructor < T > ) : T {
339354 const kc = new KubeConfig ( ) ;
340355 kc . loadFromDefault ( ) ;
341-
342- return kc . makeApiClient ( api . Core_v1Api ) ;
356+ return kc . makeApiClient ( apiClientType ) ;
343357 }
344358}
0 commit comments