Skip to content

Commit

Permalink
Add EraserTool deploy for cluster. (Azure#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatsinnit authored Jan 3, 2024
1 parent 58384bd commit 31fcfb0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@
"command": "aks.periscope",
"title": "Run AKS Periscope"
},
{
"command": "aks.eraserTool",
"title": "Run Eraser Image Cleanup"
},
{
"command": "aks.installAzureServiceOperator",
"title": "Install Azure Service Operator"
Expand Down Expand Up @@ -289,6 +293,11 @@
"when": "view == kubernetes.cloudExplorer && viewItem =~ /aks\\.cluster/i",
"group": "8@2"
},
{
"command": "aks.eraserTool",
"when": "view == kubernetes.cloudExplorer && viewItem =~ /aks\\.cluster/i || view == extension.vsKubernetesExplorer && viewItem =~ /vsKubernetes\\.\\w*cluster$/i",
"group": "8@3"
},
{
"command": "aks.showInPortal",
"when": "view == kubernetes.cloudExplorer && viewItem =~ /aks\\.cluster/i",
Expand Down
83 changes: 83 additions & 0 deletions src/commands/aksEraserTool/erasertool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import * as vscode from "vscode";
import * as k8s from "vscode-kubernetes-tools-api";
import { IActionContext } from "@microsoft/vscode-azext-utils";
import { getKubernetesClusterInfo } from "../utils/clusters";
import { Errorable, failed, succeeded } from "../utils/errorable";
import { longRunning } from "../utils/host";
import { invokeKubectlCommand } from "../utils/kubectl";
import * as tmpfile from "../utils/tempfile";


export default async function aksEraserTool(_context: IActionContext, target: unknown): Promise<void> {
const kubectl = await k8s.extension.kubectl.v1;
const cloudExplorer = await k8s.extension.cloudExplorer.v1;
const clusterExplorer = await k8s.extension.clusterExplorer.v1;

if (!kubectl.available) {
vscode.window.showWarningMessage(`Kubectl is unavailable.`);
return;
}

if (!cloudExplorer.available) {
vscode.window.showWarningMessage(`Cloud explorer is unavailable.`);
return;
}

if (!clusterExplorer.available) {
vscode.window.showWarningMessage(`Cluster explorer is unavailable.`);
return;
}

const clusterInfo = await getKubernetesClusterInfo(target, cloudExplorer, clusterExplorer);
if (failed(clusterInfo)) {
vscode.window.showErrorMessage(clusterInfo.error);
return;
}

const clusterName = clusterInfo.result.name;

const answer = await vscode.window.showInformationMessage(
`Do you want to deploy Eraser tool for cluster ${clusterName} to handle [Automatic Cleaning Image](https://eraser-dev.github.io/eraser/docs/quick-start#automatically-cleaning-images)?`,
"Yes",
"No",
);

if (answer === "Yes") {
const result = await longRunning(`Deploying Eraser on cluster ${clusterName}.`, async () => {
return await deployEraserAutomaticInstallationScenario(kubectl, clusterInfo.result.kubeconfigYaml);
});

if (failed(result)) {
vscode.window.showErrorMessage(result.error);
}

if (succeeded(result)) {
vscode.window.showInformationMessage(`Eraser tool is successfully deployed into cluster ${clusterName}. \n Output: \n ${result.result.stdout}`);
}
}
}

async function deployEraserAutomaticInstallationScenario(
kubectl: k8s.APIAvailable<k8s.KubectlV1>,
clusterKubeConfig: string,
): Promise<Errorable<k8s.KubectlV1.ShellResult>> {
return await tmpfile.withOptionalTempFile<Errorable<k8s.KubectlV1.ShellResult>>(
clusterKubeConfig,
"YAML",
async (kubeConfigFile) => {
// Clean up running instance (without an error if it doesn't yet exist).
const deleteResult = await invokeKubectlCommand(
kubectl,
kubeConfigFile,
"delete ns eraser-system --ignore-not-found=true",
);
if (failed(deleteResult)) return deleteResult;

// Deploy eraser tool: https://eraser-dev.github.io/eraser/docs/installation
const applyResult = await invokeKubectlCommand(kubectl, kubeConfigFile, `apply -f https://raw.githubusercontent.com/eraser-dev/eraser/v1.2.0/deploy/eraser.yaml`);
if (failed(applyResult)) return applyResult;

return applyResult;
},
);
}
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import aksReconcileCluster from "./commands/aksReconcileCluster/aksReconcileClus
import { aksTCPDump } from "./commands/aksTCPCollection/tcpDumpCollection";
import aksCompareCluster from "./commands/aksCompareCluster/aksCompareCluster";
import refreshSubscription from "./commands/refreshSubscriptions";
import aksEraserTool from "./commands/aksEraserTool/erasertool";

export async function activate(context: vscode.ExtensionContext) {
const cloudExplorer = await k8s.extension.cloudExplorer.v1;
Expand Down Expand Up @@ -93,6 +94,7 @@ export async function activate(context: vscode.ExtensionContext) {
registerCommandWithTelemetry("aks.aksTCPDump", aksTCPDump);
registerCommandWithTelemetry("aks.compareCluster", aksCompareCluster);
registerCommandWithTelemetry("aks.refreshSubscription", refreshSubscription);
registerCommandWithTelemetry("aks.eraserTool", aksEraserTool);

await registerAzureServiceNodes(context);

Expand Down

0 comments on commit 31fcfb0

Please sign in to comment.