-
Notifications
You must be signed in to change notification settings - Fork 174
feat: CommunityToolkit.Aspire.Hosting.K3s — k3s Kubernetes cluster hosting integration #1322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edmondshtogu
wants to merge
16
commits into
CommunityToolkit:main
Choose a base branch
from
edmondshtogu:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
983b1cd
feat: CommunityToolkit.Aspire.Hosting.K3s
edmondshtogu 4dda24b
feat: proper support for service endpoints, helm and manifests resources
edmondshtogu a2ce723
feat: support files for helm/manifest
edmondshtogu 2729a69
Merge branch 'main' into main
edmondshtogu bc271ec
Merge branch 'main' into main
edmondshtogu 0e5c9de
Merge branch 'main' into main
edmondshtogu d343ee3
compliting left work
edmondshtogu 0a4d13a
upgrade example aspire version
edmondshtogu f9a50d1
fix: copilot reviews
edmondshtogu da4bf9d
chore: exluding integration tests for win runners
edmondshtogu ff66be4
chore: update example comment
edmondshtogu 37c11e3
fix: copilot reviews
edmondshtogu 99297cd
fix: WaitForServiceReadyAsync service health check
edmondshtogu 7d34ddc
chore: addressing reviews
edmondshtogu 7473170
fix: copilot reviews
edmondshtogu 98bf921
fix: copilot reviews
edmondshtogu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
14 changes: 14 additions & 0 deletions
14
...nityToolkit.Aspire.Hosting.K3s.AppHost/CommunityToolkit.Aspire.Hosting.K3s.AppHost.csproj
This file contains hidden or 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="Aspire.AppHost.Sdk/13.3.0"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsAspireHost>true</IsAspireHost> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Hosting.K3s\CommunityToolkit.Aspire.Hosting.K3s.csproj" IsAspireProjectResource="false" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
38 changes: 38 additions & 0 deletions
38
examples/k3s/CommunityToolkit.Aspire.Hosting.K3s.AppHost/Program.cs
This file contains hidden or 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,38 @@ | ||
| // K3s hosting example | ||
| // ────────────────────────────────────────────────────────────────────────────── | ||
| // Prerequisites (host machine): | ||
| // • A container runtime that supports privileged Linux containers: | ||
| // - Linux: Docker Engine 20.10+ or rootful Podman 4.0+ | ||
| // - macOS / Windows: Docker Desktop (WSL2 / Hyper-V) | ||
| // No host-side helm or kubectl required — both run as containers. | ||
| // | ||
| // What this demonstrates: | ||
| // 1. A k3s cluster starts inside a Docker container. | ||
| // 2. podinfo is installed via Helm — a lightweight demo app. | ||
| // 3. A K3sServiceEndpointResource exposes the podinfo service: | ||
| // • Host processes reach it at http://localhost:{port} | ||
| // • DCP-network containers reach it at http://host.docker.internal:{port} | ||
| // 4. WithDataVolume keeps the cluster state alive across AppHost restarts. | ||
| // ────────────────────────────────────────────────────────────────────────────── | ||
|
|
||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var cluster = builder | ||
| .AddK3sCluster("k8s") | ||
| .WithDataVolume() | ||
| .WithLifetime(ContainerLifetime.Persistent); | ||
|
|
||
| var podinfo = cluster.AddHelmRelease( | ||
| name: "podinfo", | ||
| chart: "podinfo", | ||
| repo: "https://stefanprodan.github.io/podinfo", | ||
| version: "6.7.1", | ||
| @namespace: "podinfo"); | ||
|
|
||
| // Expose the podinfo service as an Aspire endpoint resource. | ||
| // WaitForCompletion waits for the helm install container to exit with code 0 | ||
| // before starting the port-forward — no NodePort required. | ||
| cluster.AddServiceEndpoint("podinfo-web", "podinfo", servicePort: 9898, @namespace: "podinfo") | ||
| .WaitForCompletion(podinfo); | ||
|
|
||
| builder.Build().Run(); |
30 changes: 30 additions & 0 deletions
30
examples/k3s/CommunityToolkit.Aspire.Hosting.K3s.AppHost/Properties/launchSettings.json
This file contains hidden or 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,30 @@ | ||
| { | ||
| "$schema": "https://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:53201;http://localhost:53202", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development", | ||
| "DOTNET_ENVIRONMENT": "Development", | ||
| "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:53203", | ||
| "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:53204" | ||
| } | ||
| }, | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:53202", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development", | ||
| "DOTNET_ENVIRONMENT": "Development", | ||
| "ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true", | ||
| "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:53205", | ||
| "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:53206" | ||
| } | ||
| } | ||
| } | ||
| } |
82 changes: 82 additions & 0 deletions
82
...ound/polyglot/TypeScript/CommunityToolkit.Aspire.Hosting.K3s/ValidationAppHost/apphost.ts
This file contains hidden or 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,82 @@ | ||
| import { createBuilder, ContainerLifetime } from './.modules/aspire.js'; | ||
|
|
||
| const builder = await createBuilder(); | ||
|
|
||
| // ── Runtime path (actually executed) ───────────────────────────────────────── | ||
| // Minimal cluster startup — validates that the core add/build/run path works. | ||
| const cluster = builder.addK3sCluster('k8s'); | ||
| const clusterResource = await cluster; | ||
| const _apiEndpoint = await clusterResource.apiEndpoint.get(); | ||
|
|
||
| // ── Compile-time coverage ───────────────────────────────────────────────────── | ||
| // Guards with false so these are type-checked but never executed. | ||
| // Covers the full exported API surface without requiring Docker/k3s in CI. | ||
| const includeCompileOnlyScenarios = false; | ||
|
|
||
| if (includeCompileOnlyScenarios) { | ||
|
|
||
| // ── Cluster configuration ──────────────────────────────────────────────── | ||
| const configuredCluster = builder.addK3sCluster('k8s-configured') | ||
| .withK3sVersion('v1.32.3-k3s1') | ||
| .withPodSubnet('10.42.0.0/16') | ||
| .withServiceSubnet('10.43.0.0/16') | ||
| .withDisabledComponent('traefik') | ||
| .withExtraArg('--write-kubeconfig-mode=644') | ||
| .withDataVolume({ name: 'k8s-data' }) | ||
| .withLifetime(ContainerLifetime.Persistent); | ||
|
|
||
| const configuredClusterResource = await configuredCluster; | ||
| const _configuredApiEndpoint = await configuredClusterResource.apiEndpoint.get(); | ||
|
|
||
| // ── Helm release ───────────────────────────────────────────────────────── | ||
| const argocd = configuredCluster.addHelmRelease('argocd', 'argo-cd', { | ||
| repo: 'https://argoproj.github.io/argo-helm', | ||
| version: '7.8.0', | ||
| namespace: 'argocd', | ||
| }) | ||
| .withHelmValue('server.insecure', 'true') | ||
| .withHelmValuesFile('./deploy/argocd-values.yaml'); | ||
|
|
||
| const argocdResource = await argocd; | ||
| const _argocdParent = await argocdResource.parent.get(); | ||
| const _argocdReleaseName = await argocdResource.releaseName.get(); | ||
| const _argocdNamespace = await argocdResource.namespace.get(); | ||
|
|
||
| // ── K8s manifest / Kustomize overlay ───────────────────────────────────── | ||
| const widgetCrd = configuredCluster.addK8sManifest('widget-crd', './k8s/crds/'); | ||
|
|
||
| const widgetCrdResource = await widgetCrd; | ||
| const _crdParent = await widgetCrdResource.parent.get(); | ||
| const _crdPath = await widgetCrdResource.path.get(); | ||
|
|
||
| // ── Service endpoint ───────────────────────────────────────────────────── | ||
| const ui = configuredCluster.addServiceEndpoint('argocd-ui', 'argocd-server', 443, { | ||
| namespace: 'argocd', | ||
| }); | ||
|
|
||
| const uiResource = await ui; | ||
| const _uiParent = await uiResource.parent.get(); | ||
| const _uiServiceName = await uiResource.serviceName.get(); | ||
| const _uiServicePort = await uiResource.servicePort.get(); | ||
| const _uiNamespace = await uiResource.namespace.get(); | ||
| const _uiHostPort = await uiResource.hostPort.get(); | ||
|
|
||
| // ── WithReference — cluster kubeconfig injection ────────────────────────── | ||
| // Project: receives KUBECONFIG=.../.k3s/k8s/local/kubeconfig.yaml | ||
| const _projectRef = builder | ||
| .addProject('operator', { projectPath: '../WidgetOperator/WidgetOperator.csproj' }) | ||
| .withReference(configuredCluster); | ||
|
|
||
| // Container: receives a bind-mounted kubeconfig and KUBECONFIG=/var/k3s/kubeconfig.yaml | ||
| const _containerRef = builder | ||
| .addContainer('sidecar', 'myorg/sidecar') | ||
| .withReference(configuredCluster); | ||
|
|
||
| // ── WithReference — service endpoint URL injection ──────────────────────── | ||
| // Host: receives services__argocd-ui__url=https://localhost:{port} | ||
| const _endpointRef = builder | ||
| .addProject('api', { projectPath: '../WidgetApi/WidgetApi.csproj' }) | ||
| .withReference(ui); | ||
| } | ||
|
|
||
| await builder.build().run(); |
26 changes: 26 additions & 0 deletions
26
...yglot/TypeScript/CommunityToolkit.Aspire.Hosting.K3s/ValidationAppHost/aspire.config.json
This file contains hidden or 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 @@ | ||
| { | ||
| "appHost": { | ||
| "path": "apphost.ts", | ||
| "language": "typescript/nodejs" | ||
| }, | ||
| "profiles": { | ||
| "https": { | ||
| "applicationUrl": "https://localhost:17149;http://localhost:15243", | ||
| "environmentVariables": { | ||
| "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21065", | ||
| "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22170" | ||
| } | ||
| }, | ||
| "http": { | ||
| "applicationUrl": "http://localhost:15243", | ||
| "environmentVariables": { | ||
| "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19027", | ||
| "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20141", | ||
| "ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true" | ||
| } | ||
| } | ||
| }, | ||
| "packages": { | ||
| "CommunityToolkit.Aspire.Hosting.K3s": "" | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
...nd/polyglot/TypeScript/CommunityToolkit.Aspire.Hosting.K3s/ValidationAppHost/package.json
This file contains hidden or 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,19 @@ | ||
| { | ||
| "name": "validationapphost", | ||
| "version": "1.0.0", | ||
| "type": "module", | ||
| "scripts": { | ||
| "start": "aspire run", | ||
| "build": "tsc", | ||
| "dev": "tsc --watch" | ||
| }, | ||
| "dependencies": { | ||
| "vscode-jsonrpc": "^8.2.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^20.0.0", | ||
| "nodemon": "^3.1.11", | ||
| "tsx": "^4.19.0", | ||
| "typescript": "^5.3.0" | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
...d/polyglot/TypeScript/CommunityToolkit.Aspire.Hosting.K3s/ValidationAppHost/tsconfig.json
This file contains hidden or 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,20 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2022", | ||
| "module": "NodeNext", | ||
| "moduleResolution": "NodeNext", | ||
| "esModuleInterop": true, | ||
| "forceConsistentCasingInFileNames": true, | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| "outDir": "./dist", | ||
| "rootDir": "." | ||
| }, | ||
| "include": [ | ||
| "apphost.ts", | ||
| ".modules/**/*.ts" | ||
| ], | ||
| "exclude": [ | ||
| "node_modules" | ||
| ] | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/CommunityToolkit.Aspire.Hosting.K3s/CommunityToolkit.Aspire.Hosting.K3s.csproj
This file contains hidden or 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,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <AdditionalPackageTags>kubernetes k3s hosting cluster</AdditionalPackageTags> | ||
| <Description>An Aspire hosting integration for k3s. Provides AddK3sCluster, AddHelmRelease (via alpine/helm), AddK8sManifest with Kustomize support (via rancher/kubectl), and AddServiceEndpoint for in-process WebSocket port-forwarding. No host-side kubectl or helm required.</Description> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.Hosting" /> | ||
| <PackageReference Include="KubernetesClient" /> | ||
| <PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <InternalsVisibleTo Include="CommunityToolkit.Aspire.Hosting.K3s.Tests" /> | ||
| </ItemGroup> | ||
|
|
||
|
|
||
| </Project> |
8 changes: 8 additions & 0 deletions
8
src/CommunityToolkit.Aspire.Hosting.K3s/HelmContainerImageTags.cs
This file contains hidden or 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,8 @@ | ||
| namespace CommunityToolkit.Aspire.Hosting; | ||
|
|
||
| internal static class HelmContainerImageTags | ||
| { | ||
| internal const string Registry = "docker.io"; | ||
| internal const string Image = "alpine/helm"; | ||
| internal const string Tag = "3.17.3"; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.