Skip to content

Commit

Permalink
Adding status icons for Helm release status (vscode-kubernetes-tools#318
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Shreyas Karnik authored and itowlson committed Jul 19, 2018
1 parent 348dca8 commit e58609d
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 33 deletions.
1 change: 1 addition & 0 deletions images/helmDeployed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/helmFailed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 11 additions & 2 deletions src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export function createKubernetesResource(kind: kuberesources.ResourceKind, id: s
return new KubernetesResource(kind, id, metadata);
}

function getIconForHelmRelease(status: string): vscode.Uri {
if (status === "deployed") {
return vscode.Uri.file(path.join(__dirname, "../../images/helmDeployed.svg"));
} else {
return vscode.Uri.file(path.join(__dirname, "../../images/helmFailed.svg"));
}
}

function getIconForPodStatus(status: string): vscode.Uri {
if (status === "running" || status === "completed") {
return vscode.Uri.file(path.join(__dirname, "../../images/runningPod.svg"));
Expand Down Expand Up @@ -417,7 +425,7 @@ export class KubernetesFileObject implements KubernetesObject {
class HelmReleaseResource implements KubernetesObject {
readonly id: string;

constructor(readonly name: string) {
constructor(readonly name: string, readonly status: string) {
this.id = "helmrelease:" + name;
}

Expand All @@ -433,6 +441,7 @@ class HelmReleaseResource implements KubernetesObject {
arguments: [this]
};
treeItem.contextValue = "vsKubernetes.helmRelease";
treeItem.iconPath = getIconForHelmRelease(this.status.toLowerCase());
return treeItem;
}
}
Expand All @@ -455,6 +464,6 @@ class HelmReleasesFolder extends KubernetesFolder {
return [new DummyObject("Helm list error", releases.error[0])];
}

return releases.result.map((r) => new HelmReleaseResource(r));
return releases.result.map((r) => new HelmReleaseResource(r.name, r.status));
}
}
12 changes: 6 additions & 6 deletions src/helm.exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { showWorkspaceFolderPick } from './hostutils';
import { shell as sh, ShellResult } from './shell';
import { K8S_RESOURCE_SCHEME, HELM_RESOURCE_AUTHORITY } from './kuberesources.virtualfs';
import { Errorable } from './errorable';
import { parseLineOutput } from './outputUtils';

export interface PickChartUIOptions {
readonly warnIfNoCharts: boolean;
Expand Down Expand Up @@ -292,12 +293,12 @@ export async function helmExecAsync(args: string): Promise<ShellResult> {

const HELM_PAGING_PREFIX = "next:";

export async function helmListAll(namespace?: string): Promise<Errorable<string[]>> {
export async function helmListAll(namespace?: string): Promise<Errorable<{ [key: string]: string }[]>> {
if (!ensureHelm(EnsureMode.Alert)) {
return { succeeded: false, error: [ "Helm client is not installed" ] };
}

const releases: string[] = [];
const releases: {[key: string]: string}[] = [];
let offset: string | null = null;

do {
Expand All @@ -321,9 +322,8 @@ export async function helmListAll(namespace?: string): Promise<Errorable<string[
}
}
if (lines.length > 0) {
lines.shift(); // remove the header line
const names = lines.map((l) => l.split('\t')[0].trim());
releases.push(...names);
const helmReleases = parseLineOutput(lines, helm.HELM_OUTPUT_COLUMN_SEPARATOR);
releases.push(...helmReleases);
}
} while (offset !== null);

Expand Down Expand Up @@ -427,4 +427,4 @@ export function searchForChart(name: string, version?: string): Requirement {
export function helmHome(): string {
const h = process.env.HOME;
return process.env["HELM_HOME"] || filepath.join(h, '.helm');
}
}
3 changes: 2 additions & 1 deletion src/helm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const PREVIEW_SCHEME = 'helm-template-preview';
export const PREVIEW_URI = PREVIEW_SCHEME + '://preview';
export const INSPECT_SCHEME = 'helm-inspect-values';
export const HELM_OUTPUT_COLUMN_SEPARATOR = /\t+/g;

let previewShown = false;

Expand All @@ -10,4 +11,4 @@ export function hasPreviewBeenShown(): boolean {

export function recordPreviewHasBeenShown(): void {
previewShown = true;
}
}
6 changes: 4 additions & 2 deletions src/kubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import { FS } from './fs';
import { Shell, ShellHandler, ShellResult } from './shell';
import * as binutil from './binutil';
import { Errorable } from './errorable';
import { parseLineOutput } from './kubectlUtils';
import { parseLineOutput } from './outputUtils';

const KUBECTL_OUTPUT_COLUMN_SEPARATOR = /\s+/g;

export interface Kubectl {
checkPresent(errorMessageMode: CheckPresentMessageMode): Promise<boolean>;
Expand Down Expand Up @@ -237,7 +239,7 @@ async function fromLines(context: Context, command: string): Promise<Errorable<{
if (shellResult.code === 0) {
let lines = shellResult.stdout.split('\n');
lines = lines.filter((l) => l.length > 0);
const parsedOutput = parseLineOutput(lines);
const parsedOutput = parseLineOutput(lines, KUBECTL_OUTPUT_COLUMN_SEPARATOR);
return { succeeded: true, result: parsedOutput };
}
return { succeeded: false, error: [ shellResult.stderr ] };
Expand Down
22 changes: 0 additions & 22 deletions src/kubectlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,25 +334,3 @@ export async function getResourceAsJson<T extends KubernetesResource | Kubernete
}
return shellResult.result;
}

/**
* Parse column based output which is seperated by whitespace(s) from kubectl or similar sources
* for example, kubectl get po
* @param lineOutput raw output with headers from kubectl or similar sources
* @return array of objects with key as column header and value
*/
export function parseLineOutput(lineOutput: string[]): { [key: string]: string }[] {
const headers = lineOutput.shift();
if (!headers) {
return [];
}
const parsedHeaders = headers.toLowerCase().replace(/\s+/g, '|').split('|');
return lineOutput.map((line) => {
const lineInfoObject = {};
const bits = line.replace(/\s+/g, '|').split('|');
bits.forEach((columnValue, index) => {
lineInfoObject[parsedHeaders[index]] = columnValue;
});
return lineInfoObject;
});
}
22 changes: 22 additions & 0 deletions src/outputUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Parse column based output which is seperated by whitespace(s) from kubectl or similar sources
* for example, kubectl get po
* @param lineOutput raw output with headers from kubectl or similar sources
* @param columnSeparator a regex for the column separators
* @return array of objects with key as column header and value
*/
export function parseLineOutput(lineOutput: string[], columnSeparator: RegExp): { [key: string]: string }[] {
const headers = lineOutput.shift();
if (!headers) {
return [];
}
const parsedHeaders = headers.toLowerCase().replace(columnSeparator, '|').split('|');
return lineOutput.map((line) => {
const lineInfoObject = {};
const bits = line.replace(columnSeparator, '|').split('|');
bits.forEach((columnValue, index) => {
lineInfoObject[parsedHeaders[index].trim()] = columnValue.trim();
});
return lineInfoObject;
});
}

0 comments on commit e58609d

Please sign in to comment.