-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #483 from weaveworks/toolkit-node
factor out toolkit node. loading spinners
- Loading branch information
Showing
14 changed files
with
134 additions
and
241 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,94 +1,15 @@ | ||
import { MarkdownString } from 'vscode'; | ||
|
||
import { Bucket } from 'types/flux/bucket'; | ||
import { GitRepository } from 'types/flux/gitRepository'; | ||
import { HelmRepository } from 'types/flux/helmRepository'; | ||
import { FluxSourceObject } from 'types/flux/object'; | ||
import { OCIRepository } from 'types/flux/ociRepository'; | ||
import { Condition } from 'types/kubernetes/kubernetesTypes'; | ||
import { createMarkdownError, createMarkdownHr, createMarkdownTable } from 'utils/markdownUtils'; | ||
import { shortenRevision } from 'utils/stringUtils'; | ||
import { TreeNode, TreeNodeIcon } from '../treeNode'; | ||
|
||
import { ToolkitNode } from './toolkitNode'; | ||
/** | ||
* Base class for all the Source tree view items. | ||
*/ | ||
export class SourceNode extends TreeNode { | ||
|
||
resource: FluxSourceObject; | ||
|
||
/** | ||
* Whether or not the source failed to reconcile. | ||
*/ | ||
isReconcileFailed = false; | ||
|
||
constructor(label: string, source: FluxSourceObject) { | ||
super(label); | ||
|
||
this.resource = source; | ||
|
||
// update reconciliation status | ||
this.updateStatus(); | ||
} | ||
|
||
get tooltip() { | ||
return this.getMarkdownHover(this.resource); | ||
} | ||
export class SourceNode extends ToolkitNode { | ||
resource!: FluxSourceObject; | ||
|
||
// @ts-ignore | ||
get description() { | ||
const isSuspendIcon = this.resource.spec?.suspend ? '⏸ ' : ''; | ||
let revisionOrError = ''; | ||
|
||
if (this.isReconcileFailed) { | ||
revisionOrError = `${this.findReadyOrFirstCondition(this.resource.status.conditions)?.reason || ''}`; | ||
} else { | ||
revisionOrError = shortenRevision(this.resource.status.artifact?.revision); | ||
} | ||
|
||
return `${isSuspendIcon}${revisionOrError}`; | ||
get revision() { | ||
return shortenRevision(this.resource.status.artifact?.revision); | ||
} | ||
|
||
/** | ||
* Creates markdwon string for Source tree view item tooltip. | ||
* @param source GitRepository, HelmRepository or Bucket kubernetes object. | ||
* @returns Markdown string to use for Source tree view item tooltip. | ||
*/ | ||
getMarkdownHover(source: GitRepository | OCIRepository | HelmRepository | Bucket): MarkdownString { | ||
const markdown: MarkdownString = createMarkdownTable(source); | ||
|
||
// show status in hover when source fetching failed | ||
if (this.isReconcileFailed) { | ||
const readyCondition = this.findReadyOrFirstCondition(source.status.conditions); | ||
createMarkdownHr(markdown); | ||
createMarkdownError('Status message', readyCondition?.message, markdown); | ||
createMarkdownError('Status reason', readyCondition?.reason, markdown); | ||
} | ||
|
||
return markdown; | ||
} | ||
|
||
/** | ||
* Find condition with the "Ready" type or | ||
* return first one if "Ready" not found. | ||
* | ||
* @param conditions "status.conditions" of the source | ||
*/ | ||
findReadyOrFirstCondition(conditions?: Condition[]): Condition | undefined { | ||
return conditions?.find(condition => condition.type === 'Ready') || conditions?.[0]; | ||
} | ||
|
||
/** | ||
* Update source status with showing error icon when fetch failed. | ||
* @param source target source | ||
*/ | ||
updateStatus(): void { | ||
if (this.findReadyOrFirstCondition(this.resource.status.conditions)?.status === 'True') { | ||
this.setIcon(TreeNodeIcon.Success); | ||
this.isReconcileFailed = false; | ||
} else { | ||
this.setIcon(TreeNodeIcon.Error); | ||
this.isReconcileFailed = true; | ||
} | ||
} | ||
} |
This file contains 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,92 @@ | ||
import { FluxObject } from 'types/flux/object'; | ||
import { Condition } from 'types/kubernetes/kubernetesTypes'; | ||
import { createMarkdownError, createMarkdownHr, createMarkdownTable } from 'utils/markdownUtils'; | ||
import { TreeNode, TreeNodeIcon } from '../treeNode'; | ||
|
||
export enum ReconcileState { | ||
Ready, | ||
Failed, | ||
Progressing, | ||
} | ||
|
||
export class ToolkitNode extends TreeNode { | ||
resource: FluxObject; | ||
reconcileState: ReconcileState = ReconcileState.Progressing; | ||
|
||
constructor(resource: FluxObject) { | ||
super(`${resource.kind}: ${resource.metadata?.name || 'unknown'}`); | ||
|
||
this.resource = resource; | ||
this.updateStatus(); | ||
} | ||
|
||
/** | ||
* Update status with showing error icon when fetch failed. | ||
* @param source target source | ||
*/ | ||
updateStatus() { | ||
const condition = this.readyOrFirstCondition; | ||
if (condition?.status === 'True') { | ||
this.reconcileState = ReconcileState.Ready; | ||
this.setIcon(TreeNodeIcon.Success); | ||
} else if (condition?.reason === 'Progressing') { | ||
this.reconcileState = ReconcileState.Progressing; | ||
this.setIcon(TreeNodeIcon.Progressing); | ||
} else { | ||
this.reconcileState = ReconcileState.Failed; | ||
this.setIcon(TreeNodeIcon.Error); | ||
} | ||
} | ||
|
||
/** | ||
* Find condition with the "Ready" type or | ||
* return first one if "Ready" not found. | ||
*/ | ||
get readyOrFirstCondition(): Condition | undefined { | ||
const conditions = this.resource.status.conditions; | ||
|
||
if (Array.isArray(conditions)) { | ||
return conditions.find(condition => condition.type === 'Ready') || conditions[0]; | ||
} else { | ||
return conditions; | ||
} | ||
} | ||
|
||
get tooltip() { | ||
const markdown = createMarkdownTable(this.resource); | ||
// show status in hoverwhat failed | ||
if (!this.resourceIsReady) { | ||
createMarkdownHr(markdown); | ||
createMarkdownError('Status message', this.readyOrFirstCondition?.message, markdown); | ||
createMarkdownError('Status reason', this.readyOrFirstCondition?.reason, markdown); | ||
} | ||
|
||
return markdown; | ||
} | ||
|
||
get resourceIsReady(): boolean { | ||
return this.reconcileState === ReconcileState.Ready; | ||
} | ||
|
||
get resourceIsProgressing(): boolean { | ||
return this.reconcileState === ReconcileState.Progressing; | ||
} | ||
|
||
// @ts-ignore | ||
get description() { | ||
const isSuspendIcon = this.resource.spec?.suspend ? '⏸ ' : ''; | ||
let revisionOrError = ''; | ||
|
||
if (!this.resourceIsReady) { | ||
revisionOrError = `${this.readyOrFirstCondition?.reason || ''}`; | ||
} else { | ||
revisionOrError = this.revision; | ||
} | ||
|
||
return `${isSuspendIcon}${revisionOrError}`; | ||
} | ||
|
||
get revision(): string { | ||
return 'unknown'; | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.