Skip to content

Commit

Permalink
feat(flux): support registry aliases (#29222)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored May 23, 2024
1 parent 249b50f commit 1de6906
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 20 additions & 6 deletions lib/modules/manager/flux/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand All @@ -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: [
Expand All @@ -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',
},
Expand Down Expand Up @@ -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: [
Expand All @@ -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:
Expand Down
42 changes: 31 additions & 11 deletions lib/modules/manager/flux/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function resolveSystemManifest(
function resolveResourceManifest(
manifest: ResourceFluxManifest,
helmRepositories: HelmRepository[],
registryAliases: Record<string, string> | undefined,
): PackageDependency[] {
const deps: PackageDependency[] = [];
for (const resource of manifest.resources) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -223,6 +234,7 @@ function resolveResourceManifest(
export function extractPackageFile(
content: string,
packageFile: string,
config?: ExtractConfig,
): PackageFileContent<FluxManagerData> | null {
const manifest = readManifest(content, packageFile);
if (!manifest) {
Expand All @@ -242,15 +254,19 @@ export function extractPackageFile(
deps = resolveSystemManifest(manifest);
break;
case 'resource': {
deps = resolveResourceManifest(manifest, helmRepositories);
deps = resolveResourceManifest(
manifest,
helmRepositories,
config?.registryAliases,
);
break;
}
}
return deps?.length ? { deps } : null;
}

export async function extractAllPackageFiles(
_config: ExtractConfig,
config: ExtractConfig,
packageFiles: string[],
): Promise<PackageFile<FluxManagerData>[] | null> {
const manifests: FluxManifest[] = [];
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 1de6906

Please sign in to comment.