Skip to content

Commit 57422ad

Browse files
authored
feat: (re)add KubeObs.Templates for easy creating operators (#894)
1 parent d9caf6e commit 57422ad

File tree

37 files changed

+729
-26
lines changed

37 files changed

+729
-26
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ _site
3838
.DS_Store
3939

4040
#Rider
41-
.idea
41+
.idea
42+
.fake

KubeOps.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubeOps.Transpiler.Test", "
5757
EndProject
5858
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebhookOperator", "examples\WebhookOperator\WebhookOperator.csproj", "{0BFE2297-9537-49BE-8B1F-431A8ACD654D}"
5959
EndProject
60+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubeOps.Templates", "src\KubeOps.Templates\KubeOps.Templates.csproj", "{26237038-7172-4D01-B5E1-2A5E3F6B369E}"
61+
EndProject
6062
Global
6163
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6264
Debug|Any CPU = Debug|Any CPU
@@ -82,6 +84,7 @@ Global
8284
{7229B71C-C5A5-41E0-85DB-A331A945703C} = {4DB01062-6DC5-4028-BB72-C0619C2F5F2E}
8385
{7F7744B2-CF3F-4309-9C2D-037278017D49} = {C587731F-8191-4A19-8662-B89A60FE79A1}
8486
{0BFE2297-9537-49BE-8B1F-431A8ACD654D} = {DC760E69-D0EA-417F-AE38-B12D0B04DE39}
87+
{26237038-7172-4D01-B5E1-2A5E3F6B369E} = {4DB01062-6DC5-4028-BB72-C0619C2F5F2E}
8588
EndGlobalSection
8689
GlobalSection(ProjectConfigurationPlatforms) = postSolution
8790
{E9A0B04E-D90E-4B94-90E0-DD3666B098FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
@@ -148,5 +151,9 @@ Global
148151
{0BFE2297-9537-49BE-8B1F-431A8ACD654D}.Debug|Any CPU.Build.0 = Debug|Any CPU
149152
{0BFE2297-9537-49BE-8B1F-431A8ACD654D}.Release|Any CPU.ActiveCfg = Release|Any CPU
150153
{0BFE2297-9537-49BE-8B1F-431A8ACD654D}.Release|Any CPU.Build.0 = Release|Any CPU
154+
{26237038-7172-4D01-B5E1-2A5E3F6B369E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
155+
{26237038-7172-4D01-B5E1-2A5E3F6B369E}.Debug|Any CPU.Build.0 = Debug|Any CPU
156+
{26237038-7172-4D01-B5E1-2A5E3F6B369E}.Release|Any CPU.ActiveCfg = Release|Any CPU
157+
{26237038-7172-4D01-B5E1-2A5E3F6B369E}.Release|Any CPU.Build.0 = Release|Any CPU
151158
EndGlobalSection
152159
EndGlobal

examples/ConversionWebhookOperator/ConversionWebhookOperator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
<PropertyGroup>
44
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
6-
<ImplicitUsings>enable</ImplicitUsings>
76
<EnablePreviewFeatures>true</EnablePreviewFeatures>
7+
<ImplicitUsings>enable</ImplicitUsings>
88
<IsPackable>false</IsPackable>
99
</PropertyGroup>
1010

examples/ConversionWebhookOperator/Program.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1+
using System.Net;
2+
13
using KubeOps.Operator;
24
using KubeOps.Operator.Web.Builder;
3-
4-
#pragma warning disable CS0618 // Type or member is obsolete
5+
using KubeOps.Operator.Web.Certificates;
56

67
var builder = WebApplication.CreateBuilder(args);
7-
builder.Services
8+
var opBuilder = builder.Services
89
.AddKubernetesOperator()
9-
.RegisterComponents()
10+
.RegisterComponents();
11+
1012
#if DEBUG
11-
.AddDevelopmentTunnel(5000)
13+
const string ip = "192.168.1.100";
14+
const ushort port = 443;
15+
using var generator = new CertificateGenerator(ip);
16+
var cert = generator.Server.CopyServerCertWithPrivateKey();
17+
18+
// Configure Kestrel to listen on IPv4, use port 443, and use the server certificate
19+
builder.WebHost.ConfigureKestrel(serverOptions =>
20+
{
21+
serverOptions.Listen(IPAddress.Any, port, async listenOptions =>
22+
{
23+
listenOptions.UseHttps(cert);
24+
});
25+
});
26+
27+
opBuilder.UseCertificateProvider(port, ip, generator);
1228
#endif
13-
;
1429

1530
builder.Services
1631
.AddControllers();

examples/WebhookOperator/Program.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1+
using System.Net;
2+
13
using KubeOps.Operator;
24
using KubeOps.Operator.Web.Builder;
3-
4-
#pragma warning disable CS0618 // Type or member is obsolete
5+
using KubeOps.Operator.Web.Certificates;
56

67
var builder = WebApplication.CreateBuilder(args);
7-
builder.Services
8+
var opBuilder = builder.Services
89
.AddKubernetesOperator()
9-
.RegisterComponents()
10+
.RegisterComponents();
11+
1012
#if DEBUG
11-
.AddDevelopmentTunnel(5000)
13+
const string ip = "192.168.1.100";
14+
const ushort port = 443;
15+
using var generator = new CertificateGenerator(ip);
16+
var cert = generator.Server.CopyServerCertWithPrivateKey();
17+
18+
builder.WebHost.ConfigureKestrel(serverOptions =>
19+
{
20+
serverOptions.Listen(IPAddress.Any, port, async listenOptions =>
21+
{
22+
listenOptions.UseHttps(cert);
23+
});
24+
});
25+
26+
opBuilder.UseCertificateProvider(port, ip, generator);
1227
#endif
13-
;
1428

1529
builder.Services
1630
.AddControllers();

src/KubeOps.Operator.Web/Certificates/CertificateWebhookService.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,17 @@ namespace KubeOps.Operator.Web.Certificates;
1010
internal class CertificateWebhookService(ILogger<CertificateWebhookService> logger, IKubernetesClient client, WebhookLoader loader, WebhookConfig config, ICertificateProvider provider)
1111
: WebhookServiceBase(client, loader, config), IHostedService
1212
{
13-
private readonly ILogger<CertificateWebhookService> _logger = logger;
14-
private readonly ICertificateProvider _provider = provider;
15-
1613
public async Task StartAsync(CancellationToken cancellationToken)
1714
{
18-
CaBundle = _provider.Server.Certificate.EncodeToPemBytes();
15+
CaBundle = provider.Server.Certificate.EncodeToPemBytes();
1916

20-
_logger.LogDebug("Registering webhooks");
17+
logger.LogDebug("Registering webhooks");
2118
await RegisterAll();
2219
}
2320

2421
public Task StopAsync(CancellationToken cancellationToken)
2522
{
26-
_provider.Dispose();
23+
provider.Dispose();
2724
return Task.CompletedTask;
2825
}
2926
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Title>KubeOps Templates</Title>
5+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
6+
7+
<IncludeContentInPack>true</IncludeContentInPack>
8+
<IncludeBuildOutput>false</IncludeBuildOutput>
9+
<ContentTargetFolders>content</ContentTargetFolders>
10+
<NoWarn>$(NoWarn);NU5128</NoWarn>
11+
</PropertyGroup>
12+
13+
<PropertyGroup>
14+
<PackageId>KubeOps.Templates</PackageId>
15+
<PackageTags>dotnet-new templates Kubernetes Operator Sdk KubeOps</PackageTags>
16+
<PackageType>Template</PackageType>
17+
<PackageDescription>dotnet new templates for KubeOps operator sdk.</PackageDescription>
18+
</PropertyGroup>
19+
20+
<ItemGroup>
21+
<Content Include="Templates\**\*"
22+
Exclude="Templates\**\bin\**;Templates\**\obj\**" />
23+
<Compile Remove="**\*" />
24+
</ItemGroup>
25+
26+
</Project>

src/KubeOps.Templates/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# KubeOps Dotnet New Templates
2+
3+
[![Nuget](https://img.shields.io/nuget/v/KubeOps.Templates?label=NuGet&logo=nuget)](https://www.nuget.org/packages/KubeOps.Templates)
4+
5+
To use the operator SDK as easy as possible, this
6+
[Nuget Package](https://www.nuget.org/packages/KubeOps.Templates)
7+
contains `dotnet new` templates.
8+
These templates enable developers to create Kubernetes operators
9+
with the simple dotnet new command in C#.
10+
11+
## Installation
12+
13+
To install the template package, use the `dotnet` cli
14+
(or you may use the exact version as provided in the link above):
15+
16+
```bash
17+
dotnet new --install KubeOps.Templates::*
18+
```
19+
20+
As soon as the templates are installed, you may use them with the short names below:
21+
22+
## Templates
23+
24+
### operator
25+
26+
_Short Name_: `operator`
27+
28+
Creates a standard Kubernetes operator with demo implementations for controllers, entities, and finalizers. This template is a good starting point for most operator projects.
29+
30+
```bash
31+
dotnet new operator -n MyOperator
32+
```
33+
34+
### operator-empty
35+
36+
_Short Name_: `operator-empty`
37+
38+
Creates a minimal, empty Kubernetes operator project without web capabilities. Ideal for advanced users who want to start from scratch.
39+
40+
```bash
41+
dotnet new operator-empty -n MyOperator
42+
```
43+
44+
### operator-web
45+
46+
_Short Name_: `operator-web`
47+
48+
Creates a Kubernetes operator with web server capabilities and demo implementations, including webhooks. Use this template if you need web-based features like admission webhooks.
49+
50+
```bash
51+
dotnet new operator-web -n MyOperator
52+
```
53+
54+
### operator-web-empty
55+
56+
_Short Name_: `operator-web-empty`
57+
58+
Creates a minimal Kubernetes operator project with web server capabilities, but no demo implementations. Use this template if you want web features but a clean slate.
59+
60+
```bash
61+
dotnet new operator-web-empty -n MyOperator
62+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"$schema": "https://json.schemastore.org/template",
3+
"author": "Christoph Bühler",
4+
"classifications": ["Kubernetes", "Operator", "Empty"],
5+
"identity": "KubeOps.Templates.EmptyOperator.CSharp",
6+
"groupIdentity": "KubeOps.Templates.EmptyOperator",
7+
"name": "Kubernetes Empty Operator",
8+
"description": "Create an empty Kubernetes operator with the KubeOps SDK. The default empty operator does not contain any web capabilities.",
9+
"shortName": "operator-empty",
10+
"tags": {
11+
"language": "C#",
12+
"type": "project"
13+
},
14+
"sourceName": "GeneratedOperatorProject",
15+
"defaultName": "Operator",
16+
"preferNameDirectory": true,
17+
"postActions": [
18+
{
19+
"actionId": "B17581D1-C5C9-4489-8F0A-004BE667B814",
20+
"description": "Add KubeOps Operator SDK reference",
21+
"continueOnError": false,
22+
"manualInstructions": [
23+
{
24+
"text": "Add the KubeOps Operator package to your project via nuget"
25+
}
26+
],
27+
"args": {
28+
"referenceType": "package",
29+
"reference": "KubeOps.Operator",
30+
"projectFileExtensions": ".csproj"
31+
}
32+
},
33+
{
34+
"actionId": "B17581D1-C5C9-4489-8F0A-004BE667B814",
35+
"description": "Add KubeOps Generator reference",
36+
"continueOnError": false,
37+
"manualInstructions": [
38+
{
39+
"text": "Add the KubeOps Generator package to your project via nuget"
40+
}
41+
],
42+
"args": {
43+
"referenceType": "package",
44+
"reference": "KubeOps.Generator",
45+
"projectFileExtensions": ".csproj"
46+
}
47+
}
48+
]
49+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<LangVersion>12.0</LangVersion>
7+
<Nullable>enable</Nullable>
8+
<ImplicitUsings>true</ImplicitUsings>
9+
<IsPackable>false</IsPackable>
10+
<GenerateDocumentationFile>false</GenerateDocumentationFile>
11+
</PropertyGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)