fix(manager:helmfile): remove go templating strings on a best effort basis for yaml parsing (#15106)

Signed-off-by: Jack Andersen <jandersen@plaid.com>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
jandersen-plaid 2022-04-22 00:44:46 -04:00 committed by GitHub
parent 173ab07013
commit 2af3717baf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 170 additions and 3 deletions

View file

@ -0,0 +1,39 @@
repositories:
- name: incubator
url: https://charts.helm.sh/incubator/
- name: bitnami
url: https://charts.bitnami.com/bitnami
- name: prometheus-community
url: https://prometheus-community.github.io/helm-charts
releases:
- name: "{{ requiredEnv \"RELEASE_NAME\" }}"
namespace: default
chart: ./foo
- name: {{ requiredEnv "RELEASE_NAME" }}
namespace: default
chart: ./foo
{{ if .Values.nginx.intranet.enabled }}
- name: nginx-intranet
chart: ingress-nginx/ingress-nginx
version: 3.37.0
{{ end }}
- name: example
{{- if neq .Values.example.version "" }}
version: {{ .Values.example.version }}
{{- else }}
version: 6.0.0
{{- end }}
chart: bitnami/memcached
- name: example-internal
version: 1.30.0
chart: stable/example
- name: example-private
version: {{ .Values.example.version }}
chart: prometheus-community/kube-prometheus-stack
- name: example-external
version: 1.48.0
chart: {{ .Values.example.repository }}
- name: example-public
version: 2.0.0
chart: stable/external-dns

View file

@ -116,7 +116,7 @@ Object {
"deps": Array [
Object {
"currentValue": "1.0.0",
"depName": "{{\`{{ .Release.Name }}\`}}",
"depName": "!!!!--!",
"registryUrls": Array [
"https://charts.helm.sh/stable",
],

View file

@ -68,7 +68,7 @@ describe('modules/manager/helmfile/extract', () => {
releases:
- name: example
version: 1.0.0
chart: stable/{{\`{{ .Release.Name }}\`}}
chart: stable/!!!!--!
- name: example-internal
version: 1.0.0
chart: stable/example
@ -202,5 +202,128 @@ describe('modules/manager/helmfile/extract', () => {
],
});
});
it('parses a chart with a go templating', () => {
const content = `
repositories:
- name: kiwigrid
url: https://kiwigrid.github.io
releases:
- name: example
{{- if neq .Values.example.version "" }}
version: {{ .Values.example.version }}
{{- else }}
version: 1.0.0
{{- end }}
chart: stable/example
- name: example-internal
version: 1.0.0
chart: stable/example
`;
const fileName = 'helmfile.yaml';
const result = extractPackageFile(content, fileName, {
aliases: {
stable: 'https://charts.helm.sh/stable',
},
});
expect(result).toMatchObject({
datasource: 'helm',
deps: [
{
currentValue: '1.0.0',
depName: 'example',
},
{
currentValue: '1.0.0',
depName: 'example',
},
],
});
});
it('parses a chart with empty strings for template values', () => {
const content = `
repositories:
- name: kiwigrid
url: https://kiwigrid.github.io
releases:
- name: example
version: {{ .Values.example.version }}
chart: stable/example
- name: example-external
version: 1.0.0
chart: {{ .Values.example.repository }}
- name: example-internal
version: 1.0.0
chart: stable/example
`;
const fileName = 'helmfile.yaml';
const result = extractPackageFile(content, fileName, {
aliases: {
stable: 'https://charts.helm.sh/stable',
},
});
expect(result).toMatchObject({
datasource: 'helm',
deps: [
{
skipReason: 'invalid-version',
},
{
skipReason: 'invalid-name',
},
{
currentValue: '1.0.0',
depName: 'example',
},
],
});
});
it('parses and replaces templating strings', () => {
const filename = 'helmfile.yaml';
const result = extractPackageFile(
Fixtures.get('go-template.yaml'),
filename,
{
aliases: {
stable: 'https://charts.helm.sh/stable',
},
}
);
expect(result).toMatchObject({
datasource: 'helm',
deps: [
{
depName: '{{ requiredEnv "RELEASE_NAME" }}',
skipReason: 'local-chart',
},
{ depName: null, skipReason: 'local-chart' },
{
depName: 'ingress-nginx',
currentValue: '3.37.0',
registryUrls: [],
skipReason: 'unknown-registry',
},
{
depName: 'memcached',
currentValue: '6.0.0',
registryUrls: ['https://charts.bitnami.com/bitnami'],
},
{
depName: 'example',
currentValue: '1.30.0',
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',
registryUrls: ['https://charts.helm.sh/stable'],
},
],
});
});
});
});

View file

@ -9,6 +9,11 @@ import type { Doc } from './types';
const isValidChartName = (name: string | undefined): boolean =>
!!name && !regEx(/[!@#$%^&*(),.?":{}/|<>A-Z]/).test(name);
function extractYaml(content: string): string {
// regex remove go templated ({{ . }}) values
return content.replace(/(^|:)\s*{{.+}}\s*$/gm, '$1');
}
export function extractPackageFile(
content: string,
fileName: string,
@ -18,7 +23,7 @@ export function extractPackageFile(
let docs: Doc[];
const aliases: Record<string, string> = {};
try {
docs = loadAll(content, null, { json: true }) as Doc[];
docs = loadAll(extractYaml(content), null, { json: true }) as Doc[];
} catch (err) {
logger.debug({ err, fileName }, 'Failed to parse helmfile helmfile.yaml');
return null;