-
Notifications
You must be signed in to change notification settings - Fork 66
Feat: headlamp integration #1439
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
Draft
Tatsinnit
wants to merge
2
commits into
Azure:main
Choose a base branch
from
Tatsinnit:feature/poc-headlamp-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
src/commands/oservabilitytools/deployHeadlamp/deployHeadlamp.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import * as vscode from "vscode"; | ||
| import * as k8s from "vscode-kubernetes-tools-api"; | ||
| import { IActionContext } from "@microsoft/vscode-azext-utils"; | ||
| import * as tmpfile from "../../utils/tempfile"; | ||
| import { getAksClusterTreeNode, getKubeconfigYaml, getManagedCluster } from "../../utils/clusters"; | ||
| import { getExtension, longRunning } from "../../utils/host"; | ||
| import { AksClusterTreeNode } from "../../../tree/aksClusterTreeItem"; | ||
| import { Errorable, failed } from "../../utils/errorable"; | ||
| import { invokeKubectlCommand } from "../../utils/kubectl"; | ||
| import { getEnvironment, getReadySessionProvider } from "../../../auth/azureAuth"; | ||
| import { HeadlampPanel } from "../../../panels/HeadlampPanel"; | ||
| import { HeadlampPanelDataProvider } from "../../../panels/HeadlampPanel"; | ||
|
|
||
| export default async function deployHeadlamp(_context: IActionContext, target: unknown): Promise<void> { | ||
| const kubectl = await k8s.extension.kubectl.v1; | ||
| if (!kubectl.available) { | ||
| return; | ||
| } | ||
|
|
||
| const sessionProvider = await getReadySessionProvider(); | ||
| if (failed(sessionProvider)) { | ||
| vscode.window.showErrorMessage(sessionProvider.error); | ||
| return; | ||
| } | ||
|
|
||
| const extension = getExtension(); | ||
| if (failed(extension)) { | ||
| vscode.window.showErrorMessage(extension.error); | ||
| return; | ||
| } | ||
|
|
||
| const cloudExplorer = await k8s.extension.cloudExplorer.v1; | ||
|
|
||
| const clusterNode = getAksClusterTreeNode(target, cloudExplorer); | ||
| if (failed(clusterNode)) { | ||
| vscode.window.showErrorMessage(clusterNode.error); | ||
| return; | ||
| } | ||
|
|
||
| // Once Periscope will support usgov endpoints all we need is to remove this check. | ||
| // I have done background plumbing for vscode to fetch downlodable link from correct endpoint. | ||
| const cloudName = getEnvironment().name; | ||
| if (cloudName !== "AzureCloud") { | ||
| vscode.window.showInformationMessage(`Periscope is not supported in ${cloudName} cloud.`); | ||
| return; | ||
| } | ||
|
|
||
| const properties = await longRunning(`Getting properties for cluster ${clusterNode.result.name}.`, () => | ||
| getManagedCluster( | ||
| sessionProvider.result, | ||
| clusterNode.result.subscriptionId, | ||
| clusterNode.result.resourceGroupName, | ||
| clusterNode.result.name, | ||
| ), | ||
| ); | ||
| if (failed(properties)) { | ||
| vscode.window.showErrorMessage(properties.error); | ||
| return undefined; | ||
| } | ||
|
|
||
| const kubeconfig = await longRunning(`Retrieving kubeconfig for cluster ${clusterNode.result.name}.`, () => | ||
| getKubeconfigYaml( | ||
| sessionProvider.result, | ||
| clusterNode.result.subscriptionId, | ||
| clusterNode.result.resourceGroupName, | ||
| properties.result, | ||
| ), | ||
| ); | ||
|
|
||
| if (failed(kubeconfig)) { | ||
| vscode.window.showErrorMessage(kubeconfig.error); | ||
| return undefined; | ||
| } | ||
|
|
||
| const kubeConfigFile = await tmpfile.createTempFile(kubeconfig.result, "yaml"); | ||
|
|
||
| const panel = new HeadlampPanel(extension.result.extensionUri); | ||
| const dataProvider = new HeadlampPanelDataProvider(clusterNode.result.name, kubectl, kubeConfigFile.filePath); | ||
| panel.show(dataProvider); | ||
|
|
||
| // await longRunning(`Retrieving kubeconfig for cluster ${clusterNode.result.name}.`, () => | ||
| // deployHeadlampInCluster(kubectl, clusterNode.result, kubeconfig.result) | ||
| // ); | ||
| } | ||
|
|
||
| // below function isn't being used anymore - can maybe be removed unless refactor | ||
| void deployHeadlampInCluster; | ||
| async function deployHeadlampInCluster( | ||
| kubectl: k8s.APIAvailable<k8s.KubectlV1>, | ||
| clusterNode: AksClusterTreeNode, | ||
| clusterKubeConfig: string, | ||
| ): Promise<void> { | ||
| const clusterName = clusterNode.name; | ||
| console.log(`Deploying Headlamp in cluster ${clusterName}.`); | ||
|
|
||
| const sessionProvider = await getReadySessionProvider(); | ||
| if (failed(sessionProvider)) { | ||
| vscode.window.showErrorMessage(sessionProvider.error); | ||
| return; | ||
| } | ||
|
|
||
| const extension = getExtension(); | ||
| if (failed(extension)) { | ||
| vscode.window.showErrorMessage(extension.error); | ||
| 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 aks-periscope --ignore-not-found=true", | ||
| // ); | ||
| // if (failed(deleteResult)) return deleteResult; | ||
|
|
||
| // Deploy headlamp. | ||
| const applyResult = await invokeKubectlCommand( | ||
| kubectl, | ||
| kubeConfigFile, | ||
| `apply -k https://raw.githubusercontent.com/kinvolk/headlamp/main/kubernetes-headlamp-ingress-sample.yaml`, | ||
| ); | ||
| if (failed(applyResult)) return applyResult; | ||
|
|
||
| // kubectl port-forward -n kube-system service/headlamp 8080:80 | ||
| return invokeKubectlCommand( | ||
| kubectl, | ||
| kubeConfigFile, | ||
| " port-forward -n kube-system service/headlamp 8080:80", | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| // async function deployKustomizeOverlay( | ||
| // kubectl: k8s.APIAvailable<k8s.KubectlV1>, | ||
| // overlayDir: string, | ||
| // 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 aks-periscope --ignore-not-found=true", | ||
| // ); | ||
| // if (failed(deleteResult)) return deleteResult; | ||
|
|
||
| // // Deploy aks-periscope. | ||
| // const applyResult = await invokeKubectlCommand(kubectl, kubeConfigFile, `apply -k ${overlayDir}`); | ||
| // if (failed(applyResult)) return applyResult; | ||
|
|
||
| // return invokeKubectlCommand(kubectl, kubeConfigFile, "cluster-info"); | ||
| // }, | ||
| // ); | ||
| // } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import * as vscode from "vscode"; | ||
| import * as k8s from "vscode-kubernetes-tools-api"; | ||
| import { MessageHandler, MessageSink } from "../webview-contract/messaging"; | ||
| import { InitialState, ToVsCodeMsgDef, ToWebViewMsgDef } from "../webview-contract/webviewDefinitions/headlamp"; | ||
| import { TelemetryDefinition } from "../webview-contract/webviewTypes"; | ||
| import { BasePanel, PanelDataProvider } from "./BasePanel"; | ||
| import { invokeKubectlCommand } from "../commands/utils/kubectl"; | ||
| import { failed } from "../commands/utils/errorable"; | ||
| import { spawn, ChildProcess } from "child_process"; | ||
|
|
||
| export class HeadlampPanel extends BasePanel<"headlamp"> { | ||
| constructor(extensionUri: vscode.Uri) { | ||
| super(extensionUri, "headlamp", { | ||
| headlampUpdate: null, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| export class HeadlampPanelDataProvider implements PanelDataProvider<"headlamp"> { | ||
| public constructor( | ||
| readonly clusterName: string, | ||
| readonly kubectl: k8s.APIAvailable<k8s.KubectlV1>, | ||
| readonly kubeConfigFilePath: string, | ||
| ) { | ||
| this.clusterName = clusterName; | ||
| this.kubectl = kubectl; | ||
| this.kubeConfigFilePath = kubeConfigFilePath; | ||
| } | ||
| getTitle(): string { | ||
| return `Deploy Headlamp`; | ||
| } | ||
|
|
||
| getInitialState(): InitialState { | ||
| return { | ||
| deploymentStatus: "undeployed", | ||
| token: "", | ||
| }; | ||
| } | ||
| getTelemetryDefinition(): TelemetryDefinition<"headlamp"> { | ||
| return { | ||
| deployHeadlampRequest: true, | ||
| generateTokenRequest: true, | ||
| startPortForwardingRequest: true, | ||
| stopPortForwardingRequest: true, | ||
| }; | ||
| } | ||
| getMessageHandler(webview: MessageSink<ToWebViewMsgDef>): MessageHandler<ToVsCodeMsgDef> { | ||
| return { | ||
| deployHeadlampRequest: () => { | ||
| this.handleDeployHeadlampRequest(webview); | ||
| }, | ||
| generateTokenRequest: () => { | ||
| this.handleGenerateTokenRequest(webview); | ||
| }, | ||
| startPortForwardingRequest: () => { | ||
| this.startPortForwarding(); | ||
| }, | ||
| stopPortForwardingRequest: () => { | ||
| this.stopPortForwarding(); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| private portForwardProcess?: ChildProcess; | ||
|
|
||
| private async handleDeployHeadlampRequest(webview: MessageSink<ToWebViewMsgDef>) { | ||
| // deploy headlamp | ||
| const deploy = await invokeKubectlCommand( | ||
| this.kubectl, | ||
| this.kubeConfigFilePath, | ||
| "apply -f https://raw.githubusercontent.com/kinvolk/headlamp/main/kubernetes-headlamp.yaml", | ||
| ); | ||
| if (failed(deploy)) { | ||
| vscode.window.showErrorMessage(deploy.error); | ||
| console.log(deploy.error); | ||
| return; | ||
| } | ||
|
|
||
| // wait for headlamp pod to be ready | ||
| webview.postHeadlampUpdate({ deploymentStatus: "deploying", token: "" }); | ||
| const wait = await invokeKubectlCommand( | ||
| this.kubectl, | ||
| this.kubeConfigFilePath, | ||
| "wait --namespace kube-system --for=condition=Ready pod -l k8s-app=headlamp --timeout=90s", | ||
| ); | ||
| if (failed(wait)) { | ||
| vscode.window.showErrorMessage(wait.error); | ||
| return; | ||
| } | ||
|
|
||
| vscode.window.showInformationMessage("Headlamp deployed successfully!"); | ||
| webview.postHeadlampUpdate({ deploymentStatus: "deployed", token: "" }); | ||
| } | ||
|
|
||
| private async handleGenerateTokenRequest(webview: MessageSink<ToWebViewMsgDef>) { | ||
| // create service account | ||
| const createServiceAccount = await invokeKubectlCommand( | ||
| this.kubectl, | ||
| this.kubeConfigFilePath, | ||
| "-n kube-system create serviceaccount headlamp-admin", | ||
| ); | ||
| if (failed(createServiceAccount)) { | ||
| if (!createServiceAccount.error.includes("already exists")) { | ||
| vscode.window.showErrorMessage(createServiceAccount.error); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // create cluster role binding | ||
| const createClusterRoleBinding = await invokeKubectlCommand( | ||
| this.kubectl, | ||
| this.kubeConfigFilePath, | ||
| "create clusterrolebinding headlamp-admin --serviceaccount=kube-system:headlamp-admin --clusterrole=cluster-admin", | ||
| ); | ||
| if (failed(createClusterRoleBinding)) { | ||
| if (!createClusterRoleBinding.error.includes("already exists")) { | ||
| vscode.window.showErrorMessage(createClusterRoleBinding.error); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // create token | ||
| const createToken = await invokeKubectlCommand( | ||
| this.kubectl, | ||
| this.kubeConfigFilePath, | ||
| "create token headlamp-admin -n kube-system --duration 30m", | ||
| ); | ||
| if (failed(createToken)) { | ||
| vscode.window.showErrorMessage(createToken.error); | ||
| return; | ||
| } else { | ||
| const token = createToken.result.stdout.trim(); | ||
| console.log(token); | ||
| webview.postHeadlampUpdate({ deploymentStatus: "deployed", token: token }); | ||
| } | ||
| } | ||
|
|
||
| private startPortForwarding() { | ||
| if (this.portForwardProcess) { | ||
| vscode.window.showWarningMessage("Port forwarding already running."); | ||
| return; | ||
| } | ||
|
|
||
| this.portForwardProcess = spawn("kubectl", [ | ||
| "--kubeconfig", | ||
| this.kubeConfigFilePath, | ||
| "port-forward", | ||
| "-n", | ||
| "kube-system", | ||
| "service/headlamp", | ||
| "8080:80", | ||
| ]); | ||
|
|
||
| this.portForwardProcess.stdout?.on("data", (data) => { | ||
| console.log(`[port-forward] ${data}`); | ||
| }); | ||
|
|
||
| this.portForwardProcess.stderr?.on("data", (data) => { | ||
| console.error(`[port-forward error] ${data}`); | ||
| }); | ||
|
|
||
| this.portForwardProcess.on("exit", (code) => { | ||
| console.log(`Port forward exited with code ${code}`); | ||
| this.portForwardProcess = undefined; | ||
| }); | ||
| vscode.window.showInformationMessage("Port forwarding started."); | ||
| vscode.env.openExternal(vscode.Uri.parse("http://localhost:8080")); | ||
| } | ||
|
|
||
| private stopPortForwarding() { | ||
| if (this.portForwardProcess) { | ||
| this.portForwardProcess.kill(); | ||
| this.portForwardProcess = undefined; | ||
| vscode.window.showInformationMessage("Port forwarding stopped."); | ||
| } else { | ||
| vscode.window.showWarningMessage("No port forwarding process running."); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unused reference to 'deployHeadlampInCluster' can be removed to clean up the code. Consider deleting it and its associated function if it is no longer required.