Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Use in-cluster config instead of kubeconfig when running NNI from wit…
Browse files Browse the repository at this point in the history
…hin a Kubernetes container. (#3719)

Co-authored-by: Richard Fan <richard.fan@petuum.com>
  • Loading branch information
rmfan and Richard Fan authored Jun 4, 2021
1 parent b4da4a7 commit e82731f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions ts/nni_manager/training_service/kubernetes/kubernetesApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
import {Client1_10, config} from 'kubernetes-client';
import {getLogger, Logger} from '../../common/log';

/**
* This function uses the environment variable
* 'KUBERNETES_SERVICE_HOST' to determine whether
* the code is running from within a kubernetes container.
* If it is, it returns the in-cluster config
* instead of the kubeconfig.
*/
function getKubernetesConfig(): any {
if ('KUBERNETES_SERVICE_HOST' in process.env) {
return config.getInCluster();
} else {
return config.fromKubeconfig();
}
}

/**
* Generic Kubernetes client, target version >= 1.9
*/
Expand All @@ -16,7 +31,7 @@ class GeneralK8sClient {
protected namespace: string = 'default';

constructor() {
this.client = new Client1_10({config: config.fromKubeconfig(), version: '1.9'});
this.client = new Client1_10({config: getKubernetesConfig(), version: '1.9'});
this.client.loadSpec();
}

Expand Down Expand Up @@ -139,7 +154,7 @@ abstract class KubernetesCRDClient {
protected crdSchema: any;

constructor() {
this.client = new Client1_10({config: config.fromKubeconfig()});
this.client = new Client1_10({config: getKubernetesConfig()});
this.client.loadSpec();
}

Expand Down

0 comments on commit e82731f

Please sign in to comment.