Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not use exec to get the kubeconfig file #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "microk8s-vscode",
"displayName": "Microk8s",
"description": "Work with the Microk8s local Kubernetes provider in Visual Studio Code",
"displayName": "MicroK8s",
"description": "Work with the MicroK8s local Kubernetes provider in Visual Studio Code",
"publisher": "ms-kubernetes-tools",
"version": "0.0.1",
"engines": {
Expand Down
27 changes: 10 additions & 17 deletions src/microk8s-cloud-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import * as vscode from 'vscode';
import * as k8s from 'vscode-kubernetes-tools-api';

import * as microk8s from './microk8s/microk8s';
import { failed } from './utils/errorable';
import { shell } from './utils/shell';

class Microk8sCloudProvider implements k8s.CloudExplorerV1.CloudProvider {
readonly cloudName = "Microk8s";
readonly cloudName = "MicroK8s";
readonly treeDataProvider = new Microk8sTreeDataProvider();
async getKubeconfigYaml(cluster: any): Promise<string | undefined> {
const treeNode = cluster as Microk8sCloudProviderTreeNode;
if (treeNode.nodeType === 'cluster') {
return await getMicrok8sKubeconfigYaml();
return getMicroK8sKubeconfigYaml();
}
return undefined;
}
Expand Down Expand Up @@ -39,7 +37,7 @@ class Microk8sTreeDataProvider implements vscode.TreeDataProvider<Microk8sCloudP
treeItem.tooltip = element.diagnostic;
return treeItem;
} else {
const treeItem = new vscode.TreeItem("Microk8s Instance", vscode.TreeItemCollapsibleState.None);
const treeItem = new vscode.TreeItem("Kubeconfig", vscode.TreeItemCollapsibleState.None);
treeItem.contextValue = `microk8s.cluster ${k8s.CloudExplorerV1.SHOW_KUBECONFIG_COMMANDS_CONTEXT}`;
return treeItem;
}
Expand All @@ -53,20 +51,15 @@ class Microk8sTreeDataProvider implements vscode.TreeDataProvider<Microk8sCloudP
}
}

async function getMicrok8sKubeconfigYaml(): Promise<string | undefined> {
const yaml = await microk8s.getKubeconfig(shell);
if (failed(yaml)) {
if (yaml.error[0].includes('Insufficient permissions')) {
vscode.window.showErrorMessage("Can't get kubeconfig for Microk8s. You need to add yourself to the 'microk8s' group using the command 'sudo usermod -a -G microk8s <your-name>' then log out and log in again.");
} else {
vscode.window.showErrorMessage(`Can't get kubeconfig for Microk8s: ${yaml.error[0]}`);
}
function getMicroK8sKubeconfigYaml(): string | undefined {
try {
const originalKubeconfig = microk8s.getKubeconfigYaml();
const distinctKubeconfig = renameDistinctUser(originalKubeconfig);
return distinctKubeconfig;
} catch (ex) {
vscode.window.showErrorMessage("Can't get kubeconfig for Microk8s. "+ ex.message);
return undefined;
}

const originalKubeconfig = yaml.result;
const distinctKubeconfig = renameDistinctUser(originalKubeconfig);
return distinctKubeconfig;
}

function renameDistinctUser(kubeconfig: string): string {
Expand Down
37 changes: 6 additions & 31 deletions src/microk8s/microk8s.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,12 @@
import * as vscode from 'vscode';
import * as fs from 'fs';

// import * as config from '../config/config';
import { Errorable } from '../utils/errorable';
import * as shell from '../utils/shell';

const logChannel = vscode.window.createOutputChannel("Microk8s");
const logChannel = vscode.window.createOutputChannel("MicroK8s");

async function invokeObj<T>(sh: shell.Shell, command: string, args: string, opts: shell.ExecOpts, fn: (stdout: string) => T, errfn?: (sr: shell.ShellResult) => Errorable<T>): Promise<Errorable<T>> {
const bin = /* config.microk8sPath() || */ 'microk8s';
const cmd = `${bin}.${command} ${args}`;
logChannel.appendLine(`$ ${cmd}`);
return await sh.execObj<T>(
cmd,
`microk8s.${command}`,
opts,
andLog(fn),
errfn
);
}

function andLog<T>(fn: (s: string) => T): (s: string) => T {
return (s: string) => {
logChannel.appendLine(s);
return fn(s);
};
}

export async function getKubeconfig(sh: shell.Shell): Promise<Errorable<string>> {
const onError = (sr: shell.ShellResult): Errorable<string> => {
if (sr.stderr && sr.stderr.trim().length > 0) {
return { succeeded: false, error: [sr.stderr] };
}
return { succeeded: false, error: [sr.stdout] }; // Because Microk8s prints permissions errors to stdout
};
return invokeObj(sh, `kubectl config view`, ` --raw`, {}, (s) => s, onError);
export function getKubeconfigYaml(): string {
const kubeconfigfile = '/var/snap/microk8s/current/credentials/client.config';
logChannel.appendLine(`Reading kubeconfig file at ${kubeconfigfile}`);
return fs.readFileSync(kubeconfigfile, 'utf8');
}
26 changes: 0 additions & 26 deletions src/utils/errorable.ts

This file was deleted.

73 changes: 0 additions & 73 deletions src/utils/shell.ts

This file was deleted.