From 7fb876b2467eba78fb7ec630f54900c4cfad4289 Mon Sep 17 00:00:00 2001 From: devantler Date: Mon, 18 Sep 2023 00:41:22 +0200 Subject: [PATCH] =?UTF-8?q?Pulumi=20+=20Flux=20=3D=20=F0=9F=94=AE?= =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ansible/.gitkeep | 5 - .../base/traefik-ingress/release.yaml | 3 + pulumi/.gitignore | 353 ++++++++++++++++++ pulumi/Devantler.Pulumi.Homelab.sln | 34 ++ pulumi/Pulumi.local.yaml | 2 + pulumi/Pulumi.yaml | 4 + .../Devantler.Pulumi.Homelab/Apps/.gitkeep | 0 .../Devantler.Pulumi.Homelab/CRDs/.gitkeep | 0 .../Devantler.Pulumi.Homelab.csproj | 23 ++ .../FluxSources/HomelabFluxSource.cs | 60 +++ .../Infrastructure/.gitkeep | 0 .../src/Devantler.Pulumi.Homelab/Program.cs | 25 ++ pulumi/src/Devantler.Pulumi/Attributes/App.cs | 6 + pulumi/src/Devantler.Pulumi/Attributes/CRD.cs | 6 + .../Devantler.Pulumi/Attributes/FluxSource.cs | 6 + .../Attributes/Infrastructure.cs | 6 + .../Attributes/ResourceAttribute.cs | 19 + .../Devantler.Pulumi/Devantler.Pulumi.csproj | 16 + .../Provisioners/IKubernetesProvisioner.cs | 34 ++ .../Provisioners/IProvisionable.cs | 15 + .../Provisioners/IProvisioner.cs | 8 + .../Provisioners/KubernetesProvisioner.cs | 102 +++++ scripts/bootstrap-local.sh | 10 +- scripts/bootstrap-production.sh | 2 + 24 files changed, 730 insertions(+), 9 deletions(-) delete mode 100644 ansible/.gitkeep create mode 100644 pulumi/.gitignore create mode 100644 pulumi/Devantler.Pulumi.Homelab.sln create mode 100644 pulumi/Pulumi.local.yaml create mode 100644 pulumi/Pulumi.yaml create mode 100644 pulumi/src/Devantler.Pulumi.Homelab/Apps/.gitkeep create mode 100644 pulumi/src/Devantler.Pulumi.Homelab/CRDs/.gitkeep create mode 100644 pulumi/src/Devantler.Pulumi.Homelab/Devantler.Pulumi.Homelab.csproj create mode 100644 pulumi/src/Devantler.Pulumi.Homelab/FluxSources/HomelabFluxSource.cs create mode 100644 pulumi/src/Devantler.Pulumi.Homelab/Infrastructure/.gitkeep create mode 100644 pulumi/src/Devantler.Pulumi.Homelab/Program.cs create mode 100644 pulumi/src/Devantler.Pulumi/Attributes/App.cs create mode 100644 pulumi/src/Devantler.Pulumi/Attributes/CRD.cs create mode 100644 pulumi/src/Devantler.Pulumi/Attributes/FluxSource.cs create mode 100644 pulumi/src/Devantler.Pulumi/Attributes/Infrastructure.cs create mode 100644 pulumi/src/Devantler.Pulumi/Attributes/ResourceAttribute.cs create mode 100644 pulumi/src/Devantler.Pulumi/Devantler.Pulumi.csproj create mode 100644 pulumi/src/Devantler.Pulumi/Provisioners/IKubernetesProvisioner.cs create mode 100644 pulumi/src/Devantler.Pulumi/Provisioners/IProvisionable.cs create mode 100644 pulumi/src/Devantler.Pulumi/Provisioners/IProvisioner.cs create mode 100644 pulumi/src/Devantler.Pulumi/Provisioners/KubernetesProvisioner.cs diff --git a/ansible/.gitkeep b/ansible/.gitkeep deleted file mode 100644 index 731a3b7ba..000000000 --- a/ansible/.gitkeep +++ /dev/null @@ -1,5 +0,0 @@ -# Notes - -This folder should contain playbooks to provision a K3s cluster to debian based hosts. - -The K3s cluster should be provisioned without traefik, as it will be provisioned by FluxCD. diff --git a/k8s/infrastructure/base/traefik-ingress/release.yaml b/k8s/infrastructure/base/traefik-ingress/release.yaml index 9c7b91c8d..f82379789 100644 --- a/k8s/infrastructure/base/traefik-ingress/release.yaml +++ b/k8s/infrastructure/base/traefik-ingress/release.yaml @@ -26,6 +26,9 @@ spec: entryPoints: - websecure matchRule: Host(`traefik.${domain}`) + service: + spec: + loadBalancerIP: "51.124.251.55" tlsStore: default: defaultCertificate: diff --git a/pulumi/.gitignore b/pulumi/.gitignore new file mode 100644 index 000000000..e64527066 --- /dev/null +++ b/pulumi/.gitignore @@ -0,0 +1,353 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ diff --git a/pulumi/Devantler.Pulumi.Homelab.sln b/pulumi/Devantler.Pulumi.Homelab.sln new file mode 100644 index 000000000..30d751cc4 --- /dev/null +++ b/pulumi/Devantler.Pulumi.Homelab.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{32DE6A7F-CB44-4787-986B-AC80F3BCD5EE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Devantler.Pulumi.Homelab", "src\Devantler.Pulumi.Homelab\Devantler.Pulumi.Homelab.csproj", "{4B2E2AD8-ABA5-428E-BCB2-7D481A14AF21}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Devantler.Pulumi", "src\Devantler.Pulumi\Devantler.Pulumi.csproj", "{327EB4FD-1284-4A66-97D2-D86AA871F9B3}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4B2E2AD8-ABA5-428E-BCB2-7D481A14AF21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B2E2AD8-ABA5-428E-BCB2-7D481A14AF21}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B2E2AD8-ABA5-428E-BCB2-7D481A14AF21}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B2E2AD8-ABA5-428E-BCB2-7D481A14AF21}.Release|Any CPU.Build.0 = Release|Any CPU + {327EB4FD-1284-4A66-97D2-D86AA871F9B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {327EB4FD-1284-4A66-97D2-D86AA871F9B3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {327EB4FD-1284-4A66-97D2-D86AA871F9B3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {327EB4FD-1284-4A66-97D2-D86AA871F9B3}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {4B2E2AD8-ABA5-428E-BCB2-7D481A14AF21} = {32DE6A7F-CB44-4787-986B-AC80F3BCD5EE} + {327EB4FD-1284-4A66-97D2-D86AA871F9B3} = {32DE6A7F-CB44-4787-986B-AC80F3BCD5EE} + EndGlobalSection +EndGlobal diff --git a/pulumi/Pulumi.local.yaml b/pulumi/Pulumi.local.yaml new file mode 100644 index 000000000..977e5f47b --- /dev/null +++ b/pulumi/Pulumi.local.yaml @@ -0,0 +1,2 @@ +config: + Devantler.Pulumi.Homelab:flux_branch: main diff --git a/pulumi/Pulumi.yaml b/pulumi/Pulumi.yaml new file mode 100644 index 000000000..e22c303b4 --- /dev/null +++ b/pulumi/Pulumi.yaml @@ -0,0 +1,4 @@ +name: Devantler.Pulumi.Homelab +runtime: dotnet +main: src/Devantler.Pulumi.Homelab/Devantler.Pulumi.Homelab.csproj +description: A minimal Kubernetes C# Pulumi program diff --git a/pulumi/src/Devantler.Pulumi.Homelab/Apps/.gitkeep b/pulumi/src/Devantler.Pulumi.Homelab/Apps/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/pulumi/src/Devantler.Pulumi.Homelab/CRDs/.gitkeep b/pulumi/src/Devantler.Pulumi.Homelab/CRDs/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/pulumi/src/Devantler.Pulumi.Homelab/Devantler.Pulumi.Homelab.csproj b/pulumi/src/Devantler.Pulumi.Homelab/Devantler.Pulumi.Homelab.csproj new file mode 100644 index 000000000..6e33ec08b --- /dev/null +++ b/pulumi/src/Devantler.Pulumi.Homelab/Devantler.Pulumi.Homelab.csproj @@ -0,0 +1,23 @@ + + + + Exe + net7.0 + latest + enable + enable + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi.Homelab/FluxSources/HomelabFluxSource.cs b/pulumi/src/Devantler.Pulumi.Homelab/FluxSources/HomelabFluxSource.cs new file mode 100644 index 000000000..ffd6c994e --- /dev/null +++ b/pulumi/src/Devantler.Pulumi.Homelab/FluxSources/HomelabFluxSource.cs @@ -0,0 +1,60 @@ +using Devantler.Pulumi.Attributes; +using Devantler.Pulumi.Provisioners; +using Pulumi; +using Pulumi.Flux; +using Pulumi.Flux.Inputs; +using Config = Pulumi.Config; +using Provider = Pulumi.Flux.Provider; +using ProviderArgs = Pulumi.Flux.ProviderArgs; + +namespace Devantler.Pulumi.Homelab.FluxSources; + +/// +/// Flux source for my homelab. +/// +[FluxSource] +public class HomelabFluxSource : IProvisionable +{ + /// + public void Provision(Config config) + { + string githubOwner = "devantler"; + string repoName = "homelab"; + string branch = config.Require("flux_branch"); + string path = $"k8s/clusters/{Deployment.Instance.StackName}"; + + // Read the private key from the local filesystems home directory. + string sshPrivateKey = File.ReadAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh", "id_rsa")); + + var fluxProvider = new Provider("flux", new ProviderArgs + { + Kubernetes = new ProviderKubernetesArgs + { + ConfigPath = "~/.kube/config", + ConfigContextCluster = "k3d-cluster-local" + }, + Git = new ProviderGitArgs + { + Url = $"ssh://github.com/{githubOwner}/{repoName}", + Branch = branch, + Ssh = new ProviderGitSshArgs() + { + Username = "git", + PrivateKey = sshPrivateKey, + Password = "Jepfa202042" + } + } + }); + + _ = new FluxBootstrapGit("flux", + new FluxBootstrapGitArgs + { + Path = path + }, + new CustomResourceOptions + { + Provider = fluxProvider, + } + ); + } +} \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi.Homelab/Infrastructure/.gitkeep b/pulumi/src/Devantler.Pulumi.Homelab/Infrastructure/.gitkeep new file mode 100644 index 000000000..e69de29bb diff --git a/pulumi/src/Devantler.Pulumi.Homelab/Program.cs b/pulumi/src/Devantler.Pulumi.Homelab/Program.cs new file mode 100644 index 000000000..bdc465b94 --- /dev/null +++ b/pulumi/src/Devantler.Pulumi.Homelab/Program.cs @@ -0,0 +1,25 @@ +using Devantler.Pulumi.Provisioners; +using Pulumi; + +return await Deployment.RunAsync(() => +{ + var config = new Config(); + + KubernetesProvisioner.ProvisionFluxSources(config); + KubernetesProvisioner.ProvisionInfrastructure(config); + KubernetesProvisioner.ProvisionCRDs(config); + KubernetesProvisioner.ProvisionApps(config); +}); + + + + + + + + + + + + + diff --git a/pulumi/src/Devantler.Pulumi/Attributes/App.cs b/pulumi/src/Devantler.Pulumi/Attributes/App.cs new file mode 100644 index 000000000..7d10a4143 --- /dev/null +++ b/pulumi/src/Devantler.Pulumi/Attributes/App.cs @@ -0,0 +1,6 @@ +namespace Devantler.Pulumi.Attributes; + +/// +/// Attribute to mark a resource as an app. +/// +public class App : ResourceAttribute { } \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi/Attributes/CRD.cs b/pulumi/src/Devantler.Pulumi/Attributes/CRD.cs new file mode 100644 index 000000000..d8af04fd5 --- /dev/null +++ b/pulumi/src/Devantler.Pulumi/Attributes/CRD.cs @@ -0,0 +1,6 @@ +namespace Devantler.Pulumi.Attributes; + +/// +/// Attribute to mark a resource as a CRD. +/// +public class CRD : ResourceAttribute { } \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi/Attributes/FluxSource.cs b/pulumi/src/Devantler.Pulumi/Attributes/FluxSource.cs new file mode 100644 index 000000000..d2e2d1761 --- /dev/null +++ b/pulumi/src/Devantler.Pulumi/Attributes/FluxSource.cs @@ -0,0 +1,6 @@ +namespace Devantler.Pulumi.Attributes; + +/// +/// Attribute to mark a resource as a Flux source. +/// +public class FluxSource : ResourceAttribute { } \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi/Attributes/Infrastructure.cs b/pulumi/src/Devantler.Pulumi/Attributes/Infrastructure.cs new file mode 100644 index 000000000..464a7266e --- /dev/null +++ b/pulumi/src/Devantler.Pulumi/Attributes/Infrastructure.cs @@ -0,0 +1,6 @@ +namespace Devantler.Pulumi.Attributes; + +/// +/// Attribute to mark a resource as infrastructure. +/// +public class Infrastructure : ResourceAttribute { } \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi/Attributes/ResourceAttribute.cs b/pulumi/src/Devantler.Pulumi/Attributes/ResourceAttribute.cs new file mode 100644 index 000000000..ab3cc2fc9 --- /dev/null +++ b/pulumi/src/Devantler.Pulumi/Attributes/ResourceAttribute.cs @@ -0,0 +1,19 @@ +namespace Devantler.Pulumi.Attributes; + +/// +/// Attribute base for resources. +/// +public abstract class ResourceAttribute : Attribute +{ + /// + /// List of other resources (of the same type) that this resource depends on. + /// + public string[] DependsOn { get; set; } + + /// + /// Constructor for the AppAttribute. + /// + /// List of other apps that this app depends on. + public ResourceAttribute(params string[] dependencies) + => DependsOn = dependencies; +} \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi/Devantler.Pulumi.csproj b/pulumi/src/Devantler.Pulumi/Devantler.Pulumi.csproj new file mode 100644 index 000000000..7549dc4da --- /dev/null +++ b/pulumi/src/Devantler.Pulumi/Devantler.Pulumi.csproj @@ -0,0 +1,16 @@ + + + + net7.0 + latest + enable + enable + true + true + + + + + + + \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi/Provisioners/IKubernetesProvisioner.cs b/pulumi/src/Devantler.Pulumi/Provisioners/IKubernetesProvisioner.cs new file mode 100644 index 000000000..62335820e --- /dev/null +++ b/pulumi/src/Devantler.Pulumi/Provisioners/IKubernetesProvisioner.cs @@ -0,0 +1,34 @@ +using Pulumi; + +namespace Devantler.Pulumi.Provisioners; + + +/// +/// Interface for a provisioner that can provision Kubernetes resources. +/// +public interface IKubernetesProvisioner : IProvisioner +{ + /// + /// Provision Flux sources. + /// + /// + void ProvisionFluxSources(Config config); + + /// + /// Provision infrastructure. + /// + /// + void ProvisionInfrastructure(Config config); + + /// + /// Provision CRDs. + /// + /// + void ProvisionCRDs(Config config); + + /// + /// Provision apps. + /// + /// + void ProvisionApps(Config config); +} \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi/Provisioners/IProvisionable.cs b/pulumi/src/Devantler.Pulumi/Provisioners/IProvisionable.cs new file mode 100644 index 000000000..60ba6e0ce --- /dev/null +++ b/pulumi/src/Devantler.Pulumi/Provisioners/IProvisionable.cs @@ -0,0 +1,15 @@ +using Pulumi; + +namespace Devantler.Pulumi.Provisioners; + +/// +/// Interface for a resource that can be provisioned by Pulumi. +/// +public interface IProvisionable +{ + /// + /// Provision the resource. + /// + /// + void Provision(Config config); +} \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi/Provisioners/IProvisioner.cs b/pulumi/src/Devantler.Pulumi/Provisioners/IProvisioner.cs new file mode 100644 index 000000000..a6614da5f --- /dev/null +++ b/pulumi/src/Devantler.Pulumi/Provisioners/IProvisioner.cs @@ -0,0 +1,8 @@ +namespace Devantler.Pulumi.Provisioners; + +/// +/// Interface for a provisioner that can provision Pulumi resources. +/// +public interface IProvisioner +{ +} \ No newline at end of file diff --git a/pulumi/src/Devantler.Pulumi/Provisioners/KubernetesProvisioner.cs b/pulumi/src/Devantler.Pulumi/Provisioners/KubernetesProvisioner.cs new file mode 100644 index 000000000..24940d844 --- /dev/null +++ b/pulumi/src/Devantler.Pulumi/Provisioners/KubernetesProvisioner.cs @@ -0,0 +1,102 @@ +using System.Reflection; +using Devantler.Pulumi.Attributes; +using Pulumi; + +namespace Devantler.Pulumi.Provisioners; + +/// +/// Default provisioner for Kubernetes resources. +/// +/// +/// This provisioner will provision any resources that implement the interface and are marked with one of the following attributes: +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +/// +public class KubernetesProvisioner : IProvisioner +{ + /// + /// Provision apps. + /// + /// + /// + public static void ProvisionApps(Config config) + { + var apps = GetResources(); + Provision(provisionables: apps, provisionedItems: new List(), config: config); + } + + /// + /// Provision CRDs. + /// + /// + /// + public static void ProvisionCRDs(Config config) + { + var crds = GetResources(); + Provision(provisionables: crds, provisionedItems: new List(), config: config); + } + + /// + /// Provision Flux sources. + /// + /// + /// + public static void ProvisionFluxSources(Config config) + { + var fluxSources = GetResources(); + Provision(provisionables: fluxSources, provisionedItems: new List(), config: config); + } + + /// + /// Provision infrastructure. + /// + /// + /// + public static void ProvisionInfrastructure(Config config) + { + var infrastructure = GetResources(); + Provision(provisionables: infrastructure, provisionedItems: new List(), config: config); + } + + static List<(IProvisionable? provisionable, string[]? dependsOn)> GetResources() + where T : ResourceAttribute + { + var assembly = Assembly.GetEntryAssembly() ?? throw new InvalidOperationException("Could not get entry assembly."); + return assembly + .GetTypes() + .Where(t => t.GetCustomAttribute() != null) + .Where(t => typeof(IProvisionable).IsAssignableFrom(t)) + .Select(t => (provisionable: Activator.CreateInstance(t) as IProvisionable, dependsOn: t.GetCustomAttribute()?.DependsOn)) + .ToList(); + } + + static void Provision(List<(IProvisionable? provisionable, string[]? dependsOn)> provisionables, List provisionedItems, Config config) + { + while (provisionables.Any()) + { + var (provisionable, dependsOn) = provisionables.FirstOrDefault(p => p.dependsOn == null || !p.dependsOn.Except(provisionedItems.Select(i => i.GetType().Name)).Any()); + if (provisionable != null) + { + provisionable.Provision(config); + provisionedItems.Add(provisionable); + } + else + { + throw new InvalidOperationException("Circular dependency detected."); + } + _ = provisionables.Remove((provisionable, dependsOn)); + } + } +} \ No newline at end of file diff --git a/scripts/bootstrap-local.sh b/scripts/bootstrap-local.sh index 46611225a..e5c6a409d 100755 --- a/scripts/bootstrap-local.sh +++ b/scripts/bootstrap-local.sh @@ -1,8 +1,10 @@ -branch=$(git rev-parse --abbrev-ref HEAD) +#!/bin/bash +pushd $(dirname "$0") >/dev/null echo "🚀 Provisioning local Kubernetes cluster" k3d cluster create cluster-local --config k3d-config.yaml -echo "🚀 Installing Flux" -flux check --pre -flux bootstrap github --owner=$GITHUB_USER --repository=homelab --path=./k8s/clusters/local --personal --branch=$branch +echo "🔮 Running Pulumi to provision infrastructure" +pushd ../pulumi >/dev/null +pulumi stack select local +pulumi up --yes diff --git a/scripts/bootstrap-production.sh b/scripts/bootstrap-production.sh index f887e952b..03a184e77 100755 --- a/scripts/bootstrap-production.sh +++ b/scripts/bootstrap-production.sh @@ -1,5 +1,7 @@ #!/bin/bash +echo " Create nodes" + echo "☸️ Provisioning Kubernetes cluster" microk8s install -y