Unverified Commit e82731f2 authored by Richard Fan's avatar Richard Fan Committed by GitHub
Browse files

Use in-cluster config instead of kubeconfig when running NNI from within a...


Use in-cluster config instead of kubeconfig when running NNI from within a Kubernetes container. (#3719)
Co-authored-by: default avatarRichard Fan <richard.fan@petuum.com>
parent b4da4a7b
...@@ -7,6 +7,21 @@ ...@@ -7,6 +7,21 @@
import {Client1_10, config} from 'kubernetes-client'; import {Client1_10, config} from 'kubernetes-client';
import {getLogger, Logger} from '../../common/log'; 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 * Generic Kubernetes client, target version >= 1.9
*/ */
...@@ -16,7 +31,7 @@ class GeneralK8sClient { ...@@ -16,7 +31,7 @@ class GeneralK8sClient {
protected namespace: string = 'default'; protected namespace: string = 'default';
constructor() { 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(); this.client.loadSpec();
} }
...@@ -139,7 +154,7 @@ abstract class KubernetesCRDClient { ...@@ -139,7 +154,7 @@ abstract class KubernetesCRDClient {
protected crdSchema: any; protected crdSchema: any;
constructor() { constructor() {
this.client = new Client1_10({config: config.fromKubeconfig()}); this.client = new Client1_10({config: getKubernetesConfig()});
this.client.loadSpec(); this.client.loadSpec();
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment