From d01fb04964334c2fb0ff2c74468137a279534d22 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Thu, 18 Feb 2021 10:25:13 +0300 Subject: [PATCH] Adding sorting charts by version Signed-off-by: Alex Andreev --- src/main/helm/helm-chart-manager.ts | 2 +- src/main/helm/helm-service.ts | 37 +++++++++++++++-------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/main/helm/helm-chart-manager.ts b/src/main/helm/helm-chart-manager.ts index ee11b980d3..69619a56d4 100644 --- a/src/main/helm/helm-chart-manager.ts +++ b/src/main/helm/helm-chart-manager.ts @@ -31,7 +31,7 @@ export class HelmChartManager { return cachedYaml["entries"]; } catch(error) { - logger.error(error); + logger.error("HELM-CHART-MANAGER]: failed to list charts", { error }); return {}; } diff --git a/src/main/helm/helm-service.ts b/src/main/helm/helm-service.ts index 8e388a6e12..5abbfc3be5 100644 --- a/src/main/helm/helm-service.ts +++ b/src/main/helm/helm-service.ts @@ -1,11 +1,10 @@ +import semver from "semver"; import { Cluster } from "../cluster"; import logger from "../logger"; import { repoManager } from "./helm-repo-manager"; import { HelmChartManager } from "./helm-chart-manager"; import { releaseManager } from "./helm-release-manager"; -import { HelmChart } from "../../renderer/api/endpoints/helm-charts.api"; - -import type { HelmChartList, RepoHelmChartList } from "../../renderer/api/endpoints/helm-charts.api"; +import { HelmChartList, RepoHelmChartList } from "../../renderer/api/endpoints/helm-charts.api"; class HelmService { public async installChart(cluster: Cluster, data: { chart: string; values: {}; name: string; namespace: string; version: string }) { @@ -21,9 +20,10 @@ class HelmService { for (const repo of repositories) { charts[repo.name] = {}; const manager = new HelmChartManager(repo); - const { groups } = new HelmChartGroups(await manager.charts()); + const groups = this.excludeDeprecatedChartGroups(await manager.charts()); + const sortedGroups = this.sortChartsByVersion(groups); - charts[repo.name] = groups; + charts[repo.name] = sortedGroups; } return charts; @@ -93,26 +93,27 @@ class HelmService { return { message: output }; } -} -class HelmChartGroups { - private items: Map; + private excludeDeprecatedChartGroups(chartGroups: RepoHelmChartList) { + const groups = new Map(Object.entries(chartGroups)); - constructor(groups: RepoHelmChartList) { - this.items = new Map(Object.entries(groups)); - this.excludeDeprecatedGroups(); - } - - private excludeDeprecatedGroups() { - for (const [chartName, charts] of this.items) { + for (const [chartName, charts] of groups) { if (charts[0].deprecated) { - this.items.delete(chartName); + groups.delete(chartName); } } + + return Object.fromEntries(groups); } - get groups() { - return Object.fromEntries(this.items); + private sortChartsByVersion(chartGroups: RepoHelmChartList) { + for (const key in chartGroups) { + chartGroups[key] = chartGroups[key].sort((first, second) => { + return semver.compare(second.version, first.version); + }); + } + + return chartGroups; } }