From 20a3382996359d2f0e4e5245dfcfe875cdd1a0f3 Mon Sep 17 00:00:00 2001 From: Riccardo Freschi Date: Fri, 14 Jul 2023 16:49:06 +0100 Subject: [PATCH] support for multiple paths --- lib/addons/fluxcd/index.ts | 53 +++++++++++++++++++++++------- lib/addons/fluxcd/kustomization.ts | 10 +++--- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/lib/addons/fluxcd/index.ts b/lib/addons/fluxcd/index.ts index de0725b9b..19475e157 100644 --- a/lib/addons/fluxcd/index.ts +++ b/lib/addons/fluxcd/index.ts @@ -64,6 +64,11 @@ export interface FluxCDAddOnProps extends HelmAddOnUserProps { * Default `default` */ fluxTargetNamespace?: string; + /** + * Flux Kustomization paths within the repository; + * Flux Kustomizations will be created for bootstrapRepo.path and for the paths specified in this array */ + additionalFluxKustomizationPaths?: string[]; + /** * Flux Kustomization Prune. * Default `true` */ @@ -119,10 +124,11 @@ export class FluxCDAddOn extends HelmAddOn { //Lets create a GitRepository resource as a source to Flux if (this.options.bootstrapRepo?.repoUrl) { - const gitRepositoryConstruct = createGitRepository(clusterInfo, this.options.bootstrapRepo, this.options); + const gitRepositoryConstruct = createGitRepository(clusterInfo, this.options); gitRepositoryConstruct.node.addDependency(chart); - const kustomizationConstruct = createKustomization(clusterInfo, this.options.bootstrapRepo, this.options); - kustomizationConstruct.node.addDependency(gitRepositoryConstruct); + + const kustomizationConstructs = createKustomizations(clusterInfo, this.options); + kustomizationConstructs.map(kustomizationConstruct => kustomizationConstruct.node.addDependency(gitRepositoryConstruct)); } return Promise.resolve(chart); } @@ -131,19 +137,42 @@ export class FluxCDAddOn extends HelmAddOn { /** * create GitRepository calls the FluxGitRepository().generate to create GitRepostory resource. */ -function createGitRepository(clusterInfo: ClusterInfo, bootstrapRepo: spi.ApplicationRepository, fluxcdAddonProps: FluxCDAddOnProps): KubernetesManifest { - const manifest = new FluxGitRepository(bootstrapRepo).generate(fluxcdAddonProps.namespace!, fluxcdAddonProps.fluxSyncInterval!, fluxcdAddonProps.fluxSecretRefName!); +function createGitRepository(clusterInfo: ClusterInfo, fluxcdAddonProps: FluxCDAddOnProps): KubernetesManifest { + if (fluxcdAddonProps.bootstrapRepo === undefined) { + throw new Error("Missing Git repository"); + } + + const manifest = new FluxGitRepository(fluxcdAddonProps.bootstrapRepo).generate(fluxcdAddonProps.namespace!, fluxcdAddonProps.fluxSyncInterval!, fluxcdAddonProps.fluxSecretRefName!); let manifestName: string | undefined = fluxcdAddonProps.name + 'gitrepository'; const construct = clusterInfo.cluster.addManifest(manifestName!, manifest); return construct; } /** - * create Kustomization calls the FluxKustomization().generate to create Kustomization resource. + * create Kustomizations calls the FluxKustomization().generate multiple times to create Kustomization resources. */ -function createKustomization(clusterInfo: ClusterInfo, bootstrapRepo: spi.ApplicationRepository, fluxcdAddonProps: FluxCDAddOnProps): KubernetesManifest { - const manifest = new FluxKustomization(bootstrapRepo).generate(fluxcdAddonProps.namespace!, fluxcdAddonProps.fluxSyncInterval!, fluxcdAddonProps.fluxTargetNamespace!, fluxcdAddonProps.fluxPrune!, fluxcdAddonProps.fluxTimeout!, fluxcdAddonProps.bootstrapValues!); - let manifestName: string | undefined = fluxcdAddonProps.name + 'kustomization'; - const construct = clusterInfo.cluster.addManifest(manifestName!, manifest); - return construct; -} \ No newline at end of file +function createKustomizations(clusterInfo: ClusterInfo, fluxcdAddonProps: FluxCDAddOnProps): KubernetesManifest[] { + let fluxKustomizationPaths = fluxcdAddonProps.bootstrapRepo?.path ? [fluxcdAddonProps.bootstrapRepo?.path] : ["."]; + + if (typeof fluxcdAddonProps.additionalFluxKustomizationPaths !== undefined){ + fluxKustomizationPaths = fluxKustomizationPaths.concat(fluxcdAddonProps.additionalFluxKustomizationPaths as string[]); + } + + const constructs: KubernetesManifest[] = []; + const fluxKustomization = new FluxKustomization(fluxcdAddonProps.bootstrapRepo!); + fluxKustomizationPaths.map((fluxKustomizationPath, index) => { + const manifest =fluxKustomization.generate( + fluxcdAddonProps.bootstrapRepo!.name! + "-" + index, + fluxcdAddonProps.namespace!, + fluxcdAddonProps.fluxSyncInterval!, + fluxcdAddonProps.fluxTargetNamespace!, + fluxcdAddonProps.fluxPrune!, + fluxcdAddonProps.fluxTimeout!, + fluxcdAddonProps.bootstrapValues!, + fluxKustomizationPath); + let manifestName: string | undefined = fluxcdAddonProps.name + 'kustomization' + index; + constructs.push(clusterInfo.cluster.addManifest(manifestName!, manifest)); + }); + + return constructs; +} diff --git a/lib/addons/fluxcd/kustomization.ts b/lib/addons/fluxcd/kustomization.ts index 2c3f27b01..69ef90ef4 100644 --- a/lib/addons/fluxcd/kustomization.ts +++ b/lib/addons/fluxcd/kustomization.ts @@ -8,15 +8,15 @@ export class FluxKustomization { constructor(private readonly bootstrapRepo: spi.ApplicationRepository) {} - public generate(namespace: string, fluxSyncInterval: string, fluxTargetNamespace: string, fluxPrune: boolean, fluxTimeout: string, bootstrapValues: spi.Values) { - + public generate(name: string, namespace: string, fluxSyncInterval: string, fluxTargetNamespace: string, fluxPrune: boolean, fluxTimeout: string, bootstrapValues: spi.Values, fluxKustomizationPath: string) { + const repository = this.bootstrapRepo!; const kustomizationManifest = { apiVersion: "kustomize.toolkit.fluxcd.io/v1beta2", kind: "Kustomization", metadata: { - name: repository.name, - namespace: namespace + name, + namespace }, spec: { interval: fluxSyncInterval, @@ -25,7 +25,7 @@ export class FluxKustomization { kind: "GitRepository", name: repository.name }, - path: repository.path, + path: fluxKustomizationPath, prune: fluxPrune, timeout: fluxTimeout }