diff --git a/lib/modules/manager/flux/common.ts b/lib/modules/manager/flux/common.ts index 8fff55a1cf..bafa628513 100644 --- a/lib/modules/manager/flux/common.ts +++ b/lib/modules/manager/flux/common.ts @@ -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$'; @@ -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; +} diff --git a/lib/modules/manager/flux/extract.ts b/lib/modules/manager/flux/extract.ts index a592183c30..98e10778b0 100644 --- a/lib/modules/manager/flux/extract.ts +++ b/lib/modules/manager/flux/extract.ts @@ -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, @@ -102,6 +106,39 @@ function resolveGitRepositoryPerSourceTag( } } +function resolveHelmRepository( + dep: PackageDependency, + matchingRepositories: HelmRepository[], + registryAliases: Record | 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[] { @@ -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, @@ -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; } @@ -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': @@ -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;