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

Use in-cluster config instead of kubeconfig when running NNI from within a Kubernetes container. #3719

Merged
merged 7 commits into from
Jun 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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