From 1de69069b85b283b24d6e4b679cbc63c99d6fef2 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Thu, 23 May 2024 14:51:36 +0200 Subject: [PATCH] feat(flux): support registry aliases (#29222) --- docs/usage/configuration-options.md | 1 + lib/modules/manager/flux/extract.spec.ts | 26 +++++++++++---- lib/modules/manager/flux/extract.ts | 42 +++++++++++++++++------- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 2f573e4ea19637..90be5223c044b6 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -3647,6 +3647,7 @@ This feature works with the following managers: - [`docker-compose`](modules/manager/docker-compose/index.md) - [`dockerfile`](modules/manager/dockerfile/index.md) - [`droneci`](modules/manager/droneci/index.md) +- [`flux`](modules/manager/flux/index.md) - [`gitlabci`](modules/manager/gitlabci/index.md) - [`helm-requirements`](modules/manager/helm-requirements/index.md) - [`helm-values`](modules/manager/helm-values/index.md) diff --git a/lib/modules/manager/flux/extract.spec.ts b/lib/modules/manager/flux/extract.spec.ts index 6c4b5c4f1192dd..f754ec6dc5981d 100644 --- a/lib/modules/manager/flux/extract.spec.ts +++ b/lib/modules/manager/flux/extract.spec.ts @@ -502,6 +502,9 @@ describe('modules/manager/flux/extract', () => { expect(result).toEqual({ deps: [ { + currentDigest: undefined, + currentValue: undefined, + datasource: 'docker', depName: 'ghcr.io/kyverno/manifests/kyverno', skipReason: 'unversioned-reference', }, @@ -523,6 +526,11 @@ describe('modules/manager/flux/extract', () => { url: oci://ghcr.io/kyverno/manifests/kyverno `, 'test.yaml', + { + registryAliases: { + 'ghcr.io': 'ghcr.proxy.test/some/path', + }, + }, ); expect(result).toEqual({ deps: [ @@ -531,7 +539,7 @@ describe('modules/manager/flux/extract', () => { '{{#if newValue}}{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}', currentValue: 'v1.8.2', currentDigest: undefined, - depName: 'ghcr.io/kyverno/manifests/kyverno', + depName: 'ghcr.proxy.test/some/path/kyverno/manifests/kyverno', datasource: DockerDatasource.id, replaceString: 'v1.8.2', }, @@ -723,10 +731,16 @@ describe('modules/manager/flux/extract', () => { }); it('should handle HelmRepository with type OCI', async () => { - const result = await extractAllPackageFiles(config, [ - 'lib/modules/manager/flux/__fixtures__/helmOCISource.yaml', - 'lib/modules/manager/flux/__fixtures__/helmOCIRelease.yaml', - ]); + const result = await extractAllPackageFiles( + { + ...config, + registryAliases: { 'ghcr.io': 'ghcr.proxy.test/some/path' }, + }, + [ + 'lib/modules/manager/flux/__fixtures__/helmOCISource.yaml', + 'lib/modules/manager/flux/__fixtures__/helmOCIRelease.yaml', + ], + ); expect(result).toEqual([ { deps: [ @@ -735,7 +749,7 @@ describe('modules/manager/flux/extract', () => { datasource: DockerDatasource.id, depName: 'actions-runner-controller-charts/gha-runner-scale-set', packageName: - 'ghcr.io/actions/actions-runner-controller-charts/gha-runner-scale-set', + 'ghcr.proxy.test/some/path/actions/actions-runner-controller-charts/gha-runner-scale-set', }, ], packageFile: diff --git a/lib/modules/manager/flux/extract.ts b/lib/modules/manager/flux/extract.ts index 325ca85b20032b..1bf9258c57036e 100644 --- a/lib/modules/manager/flux/extract.ts +++ b/lib/modules/manager/flux/extract.ts @@ -125,6 +125,7 @@ function resolveSystemManifest( function resolveResourceManifest( manifest: ResourceFluxManifest, helmRepositories: HelmRepository[], + registryAliases: Record | undefined, ): PackageDependency[] { const deps: PackageDependency[] = []; for (const resource of manifest.resources) { @@ -151,9 +152,13 @@ function resolveResourceManifest( // Change datasource to Docker dep.datasource = DockerDatasource.id; // Ensure the URL is a valid OCI path - dep.packageName = `${removeOCIPrefix(repo.spec.url)}/${ - resource.spec.chart.spec.chart - }`; + dep.packageName = getDep( + `${removeOCIPrefix(repo.spec.url)}/${ + resource.spec.chart.spec.chart + }`, + false, + registryAliases, + ).depName; return null; } else { return repo.spec.url; @@ -196,16 +201,22 @@ function resolveResourceManifest( } case 'OCIRepository': { const container = removeOCIPrefix(resource.spec.url); - let dep: PackageDependency = { - depName: container, - }; + let dep = getDep(container, false, registryAliases); if (resource.spec.ref?.digest) { - dep = getDep(`${container}@${resource.spec.ref.digest}`, false); + dep = getDep( + `${container}@${resource.spec.ref.digest}`, + false, + registryAliases, + ); if (resource.spec.ref?.tag) { logger.debug('A digest and tag was found, ignoring tag'); } } else if (resource.spec.ref?.tag) { - dep = getDep(`${container}:${resource.spec.ref.tag}`, false); + dep = getDep( + `${container}:${resource.spec.ref.tag}`, + false, + registryAliases, + ); dep.autoReplaceStringTemplate = '{{#if newValue}}{{newValue}}{{/if}}{{#if newDigest}}@{{newDigest}}{{/if}}'; dep.replaceString = resource.spec.ref.tag; @@ -223,6 +234,7 @@ function resolveResourceManifest( export function extractPackageFile( content: string, packageFile: string, + config?: ExtractConfig, ): PackageFileContent | null { const manifest = readManifest(content, packageFile); if (!manifest) { @@ -242,7 +254,11 @@ export function extractPackageFile( deps = resolveSystemManifest(manifest); break; case 'resource': { - deps = resolveResourceManifest(manifest, helmRepositories); + deps = resolveResourceManifest( + manifest, + helmRepositories, + config?.registryAliases, + ); break; } } @@ -250,7 +266,7 @@ export function extractPackageFile( } export async function extractAllPackageFiles( - _config: ExtractConfig, + config: ExtractConfig, packageFiles: string[], ): Promise[] | null> { const manifests: FluxManifest[] = []; @@ -283,7 +299,11 @@ export async function extractAllPackageFiles( deps = resolveSystemManifest(manifest); break; case 'resource': { - deps = resolveResourceManifest(manifest, helmRepositories); + deps = resolveResourceManifest( + manifest, + helmRepositories, + config.registryAliases, + ); break; } }