Skip to content

Commit

Permalink
Run tests in parallel and refactor Kustomize flows (#449)
Browse files Browse the repository at this point in the history
* Run in parallel

* refactor(tests): rename KSailStartCommandTests and consolidate with KSailStopCommandTests

* refactor: update Kustomize hooks and flows to use project-specific properties

* refactor: standardize kustomizeFlows and kustomizeHooks across configuration files

* refactor: update reconcile method to use project-specific Kustomize flows

* refactor: reverse kustomizeFlows order in reconcile method for Up and Update commands
  • Loading branch information
devantler authored Dec 1, 2024
1 parent 38678ea commit 629469b
Show file tree
Hide file tree
Showing 30 changed files with 96 additions and 122 deletions.
10 changes: 0 additions & 10 deletions src/KSail.Models/CLI/Commands/Init/KSailCLIInitOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,4 @@ public class KSailCLIInitOptions
/// The template to use for the generated output.
/// </summary>
public KSailCLIInitTemplate Template { get; set; } = KSailCLIInitTemplate.Simple;

/// <summary>
/// The different Kustomizations to generate. First depends on the second, and so on.
/// </summary>
public IEnumerable<string> KustomizeFlows { get; set; } = ["apps", "infrastructure", "infrastructure/controllers"];

/// <summary>
/// The different places that it should be able to hook into the Kustomization flows. For example per cluster or distribution.
/// </summary>
public IEnumerable<string> KustomizeHooks { get; set; } = [];
}
10 changes: 10 additions & 0 deletions src/KSail.Models/Project/KSailProjectOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public class KSailProjectOptions
/// </summary>
public string ConfigPath { get; set; } = "kind-config.yaml";

/// <summary>
/// The different Kustomizations to generate. First depends on the second, and so on.
/// </summary>
public IEnumerable<string> KustomizeFlows { get; set; } = ["apps", "infrastructure", "infrastructure/controllers"];

/// <summary>
/// The different places that it should be able to hook into the Kustomization flows. For example per cluster or distribution.
/// </summary>
public IEnumerable<string> KustomizeHooks { get; set; } = [];

/// <summary>
/// The Kubernetes distribution to use.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async Task GenerateFluxKustomizationPostBuildVariablesLabelComponent(KSailCluste
}
]
};
foreach (string hook in config.Spec.CLI.InitOptions.KustomizeHooks.Skip(1))
foreach (string hook in config.Spec.Project.KustomizeHooks.Skip(1))
{
fluxKustomizationPostBuildVariablesLabelComponent.Patches.First().Patch += $"{Environment.NewLine} - kind: ConfigMap";
fluxKustomizationPostBuildVariablesLabelComponent.Patches.First().Patch += $"{Environment.NewLine} name: variables-{hook}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ internal async Task GenerateAsync(KSailCluster config, CancellationToken cancell
if (!Directory.Exists(outputDirectory))
_ = Directory.CreateDirectory(outputDirectory);
await GenerateFluxSystemKustomization(config, outputDirectory, cancellationToken).ConfigureAwait(false);
foreach (string flow in config.Spec.CLI.InitOptions.KustomizeFlows)
foreach (string flow in config.Spec.Project.KustomizeFlows)
{
List<FluxDependsOn> dependsOn = [];
dependsOn = config.Spec.CLI.InitOptions.PostBuildVariables && !config.Spec.CLI.InitOptions.KustomizeFlows.IsNullOrEmpty()
? config.Spec.CLI.InitOptions.KustomizeFlows.Last() == flow
dependsOn = config.Spec.CLI.InitOptions.PostBuildVariables && !config.Spec.Project.KustomizeFlows.IsNullOrEmpty()
? config.Spec.Project.KustomizeFlows.Last() == flow
? ([new FluxDependsOn { Name = "variables" }])
: config.Spec.CLI.InitOptions.KustomizeFlows.Reverse().TakeWhile(f => f != flow).Select(f => new FluxDependsOn { Name = f.Replace('/', '-') }).TakeLast(1).ToList()
: config.Spec.CLI.InitOptions.KustomizeFlows.Reverse().TakeWhile(f => f != flow).Select(f => new FluxDependsOn { Name = f.Replace('/', '-') }).TakeLast(1).ToList();
: config.Spec.Project.KustomizeFlows.Reverse().TakeWhile(f => f != flow).Select(f => new FluxDependsOn { Name = f.Replace('/', '-') }).TakeLast(1).ToList()
: config.Spec.Project.KustomizeFlows.Reverse().TakeWhile(f => f != flow).Select(f => new FluxDependsOn { Name = f.Replace('/', '-') }).TakeLast(1).ToList();

await GenerateFluxSystemFluxKustomization(config, outputDirectory, flow, dependsOn, cancellationToken).ConfigureAwait(false);
}
Expand All @@ -49,7 +49,7 @@ async Task GenerateFluxSystemKustomization(KSailCluster config, string outputDir
Console.WriteLine($"✚ generating '{outputDirectory}'");
var kustomization = new KustomizeKustomization
{
Resources = config.Spec.CLI.InitOptions.KustomizeFlows.Select(flow => $"{flow.Replace('/', '-')}.yaml").ToList(),
Resources = config.Spec.Project.KustomizeFlows.Select(flow => $"{flow.Replace('/', '-')}.yaml").ToList(),
Components = config.Spec.CLI.InitOptions.Components ?
[
"../../../components/flux-kustomization-post-build-variables-label",
Expand Down Expand Up @@ -106,7 +106,7 @@ async Task GenerateFluxSystemFluxKustomization(KSailCluster config, string outpu
Kind = FluxSource.OCIRepository,
Name = "flux-system"
},
Path = config.Spec.CLI.InitOptions.KustomizeHooks.IsNullOrEmpty() ? flow : $"{config.Spec.CLI.InitOptions.KustomizeHooks.First()}/{flow}",
Path = config.Spec.Project.KustomizeHooks.IsNullOrEmpty() ? flow : $"{config.Spec.Project.KustomizeHooks.First()}/{flow}",
Prune = true,
Wait = true,
Decryption = config.Spec.Project.Sops && !config.Spec.CLI.InitOptions.Components ?
Expand Down Expand Up @@ -144,7 +144,7 @@ IEnumerable<FluxKustomizationSpecPostBuildSubstituteFrom> GetSubstituteFroms(KSa
Name = $"variables-sensitive-cluster"
}
};
foreach (string hook in config.Spec.CLI.InitOptions.KustomizeHooks)
foreach (string hook in config.Spec.Project.KustomizeHooks)
{
substituteList.Add(new FluxKustomizationSpecPostBuildSubstituteFrom
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class HelmReleaseGenerator

internal async Task GenerateAsync(KSailCluster config, CancellationToken cancellationToken = default)
{
string appsPath = Path.Combine(config.Spec.Project.ManifestsDirectory, config.Spec.CLI.InitOptions.KustomizeHooks.Last(), "apps");
string infrastructurePath = Path.Combine(config.Spec.Project.ManifestsDirectory, config.Spec.CLI.InitOptions.KustomizeHooks.Last(), "infrastructure");
string appsPath = Path.Combine(config.Spec.Project.ManifestsDirectory, config.Spec.Project.KustomizeHooks.Last(), "apps");
string infrastructurePath = Path.Combine(config.Spec.Project.ManifestsDirectory, config.Spec.Project.KustomizeHooks.Last(), "infrastructure");
string infrastructureControllersPath = Path.Combine(infrastructurePath, "controllers");
if (!Directory.Exists(appsPath))
_ = Directory.CreateDirectory(appsPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ class KustomizeFlowGenerator
readonly KustomizeKustomizationGenerator _kustomizationGenerator = new();
internal async Task GenerateAsync(KSailCluster config, CancellationToken cancellationToken = default)
{
if (config.Spec.CLI.InitOptions.KustomizeHooks.IsNullOrEmpty())
if (config.Spec.Project.KustomizeHooks.IsNullOrEmpty())
{
config.Spec.CLI.InitOptions.KustomizeHooks = [""];
config.Spec.Project.KustomizeHooks = [""];
}
foreach (string currentHook in config.Spec.CLI.InitOptions.KustomizeHooks)
foreach (string currentHook in config.Spec.Project.KustomizeHooks)
{
foreach (string currentFlow in config.Spec.CLI.InitOptions.KustomizeFlows)
foreach (string currentFlow in config.Spec.Project.KustomizeFlows)
{
await GenerateKustomizeFlowHook(config, currentHook, currentFlow, cancellationToken).ConfigureAwait(false);
}
}
if (config.Spec.CLI.InitOptions.PostBuildVariables)
{
foreach (string hook in config.Spec.CLI.InitOptions.KustomizeHooks)
foreach (string hook in config.Spec.Project.KustomizeHooks)
{
await GenerateKustomizeFlowHook(config, hook, "variables", cancellationToken).ConfigureAwait(false);
}
Expand All @@ -47,12 +47,12 @@ async Task GenerateKustomizeFlowHook(KSailCluster config, string currentHook, st
KustomizeKustomization? kustomization;
if (currentFlow == "variables")
{
string pathToNextFlow = currentHook == config.Spec.CLI.InitOptions.KustomizeHooks.Last() ?
string pathToNextFlow = currentHook == config.Spec.Project.KustomizeHooks.Last() ?
"" :
Path.Combine(
relativeRoot,
$"{config.Spec.CLI.InitOptions.KustomizeHooks.ElementAt(
Array.IndexOf(config.Spec.CLI.InitOptions.KustomizeHooks.ToArray(), currentHook) + 1
$"{config.Spec.Project.KustomizeHooks.ElementAt(
Array.IndexOf(config.Spec.Project.KustomizeHooks.ToArray(), currentHook) + 1
)}/{currentFlow}"
);
kustomization = new KustomizeKustomization
Expand All @@ -72,7 +72,7 @@ async Task GenerateKustomizeFlowHook(KSailCluster config, string currentHook, st
}
else
{
kustomization = currentHook == config.Spec.CLI.InitOptions.KustomizeHooks.Last()
kustomization = currentHook == config.Spec.Project.KustomizeHooks.Last()
? new KustomizeKustomization
{
Resources = config.Spec.CLI.InitOptions.HelmReleases && currentFlow == "infrastructure/controllers" ? ["cert-manager", "traefik"] :
Expand All @@ -86,7 +86,7 @@ async Task GenerateKustomizeFlowHook(KSailCluster config, string currentHook, st
}
: new KustomizeKustomization
{
Resources = [Path.Combine(relativeRoot, $"{config.Spec.CLI.InitOptions.KustomizeHooks.ElementAt(Array.IndexOf(config.Spec.CLI.InitOptions.KustomizeHooks.ToArray(), currentHook) + 1)}/{currentFlow}")],
Resources = [Path.Combine(relativeRoot, $"{config.Spec.Project.KustomizeHooks.ElementAt(Array.IndexOf(config.Spec.Project.KustomizeHooks.ToArray(), currentHook) + 1)}/{currentFlow}")],
Components = config.Spec.CLI.InitOptions.Components ?
[
Path.Combine(relativeRoot, "components/helm-release-crds-label"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class VariablesGenerator
readonly SecretGenerator _secretGenerator = new();
internal async Task GenerateAsync(KSailCluster config, CancellationToken cancellationToken = default)
{
foreach (string hook in config.Spec.CLI.InitOptions.KustomizeHooks)
foreach (string hook in config.Spec.Project.KustomizeHooks)
{
string hookPath = Path.Combine(config.Spec.CLI.InitOptions.OutputDirectory, "k8s", hook, "variables");
string name = hook.Replace("/", "-", StringComparison.Ordinal);
Expand Down
2 changes: 1 addition & 1 deletion src/KSail/Commands/Up/Handlers/KSailUpCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal async Task<int> HandleAsync(CancellationToken cancellationToken = defau
if (_config.Spec.CLI.UpOptions.Reconcile)
{
Console.WriteLine("🔄 Reconciling kustomizations");
await _gitOpsProvisioner.ReconcileAsync(_config.Spec.Connection.Timeout, cancellationToken).ConfigureAwait(false);
await _gitOpsProvisioner.ReconcileAsync(_config.Spec.Project.KustomizeFlows.Reverse().ToArray(), _config.Spec.Connection.Timeout, cancellationToken).ConfigureAwait(false);
Console.WriteLine();
}
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal async Task<bool> HandleAsync(CancellationToken cancellationToken = defa
if (_config.Spec.CLI.UpdateOptions.Reconcile)
{
Console.WriteLine("🔄 Reconciling changes");
await _gitOpsProvisioner.ReconcileAsync(_config.Spec.Connection.Timeout, cancellationToken).ConfigureAwait(false);
await _gitOpsProvisioner.ReconcileAsync(_config.Spec.Project.KustomizeFlows.Reverse().ToArray(), _config.Spec.Connection.Timeout, cancellationToken).ConfigureAwait(false);
}
Console.WriteLine();
return true;
Expand Down
10 changes: 5 additions & 5 deletions src/KSail/KSail.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
<PackageReference Include="Devantler.KubernetesGenerator.Flux" Version="1.0.1" />
<PackageReference Include="Devantler.KubernetesGenerator.K3d" Version="1.0.1" />
<PackageReference Include="Devantler.KubernetesGenerator.Kind" Version="1.0.1" />
<PackageReference Include="Devantler.KubernetesProvisioner.Cluster.K3d" Version="1.0.8" />
<PackageReference Include="Devantler.KubernetesProvisioner.Cluster.Kind" Version="1.0.8" />
<PackageReference Include="Devantler.KubernetesProvisioner.GitOps.Flux" Version="1.0.8" />
<PackageReference Include="Devantler.KubernetesValidator.ClientSide.Schemas" Version="1.0.3" />
<PackageReference Include="Devantler.KubernetesValidator.ClientSide.YamlSyntax" Version="1.0.3" />
<PackageReference Include="Devantler.KubernetesProvisioner.Cluster.K3d" Version="1.0.9" />
<PackageReference Include="Devantler.KubernetesProvisioner.Cluster.Kind" Version="1.0.9" />
<PackageReference Include="Devantler.KubernetesProvisioner.GitOps.Flux" Version="1.0.9" />
<PackageReference Include="Devantler.KubernetesValidator.ClientSide.Schemas" Version="1.0.4" />
<PackageReference Include="Devantler.KubernetesValidator.ClientSide.YamlSyntax" Version="1.0.4" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ spec:
manifestsDirectory: ./k8s
kustomizationDirectory: ./clusters/my-cluster/flux-system
configPath: ./k3d-config.yaml
kustomizeFlows:
- apps
- infrastructure
- infrastructure/controllers
kustomizeHooks: []
distribution: k3d
gitOpsTool: flux
containerEngine: docker
Expand Down Expand Up @@ -77,11 +82,6 @@ spec:
postBuildVariables: true
outputDirectory: ./
template: simple
kustomizeFlows:
- apps
- infrastructure
- infrastructure/controllers
kustomizeHooks: []
lintOptions: {}
listOptions:
all: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ spec:
manifestsDirectory: ./k8s
kustomizationDirectory: ./k8s/clusters/ksail-default/flux-system
configPath: kind-config.yaml
kustomizeFlows:
- apps
- infrastructure
- infrastructure/controllers
kustomizeHooks: []
distribution: kind
gitOpsTool: flux
containerEngine: docker
Expand Down Expand Up @@ -77,11 +82,6 @@ spec:
postBuildVariables: false
outputDirectory: ./
template: simple
kustomizeFlows:
- apps
- infrastructure
- infrastructure/controllers
kustomizeHooks: []
lintOptions: {}
listOptions:
all: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
ManifestsDirectory: ./k8s,
KustomizationDirectory: ./k8s/clusters/ksail-default/flux-system,
ConfigPath: kind-config.yaml,
KustomizeFlows: [
apps,
infrastructure,
infrastructure/controllers
],
Distribution: kind,
Sops: false
},
Expand Down Expand Up @@ -90,12 +95,7 @@
Components: false,
HelmReleases: false,
PostBuildVariables: false,
OutputDirectory: ./,
KustomizeFlows: [
apps,
infrastructure,
infrastructure/controllers
]
OutputDirectory: ./
},
LintOptions: {},
ListOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
ManifestsDirectory: ./k8s,
KustomizationDirectory: ./clusters/my-cluster/flux-system,
ConfigPath: ./k3d-config.yaml,
KustomizeFlows: [
apps,
infrastructure,
infrastructure/controllers
],
Sops: true
},
Registries: [
Expand Down Expand Up @@ -89,12 +94,7 @@
Components: true,
HelmReleases: true,
PostBuildVariables: true,
OutputDirectory: ./,
KustomizeFlows: [
apps,
infrastructure,
infrastructure/controllers
]
OutputDirectory: ./
},
LintOptions: {},
ListOptions: {
Expand Down
1 change: 0 additions & 1 deletion tests/KSail.Tests/Commands/Debug/KSailDebugCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace KSail.Tests.Commands.Debug;
/// <summary>
/// Tests for the <see cref="KSailDebugCommand"/> class.
/// </summary>
[Collection("KSail.Tests")]
public class KSailDebugCommandTests : IAsyncLifetime
{
/// <inheritdoc/>
Expand Down
1 change: 0 additions & 1 deletion tests/KSail.Tests/Commands/Down/KSailDownCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace KSail.Tests.Commands.Down;
/// <summary>
/// Tests for the <see cref="KSailDownCommand"/> class.
/// </summary>
[Collection("KSail.Tests")]
public class KSailDownCommandTests : IAsyncLifetime
{
/// <inheritdoc/>
Expand Down
1 change: 0 additions & 1 deletion tests/KSail.Tests/Commands/Gen/KSailGenCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace KSail.Tests.Commands.Gen;
/// <summary>
/// Tests for the <see cref="KSailGenCommand"/> class.
/// </summary>
[Collection("KSail.Tests")]
public class KSailGenCommandTests : IAsyncLifetime
{
/// <inheritdoc/>
Expand Down
10 changes: 5 additions & 5 deletions tests/KSail.Tests/Commands/Gen/ksail-config.yaml.verified.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ spec:
manifestsDirectory: ./k8s
kustomizationDirectory: ./k8s/clusters/ksail-default/flux-system
configPath: kind-config.yaml
kustomizeFlows:
- apps
- infrastructure
- infrastructure/controllers
kustomizeHooks: []
distribution: kind
gitOpsTool: flux
containerEngine: docker
Expand Down Expand Up @@ -77,11 +82,6 @@ spec:
postBuildVariables: false
outputDirectory: ./
template: simple
kustomizeFlows:
- apps
- infrastructure
- infrastructure/controllers
kustomizeHooks: []
lintOptions: {}
listOptions:
all: false
Expand Down
1 change: 0 additions & 1 deletion tests/KSail.Tests/Commands/Init/KSailInitCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace KSail.Tests.Commands.Init;
/// <summary>
/// Tests for the <see cref="KSailInitCommand"/> class.
/// </summary>
[Collection("KSail.Tests")]
public class KSailInitCommandTests : IAsyncLifetime
{
/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ spec:
manifestsDirectory: ./k8s
kustomizationDirectory: ./k8s/clusters/ksail-default/flux-system
configPath: ./kind-config.yaml
kustomizeFlows:
- apps
- infrastructure
- infrastructure/controllers
kustomizeHooks: []
distribution: kind
gitOpsTool: flux
containerEngine: docker
Expand Down Expand Up @@ -77,11 +82,6 @@ spec:
postBuildVariables: true
outputDirectory: {TempPath}ksail-init-advanced
template: simple
kustomizeFlows:
- apps
- infrastructure
- infrastructure/controllers
kustomizeHooks: []
lintOptions: {}
listOptions:
all: false
Expand Down
Loading

0 comments on commit 629469b

Please sign in to comment.