Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(manager/flux): extract helm repo handling to helper functions #33462

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/modules/manager/flux/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { regEx } from '../../../util/regex';
import type { HelmRepository } from './schema';
import type { FluxManifest } from './types';

export const systemManifestFileNameRegex = '(?:^|/)gotk-components\\.ya?ml$';

Expand All @@ -8,3 +10,19 @@ export const systemManifestHeaderRegex =
export function isSystemManifest(file: string): boolean {
return regEx(systemManifestFileNameRegex).test(file);
}

export function collectHelmRepos(manifests: FluxManifest[]): HelmRepository[] {
const helmRepositories: HelmRepository[] = [];

for (const manifest of manifests) {
if (manifest.kind === 'resource') {
for (const resource of manifest.resources) {
if (resource.kind === 'HelmRepository') {
helmRepositories.push(resource);
}
}
}
}

return helmRepositories;
}
98 changes: 46 additions & 52 deletions lib/modules/manager/flux/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import type {
PackageFile,
PackageFileContent,
} from '../types';
import { isSystemManifest, systemManifestHeaderRegex } from './common';
import {
collectHelmRepos,
isSystemManifest,
systemManifestHeaderRegex,
} from './common';
import { FluxResource, type HelmRepository } from './schema';
import type {
FluxManagerData,
Expand Down Expand Up @@ -102,6 +106,39 @@ function resolveGitRepositoryPerSourceTag(
}
}

function resolveHelmRepository(
dep: PackageDependency,
matchingRepositories: HelmRepository[],
registryAliases: Record<string, string> | undefined,
): void {
if (matchingRepositories.length) {
dep.registryUrls = matchingRepositories
.map((repo) => {
if (repo.spec.type === 'oci' || isOCIRegistry(repo.spec.url)) {
// Change datasource to Docker
dep.datasource = DockerDatasource.id;
// Ensure the URL is a valid OCI path
dep.packageName = getDep(
`${removeOCIPrefix(repo.spec.url)}/${dep.depName}`,
false,
registryAliases,
).depName;
return null;
} else {
return repo.spec.url;
}
})
.filter(is.string);

// if registryUrls is empty, delete it from dep
if (!dep.registryUrls?.length) {
delete dep.registryUrls;
}
} else {
dep.skipReason = 'unknown-registry';
}
}

function resolveSystemManifest(
manifest: SystemFluxManifest,
): PackageDependency<FluxManagerData>[] {
Expand All @@ -126,7 +163,8 @@ function resolveResourceManifest(
for (const resource of manifest.resources) {
switch (resource.kind) {
case 'HelmRelease': {
const depName = resource.spec.chart.spec.chart;
const chartSpec = resource.spec.chart.spec;
const depName = chartSpec.chart;
const dep: PackageDependency = {
depName,
currentValue: resource.spec.chart.spec.version,
Expand All @@ -142,40 +180,12 @@ function resolveResourceManifest(

const matchingRepositories = helmRepositories.filter(
(rep) =>
rep.kind === resource.spec.chart.spec.sourceRef?.kind &&
rep.metadata.name === resource.spec.chart.spec.sourceRef.name &&
rep.kind === chartSpec.sourceRef?.kind &&
rep.metadata.name === chartSpec.sourceRef.name &&
rep.metadata.namespace ===
(resource.spec.chart.spec.sourceRef.namespace ??
resource.metadata?.namespace),
(chartSpec.sourceRef.namespace ?? resource.metadata?.namespace),
);
if (matchingRepositories.length) {
dep.registryUrls = matchingRepositories
.map((repo) => {
if (repo.spec.type === 'oci' || isOCIRegistry(repo.spec.url)) {
// Change datasource to Docker
dep.datasource = DockerDatasource.id;
// Ensure the URL is a valid OCI path
dep.packageName = getDep(
`${removeOCIPrefix(repo.spec.url)}/${
resource.spec.chart.spec.chart
}`,
false,
registryAliases,
).depName;
return null;
} else {
return repo.spec.url;
}
})
.filter(is.string);

// if registryUrls is empty, delete it from dep
if (!dep.registryUrls?.length) {
delete dep.registryUrls;
}
} else {
dep.skipReason = 'unknown-registry';
}
resolveHelmRepository(dep, matchingRepositories, registryAliases);
deps.push(dep);
break;
}
Expand Down Expand Up @@ -252,14 +262,7 @@ export function extractPackageFile(
if (!manifest) {
return null;
}
const helmRepositories: HelmRepository[] = [];
if (manifest.kind === 'resource') {
for (const resource of manifest.resources) {
if (resource.kind === 'HelmRepository') {
helmRepositories.push(resource);
}
}
}
const helmRepositories = collectHelmRepos([manifest]);
let deps: PackageDependency[] | null = null;
switch (manifest.kind) {
case 'system':
Expand Down Expand Up @@ -293,16 +296,7 @@ export async function extractAllPackageFiles(
}
}

const helmRepositories: HelmRepository[] = [];
for (const manifest of manifests) {
if (manifest.kind === 'resource') {
for (const resource of manifest.resources) {
if (resource.kind === 'HelmRepository') {
helmRepositories.push(resource);
}
}
}
}
const helmRepositories = collectHelmRepos(manifests);

for (const manifest of manifests) {
let deps: PackageDependency[] | null = null;
Expand Down
Loading