From 0538a4c96d3677a553970de0aeb1886b393d54b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nikolai=20R=C3=B8ed=20Kristiansen?= <nikolai.kristiansen@remarkable.no> Date: Mon, 24 Apr 2023 10:22:52 +0200 Subject: [PATCH] fix(manager/helmfile): Use helmfile relative path (#21591) --- .../__fixtures__/uses-kustomization.yaml | 10 +++++ lib/modules/manager/helmfile/extract.spec.ts | 41 ++++++++++++++++++- lib/modules/manager/helmfile/extract.ts | 2 +- lib/modules/manager/helmfile/utils.ts | 10 +++-- 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 lib/modules/manager/helmfile/__fixtures__/uses-kustomization.yaml diff --git a/lib/modules/manager/helmfile/__fixtures__/uses-kustomization.yaml b/lib/modules/manager/helmfile/__fixtures__/uses-kustomization.yaml new file mode 100644 index 0000000000..13db1e75ff --- /dev/null +++ b/lib/modules/manager/helmfile/__fixtures__/uses-kustomization.yaml @@ -0,0 +1,10 @@ +repositories: + - name: bitnami + url: https://charts.bitnami.com/bitnami + +releases: + - name: my-chart + chart: ../charts/my-chart + - name: memcached + version: 6.0.0 + chart: bitnami/memcached diff --git a/lib/modules/manager/helmfile/extract.spec.ts b/lib/modules/manager/helmfile/extract.spec.ts index 5405340710..479fad453f 100644 --- a/lib/modules/manager/helmfile/extract.spec.ts +++ b/lib/modules/manager/helmfile/extract.spec.ts @@ -1,14 +1,17 @@ import { Fixtures } from '../../../../test/fixtures'; import { fs } from '../../../../test/util'; import { GlobalConfig } from '../../../config/global'; +import { FILE_ACCESS_VIOLATION_ERROR } from '../../../constants/error-messages'; import { extractPackageFile } from '.'; jest.mock('../../../util/fs'); +const localDir = '/tmp/github/some/repo'; + describe('modules/manager/helmfile/extract', () => { describe('extractPackageFile()', () => { beforeEach(() => { - GlobalConfig.set({ localDir: '/tmp/github/some/repo' }); + GlobalConfig.set({ localDir }); jest.resetAllMocks(); }); @@ -385,5 +388,41 @@ describe('modules/manager/helmfile/extract', () => { managerData: { needKustomize: true }, }); }); + + it('detects kustomize and respects relative paths', async () => { + fs.localPathExists.mockImplementationOnce((path) => { + if (!path.startsWith(GlobalConfig.get('localDir') ?? '')) { + throw new Error(FILE_ACCESS_VIOLATION_ERROR); + } + return Promise.resolve(true); + }); + + const parentDir = `${localDir}/project`; + fs.getParentDir.mockReturnValue(parentDir); + const result = await extractPackageFile( + Fixtures.get('uses-kustomization.yaml'), + `${parentDir}/helmfile.yaml`, // In subdir + { + registryAliases: { + stable: 'https://charts.helm.sh/stable', + }, + } + ); + expect(result).toMatchObject({ + datasource: 'helm', + deps: [ + { + depName: 'my-chart', + skipReason: 'local-chart', + }, + { + depName: 'memcached', + currentValue: '6.0.0', + registryUrls: ['https://charts.bitnami.com/bitnami'], + }, + ], + managerData: { needKustomize: true }, + }); + }); }); }); diff --git a/lib/modules/manager/helmfile/extract.ts b/lib/modules/manager/helmfile/extract.ts index 347f8cf77f..a1c6bc4c96 100644 --- a/lib/modules/manager/helmfile/extract.ts +++ b/lib/modules/manager/helmfile/extract.ts @@ -75,7 +75,7 @@ export async function extractPackageFile( if (isLocalPath(dep.chart)) { if ( kustomizationsKeysUsed(dep) || - (await localChartHasKustomizationsYaml(dep)) + (await localChartHasKustomizationsYaml(dep, fileName)) ) { needKustomize = true; } diff --git a/lib/modules/manager/helmfile/utils.ts b/lib/modules/manager/helmfile/utils.ts index 16abccda2a..9f843612c5 100644 --- a/lib/modules/manager/helmfile/utils.ts +++ b/lib/modules/manager/helmfile/utils.ts @@ -1,6 +1,6 @@ import upath from 'upath'; -import { localPathExists } from '../../../util/fs'; +import { getParentDir, localPathExists } from '../../../util/fs'; import type { Release } from './types'; /** Returns true if a helmfile release contains kustomize specific keys **/ @@ -15,7 +15,11 @@ export function kustomizationsKeysUsed(release: Release): boolean { /** Returns true if a helmfile release uses a local chart with a kustomization.yaml file **/ // eslint-disable-next-line require-await export async function localChartHasKustomizationsYaml( - release: Release + release: Release, + helmFileYamlFileName: string ): Promise<boolean> { - return localPathExists(upath.join(release.chart, 'kustomization.yaml')); + const helmfileYamlParentDir = getParentDir(helmFileYamlFileName) || ''; + return localPathExists( + upath.join(helmfileYamlParentDir, release.chart, 'kustomization.yaml') + ); } -- GitLab