From 2bf01a735494a09a20660a8c514e11fe51f4377e Mon Sep 17 00:00:00 2001 From: Alvaro Saurin Date: Sat, 16 Jan 2021 19:36:54 +0100 Subject: [PATCH] Simplify the KUBECONFIG management: just use the same as the k8s extension Signed-off-by: Alvaro Saurin --- package.json | 5 --- src/commands/createCluster.ts | 4 +-- src/k3d/k3d.ts | 24 ++------------- src/providers/clusterProvider.ts | 4 +-- src/utils/config.ts | 27 ----------------- src/utils/kubeconfig.ts | 52 -------------------------------- 6 files changed, 7 insertions(+), 109 deletions(-) delete mode 100644 src/utils/kubeconfig.ts diff --git a/package.json b/package.json index c4c56e0..156c2b0 100644 --- a/package.json +++ b/package.json @@ -180,11 +180,6 @@ "default": "always", "description": "Update the kubeconfig after creating or deleting a cluster." }, - "k3d.kubeconfig": { - "type": "string", - "default": "", - "description": "Apply changes in this kubeconfig when merging configuration after creating new clusters." - }, "k3d.replaceContext": { "type": "string", "enum": [ diff --git a/src/commands/createCluster.ts b/src/commands/createCluster.ts index 4298c64..8622686 100644 --- a/src/commands/createCluster.ts +++ b/src/commands/createCluster.ts @@ -8,7 +8,7 @@ import * as form from './createClusterForm'; import * as k3d from '../k3d/k3d'; -import * as config from '../utils/config'; +import * as kubectl from '../utils/kubectl'; import { logChannel } from '../utils/log'; import { shell, ProcessTrackingEvent } from '../utils/shell'; import { succeeded, Errorable } from '../utils/errorable'; @@ -52,7 +52,7 @@ export async function createClusterInteractive( clusterSettings: settings.ClusterCreateSettings, switchContext: boolean = true): Promise { - const kubeconfig = await config.getK3DKubeconfigPath(); + const kubeconfig = await kubectl.getKubeconfigPath(); // createClusterProgressOf is invoked for processing each line of output from `k3d cluster create` function createClusterProgressOf(e: ProcessTrackingEvent): ProgressStep> { diff --git a/src/k3d/k3d.ts b/src/k3d/k3d.ts index 3e599eb..d0c62f8 100644 --- a/src/k3d/k3d.ts +++ b/src/k3d/k3d.ts @@ -1,12 +1,9 @@ -import * as vscode from 'vscode'; - import { Observable, throwError } from 'rxjs'; import { K3dClusterInfo, K3dRegistryInfo } from "./k3d.objectmodel"; import { ClusterCreateSettings, createClusterArgsFromSettings } from '../commands/createClusterSettings'; import { getOrInstallK3D, EnsureMode } from '../installer/installer'; -import * as config from '../utils/config'; import { Errorable, failed } from '../utils/errorable'; import * as shell from '../utils/shell'; import { logChannel } from '../utils/log'; @@ -29,18 +26,10 @@ async function invokeK3DCommandObj( } const exe = k3dExe.result; - let opts = shell.defExecOpts(); + const kubeconfig = await kubectl.getKubeconfigPath(); - const kubeconfig = await config.getK3DKubeconfigPath(); - if (kubeconfig.length > 0) { - if (kubeconfig.includes(":")) { - const warningMsg = `KUBECONFIG includes multiple files: k3d will not be able to update it.`; - logChannel.appendLine(`[WARNING] ${warningMsg}`); - vscode.window.showInformationMessage(`WARNING: ${warningMsg}.`); - } - - opts.env["KUBECONFIG"] = kubeconfig; - } + let opts = shell.defExecOpts(); + opts.env["KUBECONFIG"] = kubeconfig; const cmd = `${exe} ${command} ${args}`; logChannel.appendLine(`$ ${cmd}`); @@ -68,14 +57,7 @@ function invokeK3DCommandTracking( const exe = k3dExe.result; let opts = shell.defExecOpts(); - if (kubeconfig) { - if (kubeconfig.includes(":")) { - const warningMsg = `KUBECONFIG includes multiple files: k3d will not be able to update it.`; - logChannel.appendLine(`[WARNING] ${warningMsg}`); - vscode.window.showInformationMessage(`WARNING: ${warningMsg}.`); - } - opts.env["KUBECONFIG"] = kubeconfig; } diff --git a/src/providers/clusterProvider.ts b/src/providers/clusterProvider.ts index 005f9ef..614ded9 100644 --- a/src/providers/clusterProvider.ts +++ b/src/providers/clusterProvider.ts @@ -11,7 +11,7 @@ import * as settings from '../commands/createClusterSettings'; import * as k3d from '../k3d/k3d'; -import * as config from '../utils/config'; +import * as kubectl from '../utils/kubectl'; import { Errorable } from '../utils/errorable'; import { shell, ProcessTrackingEvent } from '../utils/shell'; import { cantHappen } from '../utils/never'; @@ -122,7 +122,7 @@ async function getPageCreatingCluster(previousData: any): Promise>> = k3d.createCluster(shell, createSettings, kubeconfig).pipe( diff --git a/src/utils/config.ts b/src/utils/config.ts index c714840..efc0886 100644 --- a/src/utils/config.ts +++ b/src/utils/config.ts @@ -2,7 +2,6 @@ import * as vscode from 'vscode'; -import * as kubectl from './kubectl'; import { Platform, platform } from "./shell"; // the K3D config key @@ -11,10 +10,6 @@ export const VS_KUBE_K3D_CFG_KEY = "k3d"; // the Kubernetes tools config key export const VS_KUBE_CFG_KEY = "vs-kubernetes"; -// setting: force a specific KUBECONFIG where the kubeconfig will be merged -export const VS_KUBE_K3D_FORCE_KUBECONFIG_CFG_KEY = - `${VS_KUBE_K3D_CFG_KEY}.kubeconfig`; - // setting: merge of the new kubeconfig in the default kubeconfig export const VS_KUBE_K3D_UPDATE_KUBECONFIG_CFG_KEY = `${VS_KUBE_K3D_CFG_KEY}.updateKubeconfig`; @@ -47,28 +42,6 @@ export const VS_KUBE_K3D_DOCKERHOST_CFG_KEY = const USE_WSL_KEY = "use-wsl"; -export function getK3DConfigForcedKubeconfig(): string | undefined { - return vscode.workspace.getConfiguration()[VS_KUBE_K3D_FORCE_KUBECONFIG_CFG_KEY]; -} - -export async function getK3DKubeconfigPath(kubeconfig?: string): Promise { - if (kubeconfig) { - return kubeconfig; - } - - const forcedKubeconfig = getK3DConfigForcedKubeconfig(); - if (forcedKubeconfig) { - return forcedKubeconfig; - } - - const systemKubeconfig = await kubectl.getKubeconfigPath(); - if (systemKubeconfig.length > 0) { - return kubectl.getKubeconfigPath(); - } - - return ""; -} - export enum UpdateKubeconfig { OnCreate = 1, OnDelete, diff --git a/src/utils/kubeconfig.ts b/src/utils/kubeconfig.ts deleted file mode 100644 index 9cad34f..0000000 --- a/src/utils/kubeconfig.ts +++ /dev/null @@ -1,52 +0,0 @@ -import * as vscode from 'vscode'; -import * as path from 'path'; -import * as shelljs from 'shelljs'; -import { getUseWsl, VS_KUBE_CFG_KEY } from './config'; - -const VS_KUBE_KUBECONFIG_CFG_KEY = `${VS_KUBE_CFG_KEY}.kubeconfig`; -const VS_KUBE_KNOWN_KUBECONFIGS_CFG_KEY = `${VS_KUBE_CFG_KEY}.knownKubeconfigs`; - -export function getKnownKubeconfigs(): string[] { - const kkcConfig = vscode.workspace.getConfiguration(VS_KUBE_CFG_KEY)[VS_KUBE_KNOWN_KUBECONFIGS_CFG_KEY]; - if (!kkcConfig || !kkcConfig.length) { - return []; - } - return kkcConfig as string[]; -} - -export function getActiveKubeconfig(): string { - return vscode.workspace.getConfiguration(VS_KUBE_CFG_KEY)[VS_KUBE_KUBECONFIG_CFG_KEY]; -} - -// TODO: I think we could replace this by https://github.com/Azure/vscode-kubernetes-tools/blob/master/docs/extending/configuration.md -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; -}