Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filtering deprecated charts #3635

Merged
merged 4 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/common/k8s-api/endpoints/helm-charts.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import type { RequestInit } from "node-fetch";
import { autoBind, bifurcateArray } from "../../utils";
import Joi from "joi";

export type RepoHelmChartList = Record<string, HelmChart[]>;
export type HelmChartList = Record<string, RepoHelmChartList>;
export type RepoHelmChartList = Record<string, RawHelmChart[]>;

export interface IHelmChartDetails {
readme: string;
Expand All @@ -43,7 +42,7 @@ const endpoint = compile(`/v2/charts/:repo?/:name?`) as (params?: {
* Get a list of all helm charts from all saved helm repos
*/
export async function listCharts(): Promise<HelmChart[]> {
const data = await apiBase.get<HelmChartList>(endpoint());
const data = await apiBase.get<Record<string, RepoHelmChartList>>(endpoint());

return Object
.values(data)
Expand Down Expand Up @@ -311,11 +310,9 @@ export class HelmChart {
}

static create(data: RawHelmChart, { onError = "throw" }: HelmChartCreateOpts = {}): HelmChart | undefined {
const result = helmChartValidator.validate(data, {
const { value, error } = helmChartValidator.validate(data, {
abortEarly: false,
});
let { error } = result;
const { value } = result;

if (!error) {
return new HelmChart(value);
Expand All @@ -331,13 +328,13 @@ export class HelmChart {
return new HelmChart(value);
}

error = new Joi.ValidationError(actualErrors.map(er => er.message).join(". "), actualErrors, error._original);
const validationError = new Joi.ValidationError(actualErrors.map(er => er.message).join(". "), actualErrors, error._original);

if (onError === "throw") {
throw error;
throw validationError;
}

console.warn("[HELM-CHART]: failed to validate data", data, error);
console.warn("[HELM-CHART]: failed to validate data", data, validationError);

return undefined;
}
Expand Down
4 changes: 3 additions & 1 deletion src/common/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ const logLevel = process.env.LOG_LEVEL
? process.env.LOG_LEVEL
: isDebugging
? "debug"
: "info";
: isTestEnv
? "error"
: "info";

const transports: Transport[] = [
new SentryTransport("error")
Expand Down
34 changes: 33 additions & 1 deletion src/common/utils/sort-compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import semver, { SemVer } from "semver";
import semver, { coerce, SemVer } from "semver";
import * as iter from "./iter";
import type { RawHelmChart } from "../k8s-api/endpoints/helm-charts.api";
import logger from "../logger";

export function sortCompare<T>(left: T, right: T): -1 | 0 | 1 {
if (left < right) {
Expand Down Expand Up @@ -53,3 +56,32 @@ export function sortCompareChartVersions(left: ChartVersion, right: ChartVersion

return sortCompare(left.version, right.version);
}



export function sortCharts(charts: RawHelmChart[]) {
interface ExtendedHelmChart extends RawHelmChart {
__version: SemVer;
}

const chartsWithVersion = Array.from(
iter.map(
charts,
(chart => {
const __version = coerce(chart.version, { includePrerelease: true, loose: true });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the meaning to use underscores before variable name here? __version?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant it as both "temporary" and less likely to conflict with other fields. I chose __version because it is related to the version field but I don't want to override that field.


if (!__version) {
logger.warn(`[HELM-SERVICE]: Version from helm chart is not loosely coercable to semver.`, { name: chart.name, version: chart.version, repo: chart.repo });
}

(chart as ExtendedHelmChart).__version = __version;

return chart as ExtendedHelmChart;
})
),
);

return chartsWithVersion
.sort(sortCompareChartVersions)
.map(chart => (delete chart.__version, chart));
}
270 changes: 140 additions & 130 deletions src/main/helm/__mocks__/helm-chart-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,141 +19,151 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { HelmRepo, HelmRepoManager } from "../helm-repo-manager";
import { sortCharts } from "../../../common/utils";
import type { HelmRepo } from "../helm-repo-manager";

const charts = new Map([
["stable", {
"invalid-semver": sortCharts([
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "I am not semver",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "v4.3.0",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "I am not semver but more",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "v4.4.0",
repo: "stable",
digest: "test",
created: "now",
},
]),
"apm-server": sortCharts([
{
apiVersion: "3.0.0",
name: "apm-server",
version: "2.1.7",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "apm-server",
version: "2.1.6",
repo: "stable",
digest: "test",
created: "now",
}
]),
"redis": sortCharts([
{
apiVersion: "3.0.0",
name: "apm-server",
version: "1.0.0",
repo: "stable",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "apm-server",
version: "0.0.9",
repo: "stable",
digest: "test",
created: "now",
}
]),
}],
["experiment", {
"fairwind": sortCharts([
{
apiVersion: "3.0.0",
name: "fairwind",
version: "0.0.1",
repo: "experiment",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "fairwind",
version: "0.0.2",
repo: "experiment",
digest: "test",
deprecated: true,
created: "now",
}
]),
}],
["bitnami", {
"hotdog": sortCharts([
{
apiVersion: "3.0.0",
name: "hotdog",
version: "1.0.1",
repo: "bitnami",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "hotdog",
version: "1.0.2",
repo: "bitnami",
digest: "test",
created: "now",
}
]),
"pretzel": sortCharts([
{
apiVersion: "3.0.0",
name: "pretzel",
version: "1.0",
repo: "bitnami",
digest: "test",
created: "now",
},
{
apiVersion: "3.0.0",
name: "pretzel",
version: "1.0.1",
repo: "bitnami",
digest: "test",
created: "now",
}
]),
}],
]);

export class HelmChartManager {
cache: any = {};
private repo: HelmRepo;
constructor(private repo: HelmRepo){ }

constructor(repo: HelmRepo){
this.cache = HelmRepoManager.cache;
this.repo = repo;
static forRepo(repo: HelmRepo) {
return new this(repo);
}

public async charts(): Promise<any> {
switch (this.repo.name) {
case "stable":
return Promise.resolve({
"invalid-semver": [
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "I am not semver",
repo: "stable",
digest: "test"
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "v4.3.0",
repo: "stable",
digest: "test"
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "I am not semver but more",
repo: "stable",
digest: "test"
},
{
apiVersion: "3.0.0",
name: "weird-versioning",
version: "v4.4.0",
repo: "stable",
digest: "test"
},
],
"apm-server": [
{
apiVersion: "3.0.0",
name: "apm-server",
version: "2.1.7",
repo: "stable",
digest: "test"
},
{
apiVersion: "3.0.0",
name: "apm-server",
version: "2.1.6",
repo: "stable",
digest: "test"
}
],
"redis": [
{
apiVersion: "3.0.0",
name: "apm-server",
version: "1.0.0",
repo: "stable",
digest: "test"
},
{
apiVersion: "3.0.0",
name: "apm-server",
version: "0.0.9",
repo: "stable",
digest: "test"
}
]
});
case "experiment":
return Promise.resolve({
"fairwind": [
{
apiVersion: "3.0.0",
name: "fairwind",
version: "0.0.1",
repo: "experiment",
digest: "test"
},
{
apiVersion: "3.0.0",
name: "fairwind",
version: "0.0.2",
repo: "experiment",
digest: "test",
deprecated: true
}
]
});
case "bitnami":
return Promise.resolve({
"hotdog": [
{
apiVersion: "3.0.0",
name: "hotdog",
version: "1.0.1",
repo: "bitnami",
digest: "test"
},
{
apiVersion: "3.0.0",
name: "hotdog",
version: "1.0.2",
repo: "bitnami",
digest: "test",
}
],
"pretzel": [
{
apiVersion: "3.0.0",
name: "pretzel",
version: "1.0",
repo: "bitnami",
digest: "test",
},
{
apiVersion: "3.0.0",
name: "pretzel",
version: "1.0.1",
repo: "bitnami",
digest: "test"
}
]
});
default:
return Promise.resolve({});
}
return charts.get(this.repo.name) ?? {};
}
}
Loading