Skip to content

Commit

Permalink
Improved KUBECONFIG calculation
Browse files Browse the repository at this point in the history
Signed-off-by: Alvaro Saurin <[email protected]>
  • Loading branch information
inercia committed Dec 5, 2020
1 parent d7e149c commit e95b5a8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/providers/cluster-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as shelljs from 'shelljs';
import { ChildProcess } from 'child_process';
import { k3dExe } from '../k3d/k3d';
import * as k3d from '../k3d/k3d';
import { getActiveKubeconfig } from '../utils/kubeconfig';
import { getKubeconfigPath } from '../utils/kubeconfig';

const K3D_CLUSTER_PROVIDER_ID = 'k3d';

Expand Down Expand Up @@ -68,7 +68,7 @@ function createCluster(previousData: any): k8s.ClusterProviderV1.Observable<stri
argsStr += " --wait --update-default-kubeconfig";

const exe = k3dExe();
shelljs.env["KUBECONFIG"] = getActiveKubeconfig();
shelljs.env["KUBECONFIG"] = getKubeconfigPath();
const command = `${exe} cluster create ${settings.name} ${argsStr}`;

const childProcess = shelljs.exec(command, { async: true }) as ChildProcess;
Expand Down
39 changes: 39 additions & 0 deletions src/utils/kubeconfig.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as shelljs from 'shelljs';

const VS_KUBE_EXTENSION_CONFIG_KEY = "vs-kubernetes";
const VS_KUBE_KUBECONFIG_PATH_KEY = "vs-kubernetes.kubeconfig";
const VS_KUBE_KNOWN_KUBECONFIGS_KEY = "vs-kubernetes.knownKubeconfigs";
const VS_KUBE_USE_WSL_KEY = "use-wsl";

export function getUseWsl(): boolean {
return vscode.workspace.getConfiguration(VS_KUBE_EXTENSION_CONFIG_KEY)[VS_KUBE_USE_WSL_KEY];
}

export function getKnownKubeconfigs(): string[] {
const kkcConfig = vscode.workspace.getConfiguration(VS_KUBE_EXTENSION_CONFIG_KEY)[VS_KUBE_KNOWN_KUBECONFIGS_KEY];
Expand All @@ -15,3 +22,35 @@ export function getKnownKubeconfigs(): string[] {
export function getActiveKubeconfig(): string {
return vscode.workspace.getConfiguration(VS_KUBE_EXTENSION_CONFIG_KEY)[VS_KUBE_KUBECONFIG_PATH_KEY];
}

export function getKubeconfigPath(): string {
// If the user specified a kubeconfig path -WSL or not-, let's use it.
let kubeconfigPath: string | undefined = getActiveKubeconfig();

if (getUseWsl()) {
if (!kubeconfigPath) {
// User is using WSL: we want to use the same default that kubectl uses on Linux ($KUBECONFIG or home directory).
const result = shelljs.exec('wsl.exe sh -c "${KUBECONFIG:-$HOME/.kube/config}"', { silent: true }) as shelljs.ExecOutputReturnValue;
if (!result) {
throw new Error(`Impossible to retrieve the kubeconfig path from WSL. No result from the shelljs.exe call.`);
}

if (result.code !== 0) {
throw new Error(`Impossible to retrieve the kubeconfig path from WSL. Error code: ${result.code}. Error output: ${result.stderr.trim()}`);
}
kubeconfigPath = result.stdout.trim();
}
return kubeconfigPath;
}

if (!kubeconfigPath) {
kubeconfigPath = process.env['KUBECONFIG'];
}

if (!kubeconfigPath) {
// Fall back on the default kubeconfig value.
kubeconfigPath = path.join((process.env['HOME'] || process.env['USERPROFILE'] || '.'), ".kube", "config");
}

return kubeconfigPath;
}

0 comments on commit e95b5a8

Please sign in to comment.