Skip to content

Commit

Permalink
update current cluster treenode as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
juozasg committed Jul 21, 2023
1 parent 1bfffa1 commit b7b6c8c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/cli/checkVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CommandId } from 'types/extensionIds';
import { TelemetryError } from 'types/telemetryEventNames';
import { parseJson } from 'utils/jsonUtils';
import { shell, shellCodeError } from './shell/exec';
import { clusterDataProvider } from 'ui/treeviews/treeViews';

interface KubectlVersion {
major: string;
Expand Down Expand Up @@ -81,6 +82,7 @@ export async function getFluxVersion(): Promise<Errorable<string>> {

if (fluxVersionShellResult.code === 0) {
fluxVersion = parseJson(fluxVersionShellResult.stdout.trim()).flux;
clusterDataProvider.refreshCurrentNode();

return {
succeeded: true,
Expand Down
5 changes: 1 addition & 4 deletions src/commands/refreshTreeViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { refreshClustersTreeView, refreshSourcesTreeView, refreshTemplatesTreeVi

export async function refreshAllTreeViewsCommand() {
await syncKubeConfig(true);
// give proxy a chance to start
setTimeout(() => {
refreshAllTreeViews();
}, 100);
refreshAllTreeViews();
}

export async function refreshAllTreeViews() {
Expand Down
32 changes: 10 additions & 22 deletions src/ui/treeviews/dataProviders/clusterDataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ export class ClusterDataProvider extends DataProvider {
*/
private clusterNodes: ClusterNode[] = [];

public getCurrentClusterNode(): ClusterNode | undefined {
return this.clusterNodes.find(c => c.context.name === kubeConfig?.getCurrentContext());
}

public refreshCurrentNode() {
this.refresh(this.getCurrentClusterNode());
}

/**
* Check if the cluster node exists or not.
*/
Expand Down Expand Up @@ -69,7 +77,6 @@ export class ClusterDataProvider extends DataProvider {
for (const context of kubeConfig.getContexts()) {
const clusterNode = new ClusterNode(context);
if (context.name === kubeConfig.getCurrentContext()) {
clusterNode.isCurrent = true;
currentContextTreeItem = clusterNode;
clusterNode.makeCollapsible();
// load flux system deployments
Expand All @@ -88,9 +95,9 @@ export class ClusterDataProvider extends DataProvider {
}

// Update async status of the deployments (flux commands take a while to run)
currentContextTreeItem?.updateNodeContext();
this.updateDeploymentStatus(currentContextTreeItem);
// Update async cluster context/icons
// this.updateClusterContexts(clusterNodes);


statusBar.stopLoadingTree();
setVSCodeContext(ContextId.LoadingClusters, false);
Expand Down Expand Up @@ -132,24 +139,5 @@ export class ClusterDataProvider extends DataProvider {
}
}

/**
* Update cluster context for all cluster nodes one by one.
* @param clusterNodes all cluster nodes in this tree view.
*/
// TODO: FIXME: calling this is a bad idea with more than 10-100 contexts
async updateClusterContexts(clusterNodes: ClusterNode[]) {
await Promise.all(clusterNodes.map(async clusterNode => {
await clusterNode.updateNodeContext();
refreshClustersTreeView(clusterNode);
}));
}

/**
* Update cluster context for a single cluster node.
* @param clusterNode Usually the selected clusterNode.
*/
async updateClusterContext(clusterNode: ClusterNode) {
await clusterNode.updateNodeContext();
refreshClustersTreeView(clusterNode);
}
}
12 changes: 7 additions & 5 deletions src/ui/treeviews/nodes/cluster/clusterNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ClusterProvider } from 'types/kubernetes/clusterProvider';
import { NodeContext } from 'types/nodeContext';
import { createContextMarkdownTable, createMarkdownHr } from 'utils/markdownUtils';
import { TreeNode } from '../treeNode';
import { clusterDataProvider } from 'ui/treeviews/treeViews';

/**
* Defines Cluster context tree view item for displaying
Expand Down Expand Up @@ -41,11 +42,6 @@ export class ClusterNode extends TreeNode {
*/
public context: k8s.Context;

/**
* 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.
Expand Down Expand Up @@ -90,6 +86,12 @@ export class ClusterNode extends TreeNode {
} else {
this.setIcon('cloud');
}

clusterDataProvider.refresh(this);
}

get isCurrent(): boolean {
return this.context.name === kubeConfig.getCurrentContext();
}

get tooltip(): MarkdownString {
Expand Down

0 comments on commit b7b6c8c

Please sign in to comment.