Skip to content

Commit

Permalink
support for multiple paths
Browse files Browse the repository at this point in the history
  • Loading branch information
freschri committed Jul 14, 2023
1 parent 4b1c85f commit 20a3382
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
53 changes: 41 additions & 12 deletions lib/addons/fluxcd/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` */
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
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;
}
10 changes: 5 additions & 5 deletions lib/addons/fluxcd/kustomization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,7 +25,7 @@ export class FluxKustomization {
kind: "GitRepository",
name: repository.name
},
path: repository.path,
path: fluxKustomizationPath,
prune: fluxPrune,
timeout: fluxTimeout
}
Expand Down

0 comments on commit 20a3382

Please sign in to comment.