Skip to content

Commit

Permalink
feat(kustomize): support registry aliases (#27911)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Mar 13, 2024
1 parent bc4d090 commit b92afa4
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 29 deletions.
1 change: 1 addition & 0 deletions lib/config/options/index.ts
Expand Up @@ -1022,6 +1022,7 @@ const options: RenovateOptions[] = [
'helmfile',
'helmv3',
'kubernetes',
'kustomize',
'terraform',
'woodpecker',
],
Expand Down
82 changes: 68 additions & 14 deletions lib/modules/manager/kustomize/extract.spec.ts
Expand Up @@ -279,33 +279,67 @@ describe('modules/manager/kustomize/extract', () => {
});
expect(pkg).toEqual(sample);
});

it('should correctly extract with registryAliases', () => {
const sample = {
autoReplaceStringTemplate:
'{{newValue}}{{#if newDigest}}@{{newDigest}}{{/if}}',
currentDigest: undefined,
currentValue: 'v1.0.0',
replaceString: 'v1.0.0',
datasource: DockerDatasource.id,
depName: 'docker.io/image/service',
};
const pkg = extractImage(
{
name: 'localhost:5000/repo/image/service',
newTag: sample.currentValue,
},
{ 'localhost:5000/repo': 'docker.io' },
);
expect(pkg).toEqual(sample);
});
});

describe('extractPackageFile()', () => {
it('returns null for non kustomize kubernetes files', () => {
expect(extractPackageFile(nonKustomize)).toBeNull();
expect(
extractPackageFile(nonKustomize, 'kustomization.yaml', {}),
).toBeNull();
});

it('extracts multiple image lines', () => {
const res = extractPackageFile(kustomizeWithLocal);
const res = extractPackageFile(
kustomizeWithLocal,
'kustomization.yaml',
{},
);
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(2);
});

it('extracts ssh dependency', () => {
const res = extractPackageFile(kustomizeGitSSHBase);
const res = extractPackageFile(
kustomizeGitSSHBase,
'kustomization.yaml',
{},
);
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(1);
});

it('extracts ssh dependency with a subdir', () => {
const res = extractPackageFile(kustomizeGitSSHSubdir);
const res = extractPackageFile(
kustomizeGitSSHSubdir,
'kustomization.yaml',
{},
);
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(1);
});

it('extracts http dependency', () => {
const res = extractPackageFile(kustomizeHTTP);
const res = extractPackageFile(kustomizeHTTP, 'kustomization.yaml', {});
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(2);
expect(res?.deps[0].currentValue).toBe('v0.0.1');
Expand All @@ -314,7 +348,7 @@ describe('modules/manager/kustomize/extract', () => {
});

it('should extract out image versions', () => {
const res = extractPackageFile(gitImages);
const res = extractPackageFile(gitImages, 'kustomization.yaml', {});
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(6);
expect(res?.deps[0].currentValue).toBe('v0.1.0');
Expand All @@ -323,15 +357,21 @@ describe('modules/manager/kustomize/extract', () => {
});

it('ignores non-Kubernetes empty files', () => {
expect(extractPackageFile('')).toBeNull();
expect(extractPackageFile('', 'kustomization.yaml', {})).toBeNull();
});

it('does nothing with kustomize empty kustomize files', () => {
expect(extractPackageFile(kustomizeEmpty)).toBeNull();
expect(
extractPackageFile(kustomizeEmpty, 'kustomization.yaml', {}),
).toBeNull();
});

it('should extract bases resources and components from their respective blocks', () => {
const res = extractPackageFile(kustomizeDepsInResources);
const res = extractPackageFile(
kustomizeDepsInResources,
'kustomization.yaml',
{},
);
expect(res).not.toBeNull();
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(3);
Expand All @@ -347,7 +387,11 @@ describe('modules/manager/kustomize/extract', () => {
});

it('should extract dependencies when kind is Component', () => {
const res = extractPackageFile(kustomizeComponent);
const res = extractPackageFile(
kustomizeComponent,
'kustomization.yaml',
{},
);
expect(res).not.toBeNull();
expect(res?.deps).toMatchSnapshot();
expect(res?.deps).toHaveLength(3);
Expand All @@ -366,7 +410,9 @@ describe('modules/manager/kustomize/extract', () => {
'sha256:b0cfe264cb1143c7c660ddfd5c482464997d62d6bc9f97f8fdf3deefce881a8c';

it('extracts from newTag', () => {
expect(extractPackageFile(newTag)).toMatchSnapshot({
expect(
extractPackageFile(newTag, 'kustomization.yaml', {}),
).toMatchSnapshot({
deps: [
{
currentDigest: undefined,
Expand All @@ -386,7 +432,9 @@ describe('modules/manager/kustomize/extract', () => {
});

it('extracts from digest', () => {
expect(extractPackageFile(digest)).toMatchSnapshot({
expect(
extractPackageFile(digest, 'kustomization.yaml', {}),
).toMatchSnapshot({
deps: [
{
currentDigest: postgresDigest,
Expand All @@ -412,7 +460,9 @@ describe('modules/manager/kustomize/extract', () => {
});

it('extracts newName', () => {
expect(extractPackageFile(newName)).toMatchSnapshot({
expect(
extractPackageFile(newName, 'kustomization.yaml', {}),
).toMatchSnapshot({
deps: [
{
depName: 'awesome/postgres',
Expand Down Expand Up @@ -440,7 +490,11 @@ describe('modules/manager/kustomize/extract', () => {
});

it('parses helmChart field', () => {
const res = extractPackageFile(kustomizeHelmChart);
const res = extractPackageFile(
kustomizeHelmChart,
'kustomization.yaml',
{},
);
expect(res).toMatchSnapshot({
deps: [
{
Expand Down
32 changes: 17 additions & 15 deletions lib/modules/manager/kustomize/extract.ts
Expand Up @@ -3,12 +3,15 @@ import { logger } from '../../../logger';
import { coerceArray } from '../../../util/array';
import { regEx } from '../../../util/regex';
import { parseSingleYaml } from '../../../util/yaml';
import { DockerDatasource } from '../../datasource/docker';
import { GitTagsDatasource } from '../../datasource/git-tags';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import { HelmDatasource } from '../../datasource/helm';
import { splitImageParts } from '../dockerfile/extract';
import type { PackageDependency, PackageFileContent } from '../types';
import { getDep } from '../dockerfile/extract';
import type {
ExtractConfig,
PackageDependency,
PackageFileContent,
} from '../types';
import type { HelmChart, Image, Kustomize } from './types';

// URL specifications should follow the hashicorp URL format
Expand Down Expand Up @@ -63,7 +66,10 @@ export function extractResource(base: string): PackageDependency | null {
};
}

export function extractImage(image: Image): PackageDependency | null {
export function extractImage(
image: Image,
aliases?: Record<string, string> | undefined,
): PackageDependency | null {
if (!image.name) {
return null;
}
Expand All @@ -72,7 +78,7 @@ export function extractImage(image: Image): PackageDependency | null {
logger.debug({ image }, 'Invalid image name');
return null;
}
const nameDep = splitImageParts(nameToSplit);
const nameDep = getDep(nameToSplit, false, aliases);
const { depName } = nameDep;
const { digest, newTag } = image;
if (digest && newTag) {
Expand All @@ -98,9 +104,7 @@ export function extractImage(image: Image): PackageDependency | null {
}

return {
datasource: DockerDatasource.id,
depName,
currentValue: nameDep.currentValue,
...nameDep,
currentDigest: digest,
replaceString: digest,
};
Expand All @@ -115,11 +119,9 @@ export function extractImage(image: Image): PackageDependency | null {
};
}

// TODO: types (#22198)
const dep = splitImageParts(`${depName}:${newTag}`);
const dep = getDep(`${depName}:${newTag}`, false, aliases);
return {
...dep,
datasource: DockerDatasource.id,
replaceString: newTag,
autoReplaceStringTemplate:
'{{newValue}}{{#if newDigest}}@{{newDigest}}{{/if}}',
Expand All @@ -129,7 +131,6 @@ export function extractImage(image: Image): PackageDependency | null {
if (image.newName) {
return {
...nameDep,
datasource: DockerDatasource.id,
replaceString: image.newName,
};
}
Expand Down Expand Up @@ -180,9 +181,10 @@ export function parseKustomize(

export function extractPackageFile(
content: string,
packageFile?: string, // TODO: fix tests
packageFile: string,
config: ExtractConfig,
): PackageFileContent | null {
logger.trace(`kustomize.extractPackageFile(${packageFile!})`);
logger.trace(`kustomize.extractPackageFile(${packageFile})`);
const deps: PackageDependency[] = [];

const pkg = parseKustomize(content, packageFile);
Expand Down Expand Up @@ -225,7 +227,7 @@ export function extractPackageFile(

// grab the image tags
for (const image of coerceArray(pkg.images)) {
const dep = extractImage(image);
const dep = extractImage(image, config.registryAliases);
if (dep) {
deps.push({
...dep,
Expand Down

0 comments on commit b92afa4

Please sign in to comment.