Skip to content

Commit 80e7752

Browse files
authored
Merge pull request #99 from sheldonkwok/feature/api-config-helpers
Feature/api config helpers
2 parents c654db9 + aa6221b commit 80e7752

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

node-client/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node-client/src/config.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

308308
export 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

Comments
 (0)