import { MarkdownString , window } from 'vscode' ; import { CommandId } from '../../commands' ; import { ContextTypes , setVSCodeContext } from '../../context' ; import { extensionState } from '../../extensionState' ; import { globalState } from '../../globalState' ; import { KubernetesCluster , KubernetesContextWithCluster } from '../../kubernetes/kubernetesConfig' ; import { kubernetesTools } from '../../kubernetes/kubernetesTools' ; import { ClusterProvider } from '../../kubernetes/kubernetesTypes' ; import { createMarkdownHr , createMarkdownTable } from '../../utils/markdownUtils' ; import { NodeContext } from './nodeContext' ; import { TreeNode } from './treeNode' ; /**
 * Defines Cluster tree view item for displaying
 * kubernetes contexts inside the Clusters tree view.
 */ export class ClusterContextNode extends TreeNode { /**
     * Whether cluster is managed by AKS or Azure ARC
     * or some other provider.
     */ private clusterProvider : ClusterProvider = ClusterProvider . Unknown ; /**
     * User used "Set Cluster Provider" context menu item
     * to override the cluster provider detection.
     */ private clusterProviderManuallyOverridden = false ; /**
     * Cluster object.
     */ private cluster ? : KubernetesCluster ; /**
     * Cluster context.
     */ private clusterContext : KubernetesContextWithCluster ; /**
     * Context name.
     */ contextName : string ; /**
     * Cluster name.
     */ clusterName : string ; /**
     * Current/active cluster/context.
     */ isCurrent = false ; /**
     * Whether or not gitops is installed on this cluster.
     * `undefined` when it's not yet initialized or when detection failed.
     */ isGitOpsEnabled ? : boolean ; param kubernetesContext constructor ( kubernetesContext ) { super ( kubernetesContext . name ) ; this . cluster = kubernetesContext . context . clusterInfo ; this . clusterContext = kubernetesContext ; this . clusterName = kubernetesContext . name ; this . contextName = kubernetesContext . name ; this . description = kubernetesContext . context . clusterInfo ?. cluster . server ; this . setIcon ( 'cloud' ) ; } get command ( ) { return { command : CommandId . SetCurrentKubernetesContext , arguments : [ this . contextName ] , title : 'Set current context' , } ; } /**
     * Set context/icon and refresh the node:
     * - Whether or not GitOps is enabled
     * - Cluster provider.
     */ async updateNodeContext ( ) { this . isGitOpsEnabled = await kubernetesTools . isGitOpsEnabled ( this . contextName ) ; const clusterMetadata = globalState . getClusterMetadata ( this . clusterName ) ; if ( clusterMetadata ?. clusterProvider ) { this . clusterProviderManuallyOverridden = true ; } this . clusterProvider = clusterMetadata ?. clusterProvider || await kubernetesTools . detectClusterProvider ( this . contextName ) ; if ( this . isCurrent && typeof this . isGitOpsEnabled === 'boolean' ) { setVSCodeContext ( ContextTypes . CurrentClusterGitOpsNotEnabled , ! this . isGitOpsEnabled ) ; } if ( this . isGitOpsEnabled ) { this . setIcon ( 'cloud-gitops' ) ; } else { this . setIcon ( 'cloud' ) ; } } get tooltip ( ) : MarkdownString { return this . getMarkdownHover ( this . clusterContext ) ; } param cluster getMarkdownHover ( cluster ) { const markdown = createMarkdownTable ( cluster ) ; createMarkdownHr ( markdown ) ; markdown . appendMarkdown ( `Flux Version: ${ extensionState . get ( 'fluxVersion' ) }` ) ; if ( this . clusterProvider !== ClusterProvider . Generic || this . clusterProviderManuallyOverridden ) { createMarkdownHr ( markdown ) ; markdown . appendMarkdown ( `Cluster Provider: ${ this . clusterProvider || ClusterProvider . Unknown }` ) ; if ( this . clusterProviderManuallyOverridden ) { markdown . appendMarkdown ( ' <span title="Use context menu on the cluster `Set Cluster Provider` to change.">(User override)</span>' ) ; } } return markdown ; } /**
     * Return this cluster provider.
     */ async getClusterProvider ( ) { if ( this . clusterProvider === ClusterProvider . Unknown ) { this . clusterProvider = await kubernetesTools . detectClusterProvider ( this . contextName ) ; } if ( this . clusterProvider === ClusterProvider . Unknown ) { window . showErrorMessage ( 'Cluster provider detection failed.' ) ; } return this . clusterProvider ; } get contexts ( ) { const result = [ NodeContext . Cluster ] ; if ( typeof this . isGitOpsEnabled === 'boolean' ) { result . push ( this . isGitOpsEnabled ? NodeContext . ClusterGitOpsEnabled : NodeContext . ClusterGitOpsNotEnabled ) ; } return result ; } }