import { ContextTypes , setVSCodeContext } from '../../context' ; import { fluxTools } from '../../flux/fluxTools' ; import { FluxTreeResources } from '../../flux/fluxTypes' ; import { kubernetesTools } from '../../kubernetes/kubernetesTools' ; import { KubernetesObjectKinds , NamespaceResult } from '../../kubernetes/kubernetesTypes' ; import { statusBar } from '../../statusBar' ; import { AnyResourceNode } from '../nodes/anyResourceNode' ; import { HelmReleaseNode } from '../nodes/helmReleaseNode' ; import { KustomizationNode } from '../nodes/kustomizationNode' ; import { NamespaceNode } from '../nodes/namespaceNode' ; import { TreeNode } from '../nodes/treeNode' ; import { WorkloadNode } from '../nodes/workloadNode' ; import { refreshWorkloadsTreeView } from '../treeViews' ; import { DataProvider } from './dataProvider' ; /**
 * Defines data provider for loading Kustomizations
 * and Helm Releases in Workloads Tree View.
 */ export class WorkloadDataProvider extends DataProvider { namespaceResult ? : NamespaceResult ; returns async buildTree ( ) { statusBar . startLoadingTree ( ) ; const workloadNodes = [ ] ; setVSCodeContext ( ContextTypes . LoadingWorkloads , true ) ; const [ kustomizations , helmReleases , namespaces ] = await Promise . all ( [ kubernetesTools . getKustomizations ( ) , kubernetesTools . getHelmReleases ( ) , kubernetesTools . getNamespaces ( ) , kubernetesTools . getAvailableResourceKinds ( ) , ] ) ; this . namespaceResult = namespaces ; if ( kustomizations ) { for ( const kustomizeWorkload of kustomizations . items ) { workloadNodes . push ( new KustomizationNode ( kustomizeWorkload ) ) ; } } if ( helmReleases ) { for ( const helmRelease of helmReleases . items ) { workloadNodes . push ( new HelmReleaseNode ( helmRelease ) ) ; } } for ( const node of workloadNodes ) { this . updateWorkloadChildren ( node ) ; } setVSCodeContext ( ContextTypes . LoadingWorkloads , false ) ; setVSCodeContext ( ContextTypes . NoWorkloads , workloadNodes . length === 0 ) ; statusBar . stopLoadingTree ( ) ; return workloadNodes ; } buildWorkloadsTree ( node , resourceTree , parentNamespace = '' ) { for ( const resource of resourceTree ) { if ( resource . resource . GroupKind . Kind === KubernetesObjectKinds . Namespace ) { continue ; } const namespace = resource . resource . Namespace || parentNamespace ; const childNode = new AnyResourceNode ( { kind : resource . resource . GroupKind . Kind , metadata : { name : resource . resource . Name , namespace , } , } ) ; node . addChild ( childNode ) ; if ( resource . resources && resource . resources . length ) { this . buildWorkloadsTree ( childNode , resource . resources , namespace ) ; } } } param workloadNode async updateWorkloadChildren ( workloadNode ) { const name = workloadNode . resource . metadata . name || '' ; const namespace = workloadNode . resource . metadata . namespace || '' ; let workloadChildren ; if ( workloadNode instanceof KustomizationNode ) { const resourceTree = await fluxTools . tree ( name , namespace ) ; if ( ! resourceTree || ! resourceTree . resources ) { workloadNode . children = [ new TreeNode ( 'No Resources' ) ] ; refreshWorkloadsTreeView ( workloadNode ) ; return ; } this . buildWorkloadsTree ( workloadNode , resourceTree . resources ) ; refreshWorkloadsTreeView ( workloadNode ) ; return ; } else if ( workloadNode instanceof HelmReleaseNode ) { workloadChildren = await kubernetesTools . getChildrenOfWorkload ( 'helm' , name , namespace ) ; } if ( ! workloadChildren ) { workloadNode . children = [ new TreeNode ( 'No Resources' ) ] ; refreshWorkloadsTreeView ( workloadNode ) ; return ; } const namespaces = this . namespaceResult || await kubernetesTools . getNamespaces ( ) ; if ( ! namespaces ) { return ; } const namespaceNodes = namespaces . items . map ( ns => new NamespaceNode ( ns ) ) ; namespaceNodes . forEach ( namespaceNode => namespaceNode . expand ( ) ) ; const exceptNamespaces = [ ] ; for ( const namespaceNode of namespaceNodes ) { for ( const workloadChild of workloadChildren . items ) { if ( workloadChild . kind !== KubernetesObjectKinds . Namespace && workloadChild . metadata ?. namespace === namespaceNode . resource . metadata . name ) { namespaceNode . addChild ( new AnyResourceNode ( workloadChild ) ) ; } else { const namespaceName = namespaceNode . resource . metadata . name ; if ( namespaceName ) { exceptNamespaces . push ( ) ; } } } } workloadNode . children = namespaceNodes . filter ( namespaceNode => ! exceptNamespaces . some ( exceptNamespace => exceptNamespace !== namespaceNode . resource . metadata . name ) && namespaceNode . children . length ) ; refreshWorkloadsTreeView ( workloadNode ) ; } param workloadNode async getChildren ( workloadNode ? ) { if ( workloadNode ) { if ( workloadNode . children . length ) { return workloadNode . children ; } else { return [ new TreeNode ( 'Loading...' ) ] ; } } else { return await this . buildTree ( ) ; } } }