import * as vscode from "vscode" ; import * as k8s from 'vscode-kubernetes-tools-api' ; import { logChannel } from "./log" ; export interface KubectlContext { readonly clusterName : string ; readonly contextName : string ; readonly userName : string ; } export async function deleteClusterFromKubeconfig ( context ) { const kubectl = await k8s . extension . kubectl . v1 ; if ( ! kubectl . available ) { vscode . window . showErrorMessage ( `Delete ${ context . contextName } failed: kubectl is not available. See Output window for more details.` ) ; return false ; } const deleteClusterResult = await kubectl . api . invokeCommand ( `config delete-cluster ${ context . clusterName }` ) ; if ( deleteClusterResult ?. code !== 0 ) { const whatFailed = `Failed to remove the underlying cluster for context ${ context . clusterName } from the kubeconfig: ${ deleteClusterResult ?. stderr }` ; logChannel . showOutput ( whatFailed ) ; vscode . window . showWarningMessage ( `Failed to remove the underlying cluster for context ${ context . contextName }. See Output window for more details.` ) ; } const deleteUserResult = await kubectl . api . invokeCommand ( `config unset users.${ context . userName }` ) ; if ( deleteUserResult ?. code !== 0 ) { const whatFailed = `Failed to remove the underlying user for context ${ context . contextName } from the kubeconfig: ${ deleteUserResult ?. stderr }` ; logChannel . showOutput ( whatFailed ) ; vscode . window . showWarningMessage ( `Failed to remove the underlying user for context ${ context . contextName }. See Output window for more details.` ) ; } const deleteContextResult = await kubectl . api . invokeCommand ( `config delete-context ${ context . contextName }` ) ; if ( deleteContextResult ?. code !== 0 ) { const whatFailed = `Failed to delete the specified cluster's context ${ context . contextName } from the kubeconfig: ${ deleteContextResult ?. stderr }` ; logChannel . showOutput ( whatFailed ) ; vscode . window . showErrorMessage ( `Delete ${ context . contextName } failed. See Output window for more details.` ) ; return false ; } vscode . window . showInformationMessage ( `Deleted context '${ context . contextName }' and associated data from the kubeconfig.` ) ; return true ; } export async function setContext ( contextName ) { const kubectl = await k8s . extension . kubectl . v1 ; if ( ! kubectl . available ) { vscode . window . showErrorMessage ( `Set context ${ contextName } failed: kubectl is not available. See Output window for more details.` ) ; return ; } const setContextResult = await kubectl . api . invokeCommand ( `config set-context ${ contextName }` ) ; if ( setContextResult ?. code !== 0 ) { const whatFailed = `Failed to set "${ contextName }": ${ setContextResult ?. stderr }` ; logChannel . showOutput ( whatFailed ) ; vscode . window . showErrorMessage ( `Count not set "${ contextName }" failed. See Output window for more details.` ) ; return ; } vscode . window . showInformationMessage ( `Switched to "${ contextName }".` ) ; } export async function getContext ( ) { const kubectl = await k8s . extension . kubectl . v1 ; if ( ! kubectl . available ) { vscode . window . showErrorMessage ( `Get context failed: kubectl is not available. See Output window for more details.` ) ; return "" ; } const setContextResult = await kubectl . api . invokeCommand ( `config current-context` ) ; if ( setContextResult ?. code !== 0 ) { const whatFailed = `Failed to get context: ${ setContextResult ?. stderr }` ; logChannel . showOutput ( whatFailed ) ; vscode . window . showErrorMessage ( `Could not get context. See Output window for more details.` ) ; return "" ; } const context = setContextResult . stdout . trim ( ) ; return context ; } export async function drain ( node ) { const kubectl = await k8s . extension . kubectl . v1 ; if ( ! kubectl . available ) { vscode . window . showErrorMessage ( `Drain ${ node } failed: kubectl is not available. See Output window for more details.` ) ; return "" ; } const drainResult = await kubectl . api . invokeCommand ( `drain ${ node }` ) ; if ( drainResult ?. code !== 0 ) { const whatFailed = `Failed to drain "${ node }": ${ drainResult ?. stderr }` ; logChannel . showOutput ( whatFailed ) ; vscode . window . showErrorMessage ( `Count not drain "${ node }" failed. See Output window for more details.` ) ; return "" ; } vscode . window . showInformationMessage ( `"${ node }" has been drained.` ) ; return drainResult . stdout ; } export async function getKubeconfigPath ( ) { const config = await k8s . extension . configuration . v1 ; if ( ! config . available ) { vscode . window . showErrorMessage ( `Kuebernetes config is not available. See Output window for more details.` ) ; return "" ; } const kubeconfig = config . api . getKubeconfigPath ( ) ; switch ( kubeconfig . pathType ) { case "wsl" : return kubeconfig . wslPath ; case "host" : return kubeconfig . hostPath ; } } export function getClusterForContext ( context ) { return context . substr ( "k3d-" . length ) ; } export function getContextForCluster ( cluster ) { return `k3d-${ cluster }` ; }