Skip to content

Commit

Permalink
refactor(manager/helmfile): use schema for yaml parsing (#27539)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
secustor and viceice committed Mar 15, 2024
1 parent 5075318 commit d0878d9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 84 deletions.
44 changes: 0 additions & 44 deletions lib/modules/manager/helmfile/__snapshots__/extract.spec.ts.snap
@@ -1,49 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`modules/manager/helmfile/extract extractPackageFile() parses multidoc yaml 1`] = `
{
"datasource": "helm",
"deps": [
{
"depName": "manifests",
"skipReason": "local-chart",
},
{
"currentValue": "7.4.3",
"depName": "rabbitmq",
"registryUrls": [
"https://charts.bitnami.com/bitnami",
],
},
{
"currentValue": "13.7",
"depName": "kube-prometheus-stack",
"registryUrls": [
"https://prometheus-community.github.io/helm-charts",
],
},
{
"depName": "invalid",
"skipReason": "invalid-name",
},
{
"depName": "external-dns",
"skipReason": "invalid-version",
},
{
"currentValue": "0.1.0",
"depName": "raw",
"registryUrls": [
"https://charts.helm.sh/incubator/",
],
},
],
"managerData": {
"needKustomize": true,
},
}
`;

exports[`modules/manager/helmfile/extract extractPackageFile() skip chart that does not have specified version 1`] = `
{
"datasource": "helm",
Expand Down
31 changes: 18 additions & 13 deletions lib/modules/manager/helmfile/extract.spec.ts
Expand Up @@ -212,15 +212,28 @@ describe('modules/manager/helmfile/extract', () => {
},
},
);
expect(result).toMatchSnapshot({
expect(result).toMatchObject({
datasource: 'helm',
deps: [
{ depName: 'manifests', skipReason: 'local-chart' },
{ depName: 'rabbitmq', currentValue: '7.4.3' },
{ depName: 'kube-prometheus-stack', currentValue: '13.7' },
{ depName: 'invalid', skipReason: 'invalid-name' },
{
depName: 'rabbitmq',
currentValue: '7.4.3',
registryUrls: ['https://charts.bitnami.com/bitnami'],
},
{
depName: 'kube-prometheus-stack',
currentValue: '13.7',
registryUrls: [
'https://prometheus-community.github.io/helm-charts',
],
},
{ depName: 'external-dns', skipReason: 'invalid-version' },
{ depName: 'raw' },
{
depName: 'raw',
currentValue: '0.1.0',
registryUrls: ['https://charts.helm.sh/incubator/'],
},
],
managerData: { needKustomize: true },
});
Expand Down Expand Up @@ -292,9 +305,6 @@ describe('modules/manager/helmfile/extract', () => {
{
skipReason: 'invalid-version',
},
{
skipReason: 'invalid-name',
},
{
currentValue: '1.0.0',
depName: 'example',
Expand Down Expand Up @@ -380,10 +390,6 @@ describe('modules/manager/helmfile/extract', () => {
depName: '',
skipReason: 'local-chart',
},
{
depName: null,
skipReason: 'local-chart',
},
{
depName: 'ingress-nginx',
currentValue: '3.37.0',
Expand All @@ -401,7 +407,6 @@ describe('modules/manager/helmfile/extract', () => {
registryUrls: ['https://charts.helm.sh/stable'],
},
{ depName: 'kube-prometheus-stack', skipReason: 'invalid-version' },
{ depName: 'example-external', skipReason: 'invalid-name' },
{
depName: 'external-dns',
currentValue: '2.0.0',
Expand Down
28 changes: 5 additions & 23 deletions lib/modules/manager/helmfile/extract.ts
@@ -1,5 +1,6 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { coerceArray } from '../../../util/array';
import { regEx } from '../../../util/regex';
import { parseYaml } from '../../../util/yaml';
import { DockerDatasource } from '../../datasource/docker';
Expand All @@ -10,6 +11,7 @@ import type {
PackageFileContent,
} from '../types';
import type { Doc } from './schema';
import { Doc as documentSchema } from './schema';
import {
kustomizationsKeysUsed,
localChartHasKustomizationsYaml,
Expand Down Expand Up @@ -39,8 +41,9 @@ export async function extractPackageFile(
// Record kustomization usage for all deps, since updating artifacts is run on the helmfile.yaml as a whole.
let needKustomize = false;
try {
// TODO: use schema (#9610)
docs = parseYaml(content, null, {
customSchema: documentSchema,
failureBehaviour: 'filter',
removeTemplates: true,
json: true,
});
Expand All @@ -52,10 +55,6 @@ export async function extractPackageFile(
return null;
}
for (const doc of docs) {
if (!doc) {
continue;
}

// Always check for repositories in the current document and override the existing ones if any (as YAML does)
if (doc.repositories) {
registryAliases = {};
Expand All @@ -68,23 +67,10 @@ export async function extractPackageFile(
);
}

// Skip extraction if the document contains no releases
if (!is.array(doc.releases)) {
continue;
}

for (const dep of doc.releases) {
for (const dep of coerceArray(doc.releases)) {
let depName = dep.chart;
let repoName: string | null = null;

if (!is.string(dep.chart)) {
deps.push({
depName: dep.name,
skipReason: 'invalid-name',
});
continue;
}

// If it starts with ./ ../ or / then it's a local path
if (isLocalPath(dep.chart)) {
if (
Expand All @@ -100,10 +86,6 @@ export async function extractPackageFile(
continue;
}

if (is.number(dep.version)) {
dep.version = String(dep.version);
}

if (isOciUrl(dep.chart)) {
const v = dep.chart.substring(6).split('/');
depName = v.pop()!;
Expand Down
13 changes: 9 additions & 4 deletions lib/modules/manager/helmfile/schema.ts
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { Yaml } from '../../../util/schema-utils';
import { LooseArray, Yaml } from '../../../util/schema-utils';

export const HelmRepository = z.object({
name: z.string(),
Expand All @@ -11,16 +11,21 @@ export type HelmRepository = z.infer<typeof HelmRepository>;
export const HelmRelease = z.object({
name: z.string(),
chart: z.string(),
version: z.string(),
version: z
.string()
.or(z.number())
.optional()
.nullable()
.transform((version) => (version ? version.toString() : null)),
strategicMergePatches: z.unknown().optional(),
jsonPatches: z.unknown().optional(),
transformers: z.unknown().optional(),
});
export type HelmRelease = z.infer<typeof HelmRelease>;

export const Doc = z.object({
releases: z.array(HelmRelease).optional(),
repositories: z.array(HelmRepository).optional(),
releases: LooseArray(HelmRelease).optional(),
repositories: LooseArray(HelmRepository).optional(),
});
export type Doc = z.infer<typeof Doc>;

Expand Down

0 comments on commit d0878d9

Please sign in to comment.