generated from devantler/dotnet-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added FluxProvisioner * Update README Tree workflow to include Flux GitOps tooling dependency * Refactor FluxProvisioner import statement * Fix FluxProvisioner reconciliation * Fix Flux_InstallsAndReconciles_KustomizationsAsync to use the kube context for all calls
- Loading branch information
Showing
10 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
...tler.KubernetesProvisioner.GitOps.Core/Devantler.KubernetesProvisioner.GitOps.Core.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AnalysisLevel>preview-all</AnalysisLevel> | ||
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors> | ||
</PropertyGroup> | ||
|
||
</Project> |
26 changes: 26 additions & 0 deletions
26
Devantler.KubernetesProvisioner.GitOps.Core/IKubernetesGitOpsProvisioner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Devantler.KubernetesProvisioner.GitOps.Core; | ||
|
||
/// <summary> | ||
/// A Kubernetes GitOps provisioner. | ||
/// </summary> | ||
public interface IKubernetesGitOpsProvisioner | ||
{ | ||
/// <summary> | ||
/// The Kubernetes context. | ||
/// </summary> | ||
public string? Context { get; set; } | ||
/// <summary> | ||
/// Install the GitOps tooling on the Kubernetes cluster. | ||
/// </summary> | ||
public Task InstallAsync(CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Uninstall the GitOps tooling from the Kubernetes cluster. | ||
/// </summary> | ||
public Task UninstallAsync(CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Reconcile resources on the Kubernetes cluster. | ||
/// </summary> | ||
public Task ReconcileAsync(CancellationToken cancellationToken = default); | ||
} |
36 changes: 36 additions & 0 deletions
36
...tesProvisioner.GitOps.Flux.Tests/Devantler.KubernetesProvisioner.GitOps.Flux.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AnalysisLevel>preview-all</AnalysisLevel> | ||
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors> | ||
<UseCurrentRuntimeIdentifier>true</UseCurrentRuntimeIdentifier> | ||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.2" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
<PackageReference Include="xunit" Version="2.9.2" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Devantler.KubernetesProvisioner.GitOps.Flux\Devantler.KubernetesProvisioner.GitOps.Flux.csproj" /> | ||
<ProjectReference Include="..\Devantler.KubernetesProvisioner.Cluster.Kind\Devantler.KubernetesProvisioner.Cluster.Kind.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="assets/kind-config.yaml" CopyToOutputDirectory="PreserveNewest" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="Xunit" /> | ||
</ItemGroup> | ||
</Project> |
37 changes: 37 additions & 0 deletions
37
Devantler.KubernetesProvisioner.GitOps.Flux.Tests/FluxProvisionerTests/AllMethodsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Devantler.KindCLI; | ||
|
||
namespace Devantler.KubernetesProvisioner.GitOps.Flux.Tests.FluxProvisionerTests; | ||
|
||
/// <summary> | ||
/// Tests for all methods in the <see cref="FluxProvisioner"/> class. | ||
/// </summary> | ||
[Collection("Flux")] | ||
public class AllMethodsTests | ||
{ | ||
/// <summary> | ||
/// Test to verify that flux installs and reconciles kustomizations. | ||
/// </summary> | ||
/// <returns></returns> | ||
[Fact] | ||
public async Task Flux_InstallsAndReconciles_KustomizationsAsync() | ||
{ | ||
// Arrange | ||
string clusterName = "test-flux-cluster"; | ||
string context = "kind-" + clusterName; | ||
string configPath = Path.Combine(AppContext.BaseDirectory, "assets/kind-config.yaml"); | ||
var fluxProvisioner = new FluxProvisioner(context); | ||
var cancellationToken = new CancellationToken(); | ||
|
||
// Act | ||
await Kind.DeleteClusterAsync(clusterName, cancellationToken); | ||
await Kind.CreateClusterAsync(clusterName, configPath, cancellationToken); | ||
await fluxProvisioner.InstallAsync(cancellationToken); | ||
await FluxCLI.Flux.CreateOCISourceAsync("podinfo", new Uri("oci://ghcr.io/stefanprodan/manifests/podinfo"), context, cancellationToken: cancellationToken); | ||
await FluxCLI.Flux.CreateKustomizationAsync("podinfo", "OCIRepository/podinfo", "", context, cancellationToken: cancellationToken); | ||
await fluxProvisioner.ReconcileAsync(cancellationToken); | ||
await fluxProvisioner.UninstallAsync(cancellationToken); | ||
|
||
// Cleanup | ||
await Kind.DeleteClusterAsync(clusterName, cancellationToken); | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
Devantler.KubernetesProvisioner.GitOps.Flux.Tests/assets/kind-config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
kind: Cluster | ||
apiVersion: kind.x-k8s.io/v1alpha4 |
23 changes: 23 additions & 0 deletions
23
...tler.KubernetesProvisioner.GitOps.Flux/Devantler.KubernetesProvisioner.GitOps.Flux.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AnalysisLevel>preview-all</AnalysisLevel> | ||
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild> | ||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Devantler.FluxCLI" Version="0.0.4" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Devantler.KubernetesProvisioner.GitOps.Core\Devantler.KubernetesProvisioner.GitOps.Core.csproj" /> | ||
<ProjectReference Include="..\Devantler.KubernetesProvisioner.Resources.Native\Devantler.KubernetesProvisioner.Resources.Native.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
51 changes: 51 additions & 0 deletions
51
Devantler.KubernetesProvisioner.GitOps.Flux/FluxProvisioner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Devantler.KubernetesProvisioner.GitOps.Core; | ||
using Devantler.KubernetesProvisioner.Resources.Native; | ||
using k8s; | ||
using k8s.Models; | ||
|
||
namespace Devantler.KubernetesProvisioner.GitOps.Flux; | ||
|
||
/// <summary> | ||
/// A Kubernetes GitOps provisioner using Flux. | ||
/// </summary> | ||
/// <remarks> | ||
/// Initializes a new instance of the <see cref="FluxProvisioner"/> class. | ||
/// </remarks> | ||
/// <param name="context"></param> | ||
public class FluxProvisioner(string? context = default) : IKubernetesGitOpsProvisioner | ||
{ | ||
|
||
/// <inheritdoc/> | ||
public string? Context { get; set; } = context; | ||
|
||
/// <summary> | ||
/// Install Flux on the Kubernetes cluster. | ||
/// </summary> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public async Task InstallAsync(CancellationToken cancellationToken = default) => | ||
await FluxCLI.Flux.InstallAsync(Context, cancellationToken).ConfigureAwait(false); | ||
|
||
/// <summary> | ||
/// Reconcile resources on the Kubernetes cluster. | ||
/// </summary> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public async Task ReconcileAsync(CancellationToken cancellationToken = default) | ||
{ | ||
using var kubernetesResourceProvisioner = new KubernetesResourceProvisioner(Context); | ||
var kustomizations = await kubernetesResourceProvisioner.CustomObjects.ListNamespacedCustomObjectAsync<V1CustomResourceDefinitionList>("kustomize.toolkit.fluxcd.io", "v1", "flux-system", "kustomizations", cancellationToken: cancellationToken).ConfigureAwait(false); | ||
foreach (var kustomization in kustomizations.Items) | ||
{ | ||
await FluxCLI.Flux.ReconcileKustomizationAsync(kustomization.Metadata.Name, Context, withSource: true, cancellationToken: cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Uninstall Flux from the Kubernetes cluster. | ||
/// </summary> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
public async Task UninstallAsync(CancellationToken cancellationToken = default) => | ||
await FluxCLI.Flux.UninstallAsync(Context, cancellationToken).ConfigureAwait(false); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters