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

This commit is contained in:
ssams 2025-01-08 16:17:04 +01:00 committed by GitHub
parent 8683eeb7ad
commit 766d0c37cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 64 additions and 52 deletions

View file

@ -1,4 +1,6 @@
import { regEx } from '../../../util/regex'; import { regEx } from '../../../util/regex';
import type { HelmRepository } from './schema';
import type { FluxManifest } from './types';
export const systemManifestFileNameRegex = '(?:^|/)gotk-components\\.ya?ml$'; export const systemManifestFileNameRegex = '(?:^|/)gotk-components\\.ya?ml$';
@ -8,3 +10,19 @@ export const systemManifestHeaderRegex =
export function isSystemManifest(file: string): boolean { export function isSystemManifest(file: string): boolean {
return regEx(systemManifestFileNameRegex).test(file); 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;
}

View file

@ -21,7 +21,11 @@ import type {
PackageFile, PackageFile,
PackageFileContent, PackageFileContent,
} from '../types'; } from '../types';
import { isSystemManifest, systemManifestHeaderRegex } from './common'; import {
collectHelmRepos,
isSystemManifest,
systemManifestHeaderRegex,
} from './common';
import { FluxResource, type HelmRepository } from './schema'; import { FluxResource, type HelmRepository } from './schema';
import type { import type {
FluxManagerData, FluxManagerData,
@ -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( function resolveSystemManifest(
manifest: SystemFluxManifest, manifest: SystemFluxManifest,
): PackageDependency<FluxManagerData>[] { ): PackageDependency<FluxManagerData>[] {
@ -126,7 +163,8 @@ function resolveResourceManifest(
for (const resource of manifest.resources) { for (const resource of manifest.resources) {
switch (resource.kind) { switch (resource.kind) {
case 'HelmRelease': { case 'HelmRelease': {
const depName = resource.spec.chart.spec.chart; const chartSpec = resource.spec.chart.spec;
const depName = chartSpec.chart;
const dep: PackageDependency = { const dep: PackageDependency = {
depName, depName,
currentValue: resource.spec.chart.spec.version, currentValue: resource.spec.chart.spec.version,
@ -142,40 +180,12 @@ function resolveResourceManifest(
const matchingRepositories = helmRepositories.filter( const matchingRepositories = helmRepositories.filter(
(rep) => (rep) =>
rep.kind === resource.spec.chart.spec.sourceRef?.kind && rep.kind === chartSpec.sourceRef?.kind &&
rep.metadata.name === resource.spec.chart.spec.sourceRef.name && rep.metadata.name === chartSpec.sourceRef.name &&
rep.metadata.namespace === rep.metadata.namespace ===
(resource.spec.chart.spec.sourceRef.namespace ?? (chartSpec.sourceRef.namespace ?? resource.metadata?.namespace),
resource.metadata?.namespace),
); );
if (matchingRepositories.length) { resolveHelmRepository(dep, matchingRepositories, registryAliases);
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';
}
deps.push(dep); deps.push(dep);
break; break;
} }
@ -252,14 +262,7 @@ export function extractPackageFile(
if (!manifest) { if (!manifest) {
return null; return null;
} }
const helmRepositories: HelmRepository[] = []; const helmRepositories = collectHelmRepos([manifest]);
if (manifest.kind === 'resource') {
for (const resource of manifest.resources) {
if (resource.kind === 'HelmRepository') {
helmRepositories.push(resource);
}
}
}
let deps: PackageDependency[] | null = null; let deps: PackageDependency[] | null = null;
switch (manifest.kind) { switch (manifest.kind) {
case 'system': case 'system':
@ -293,16 +296,7 @@ export async function extractAllPackageFiles(
} }
} }
const helmRepositories: HelmRepository[] = []; const helmRepositories = collectHelmRepos(manifests);
for (const manifest of manifests) {
if (manifest.kind === 'resource') {
for (const resource of manifest.resources) {
if (resource.kind === 'HelmRepository') {
helmRepositories.push(resource);
}
}
}
}
for (const manifest of manifests) { for (const manifest of manifests) {
let deps: PackageDependency[] | null = null; let deps: PackageDependency[] | null = null;