Skip to content

Commit c445dc1

Browse files
committed
docs: add better readme documentation for packages
1 parent 5f6ffe6 commit c445dc1

File tree

8 files changed

+395
-10
lines changed

8 files changed

+395
-10
lines changed

src/KubeOps.Abstractions/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
all abstractions, interfaces and such for KubeOps
1+
# KubeOps Abstractions
2+
3+
This package contains the base abstractions for the KubeOps project.
4+
All interfaces and base classes are defined here.
5+
6+
Further, convenience classes and attributes are defined in this package.

src/KubeOps.Cli/README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,54 @@
1-
this project is the CLI of kubeops.
2-
used for all generation commands (generate crds and such)
1+
# KubeOps CLI
2+
3+
.NET tool to help managing your KubeOps project.
4+
It allows you to generate the needed files for your operator
5+
as well as managing resources in your Kubernetes cluster.
6+
7+
## Installation
8+
9+
To install the CLI into your solution / project, first create a new tool manifest:
10+
```bash
11+
dotnet new tool-manifest
12+
```
13+
14+
Then install the CLI:
15+
```bash
16+
dotnet tool install KubeOps.Cli
17+
```
18+
19+
This allows you to use the CLI with `dotnet kubeops`.
20+
21+
## Available Commands
22+
23+
Here is a brief overview over the available commands
24+
(for all options and descriptions, use `-h` or `--help`):
25+
26+
- `version`: prints the version information for the actual connected Kubernetes cluster
27+
- `install`: install the CRDs for the given solution into the cluster
28+
- `uninstall`: uninstall the CRDs for the given solution from the cluster
29+
- `generator`: entry command for generator commands (i.e. has subcommands), all commands
30+
output their result to the stdout or the given output path
31+
- `cert`: generate a CA & server certificate for usage with webhooks
32+
- `crd`: generate the CRDs
33+
- `docker`: generate the dockerfile
34+
- `installer`: generate the installer files (i.e. kustomization yaml) for the operator
35+
- `operator`: generate the deployment for the operator
36+
- `rbac`: generate the needed rbac roles / role bindings for the operator
37+
- `webhook`: entry command for webhook related operations
38+
- `install`: generate the server certificate and install the service / webhook registration
39+
- `register`: register the currently implemented webhooks to the currently selected cluster
40+
41+
### Example
42+
43+
When running `dotnet kubeops api-version`, your output may look like this:
44+
45+
```bash
46+
> dotnet kubeops api-version
47+
Kubernetes API Version
48+
┌─────────────┬─────────────┐
49+
│ Git-Version │ v1.27.2 │
50+
│ Major │ 1 │
51+
│ Minor │ 27 │
52+
│ Platform │ linux/arm64 │
53+
└─────────────┴─────────────┘
54+
```

src/KubeOps.Generator/README.md

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,116 @@
1-
generator
2-
TODO: create generator for init logic of entities
1+
# KubeOps Generator
2+
3+
This is a C# source generator for KubeOps and operators.
4+
It is used to generate convenience functions to help registering
5+
resources within an operator.
6+
7+
## Usage
8+
9+
The generator is automatically used when the `KubeOps.Generator` package is referenced.
10+
11+
```bash
12+
dotnet add package KubeOps.Generator
13+
```
14+
15+
which results in the following `csproj` reference:
16+
17+
```xml
18+
<ItemGroup>
19+
<PackageReference Include="KubeOps.Generator" Version="..." />
20+
</ItemGroup>
21+
```
22+
23+
## Generated Sources
24+
25+
The generator will automatically generate functions for the `IOperatorBuilder`.
26+
27+
### Entity Metadata / Entity Definitions
28+
29+
The generator creates a file in the root namespace called `EntityDefinitions.g.cs`.
30+
This file contains all entities that are annotated with the `KubernetesEntityAttribute`.
31+
The static class contains the `EntityMetadata` for the entities as well
32+
as a function to register all entities within the `IOperatorBuilder`.
33+
34+
#### Example
35+
36+
```csharp
37+
using KubeOps.Abstractions.Builder;
38+
using KubeOps.Abstractions.Entities;
39+
40+
public static class EntityDefinitions
41+
{
42+
public static readonly EntityMetadata V1TestEntity = new("TestEntity", "v1", "testing.dev", null);
43+
public static IOperatorBuilder RegisterEntities(this IOperatorBuilder builder)
44+
{
45+
builder.AddEntity<global::Operator.Entities.V1TestEntity>(V1TestEntity);
46+
return builder;
47+
}
48+
}
49+
```
50+
51+
### Controller Registrations
52+
53+
The generator creates a file in the root namespace called `ControllerRegistrations.g.cs`.
54+
This file contains a function to register all found controllers
55+
(i.e. classes that implement the `IEntityController<T>` interface).
56+
57+
#### Example
58+
59+
```csharp
60+
using KubeOps.Abstractions.Builder;
61+
62+
public static class ControllerRegistrations
63+
{
64+
public static IOperatorBuilder RegisterControllers(this IOperatorBuilder builder)
65+
{
66+
builder.AddController<global::Operator.Controller.V1TestEntityController, global::Operator.Entities.V1TestEntity>();
67+
return builder;
68+
}
69+
}
70+
```
71+
72+
### Finalizer Registrations
73+
74+
The generator creates a file in the root namespace called `FinalizerRegistrations.g.cs`.
75+
This file contains all finalizers with generated finalizer-identifiers.
76+
Further, a function to register all finalizers is generated.
77+
78+
#### Example
79+
80+
```csharp
81+
using KubeOps.Abstractions.Builder;
82+
83+
public static class FinalizerRegistrations
84+
{
85+
public const string FinalizerOneIdentifier = "testing.dev/finalizeronefinalizer";
86+
public const string FinalizerTwoIdentifier = "testing.dev/finalizertwofinalizer";
87+
public static IOperatorBuilder RegisterFinalizers(this IOperatorBuilder builder)
88+
{
89+
builder.AddFinalizer<global::Operator.Finalizer.FinalizerOne, global::Operator.Entities.V1TestEntity>(FinalizerOneIdentifier);
90+
builder.AddFinalizer<global::Operator.Finalizer.FinalizerTwo, global::Operator.Entities.V1TestEntity>(FinalizerTwoIdentifier);
91+
return builder;
92+
}
93+
}
94+
```
95+
96+
### General Operator Extensions
97+
98+
The generator creates a file in the root namespace called `OperatorExtensions.g.cs`.
99+
It contains convenience functions to register all generated sources.
100+
101+
#### Example
102+
103+
```csharp
104+
using KubeOps.Abstractions.Builder;
105+
106+
public static class OperatorBuilderExtensions
107+
{
108+
public static IOperatorBuilder RegisterComponents(this IOperatorBuilder builder)
109+
{
110+
builder.RegisterEntities();
111+
builder.RegisterControllers();
112+
builder.RegisterFinalizers();
113+
return builder;
114+
}
115+
}
116+
```

src/KubeOps.KubernetesClient/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ with group and kind information as well.
1212
An example of the client that lists all namespaces in the cluster:
1313

1414
```csharp
15+
// Create the entity metadata
16+
var meta = Entities.ToEntityMetadata(typeof(V1Namespace));
17+
1518
// Creates the client with the default config.
16-
var client = new KubernetesClient();
19+
var client = new KubernetesClient<V1Namespace>(meta);
1720

1821
// Get all namespaces in the cluster.
19-
var namespaces = await client.List<V1Namespace>();
22+
var namespaces = await client.List();
2023
```

src/KubeOps.Operator.Web/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
operator web
1+
# KubeOps Operator Web
2+
3+
The KubeOps Operator Web package provides a webserver to enable
4+
webhooks for your Kubernetes operator.
5+
6+
TODO.

src/KubeOps.Operator/README.md

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,181 @@
1-
operator
1+
# KubeOps Operator
2+
3+
The `KubeOps.Operator` package provides a framework
4+
for building Kubernetes operators in .NET.
5+
It is built on top of the Kubernetes client libraries for .NET
6+
and provides a set of abstractions and utilities for implementing
7+
operators that manage custom resources in a Kubernetes cluster.
8+
9+
## Getting Started
10+
11+
To get started with the SDK, you can install it from NuGet:
12+
13+
```bash
14+
dotnet add package KubeOps.Operator
15+
```
16+
17+
Once you have installed the package, you can create entities,
18+
controllers, finalizers, and more to implement your operator.
19+
20+
All resources must be added to the operator builder
21+
in order to be recognized by the SDK and to be used as
22+
operator resources. The [KubeOps.Generator](../KubeOps.Generator/README.md)
23+
helps with convenience methods to register everything
24+
at once.
25+
26+
You'll need to use the Generic Host to run your operator.
27+
However, for a plain operator without webhooks, no ASP.net
28+
is required (in contrast to v7).
29+
30+
```csharp
31+
using KubeOps.Operator.Extensions;
32+
33+
using Microsoft.Extensions.Hosting;
34+
using Microsoft.Extensions.Logging;
35+
36+
var builder = Host.CreateApplicationBuilder(args);
37+
38+
builder.Logging.SetMinimumLevel(LogLevel.Trace);
39+
40+
builder.Services
41+
.AddKubernetesOperator()
42+
.RegisterComponents();
43+
44+
using var host = builder.Build();
45+
await host.RunAsync();
46+
```
47+
48+
### Registering Resources
49+
50+
When using the [KubeOps.Generator](../KubeOps.Generator/README.md),
51+
you can use the `RegisterResources` function:
52+
53+
```csharp
54+
builder.Services
55+
.AddKubernetesOperator()
56+
.RegisterComponents();
57+
```
58+
59+
Otherwise, you can register resources manually:
60+
61+
```csharp
62+
builder.Services
63+
.AddKubernetesOperator()
64+
.AddControllerWithEntity<TestController, V1TestEntity>(meta)
65+
.AddFinalizer<FirstFinalizer, V1TestEntity>("first")
66+
.AddFinalizer<SecondFinalizer, V1TestEntity>("second")
67+
```
68+
69+
### Entity
70+
71+
To create an entity, you need to implement the
72+
`IKubernetesObject<V1ObjectMeta>` interface. There are convenience
73+
classes available to help with initialization, status and spec
74+
properties.
75+
76+
```csharp
77+
[KubernetesEntity(Group = "testing.dev", ApiVersion = "v1", Kind = "TestEntity")]
78+
public class V1TestEntity :
79+
CustomKubernetesEntity<V1TestEntity.EntitySpec, V1TestEntity.EntityStatus>
80+
{
81+
public override string ToString()
82+
=> $"Test Entity ({Metadata.Name}): {Spec.Username} ({Spec.Email})";
83+
84+
public class EntitySpec
85+
{
86+
public string Username { get; set; } = string.Empty;
87+
88+
public string Email { get; set; } = string.Empty;
89+
}
90+
91+
public class EntityStatus
92+
{
93+
public string Status { get; set; } = string.Empty;
94+
}
95+
}
96+
```
97+
98+
### Controller
99+
100+
A controller is the element that reconciles a specific entity.
101+
You can reconcile your own custom entities or all other entities
102+
as long as they are registered within the SDK. For a guide
103+
on how to reconcile external entities, refer to the
104+
[documentation](https://buehler.github.io/dotnet-operator-sdk/).
105+
106+
A simple controller could look like this:
107+
108+
```csharp
109+
[EntityRbac(typeof(V1TestEntity), Verbs = RbacVerb.All)]
110+
public class V1TestEntityController : IEntityController<V1TestEntity>
111+
{
112+
private readonly IKubernetesClient<V1TestEntity> _client;
113+
private readonly EntityFinalizerAttacher<FinalizerOne, V1TestEntity> _finalizer1;
114+
115+
public V1TestEntityController(
116+
IKubernetesClient<V1TestEntity> client,
117+
EntityFinalizerAttacher<FinalizerOne, V1TestEntity> finalizer1)
118+
{
119+
_client = client;
120+
_finalizer1 = finalizer1;
121+
}
122+
123+
public async Task ReconcileAsync(V1TestEntity entity)
124+
{
125+
_logger.LogInformation("Reconciling entity {Entity}.", entity);
126+
127+
entity = await _finalizer1(entity);
128+
129+
entity.Status.Status = "Reconciling";
130+
entity = await _client.UpdateStatus(entity);
131+
entity.Status.Status = "Reconciled";
132+
await _client.UpdateStatus(entity);
133+
}
134+
}
135+
```
136+
137+
This controller attaches a specific finalizer to the entity,
138+
updates its status and then saves the entity.
139+
140+
> [!CAUTION]
141+
> It is important to always use the returned values
142+
> of an entity when using modifying actions of the
143+
> Kubernetes client. Otherwise, you will receive
144+
> "HTTP CONFLICT" errors because of the resource version
145+
> field in the entity.
146+
147+
> [!NOTE]
148+
> Do not update the entity itself in the reconcile loop.
149+
> It is considered bad practice to update entities
150+
> while reconciling them. However, the status may be updated.
151+
> To update entities before they are reconciled
152+
> (e.g. to ban certain values or change values),
153+
> use webhooks instead.
154+
155+
### Finalizer
156+
157+
A [finalizer](https://kubernetes.io/docs/concepts/overview/working-with-objects/finalizers/)
158+
is an element for asynchronous cleanup in Kubernetes.
159+
160+
It is attached with an `EntityFinalizerAttacher` and is called
161+
when the entity is marked as deleted.
162+
163+
```csharp
164+
public class FinalizerOne : IEntityFinalizer<V1TestEntity>
165+
{
166+
public Task FinalizeAsync(V1TestEntity entity)
167+
{
168+
return Task.CompletedTask;
169+
}
170+
}
171+
```
172+
173+
> [!NOTE]
174+
> The controller (if you overwrote the `DeletedAsync` method)
175+
> will receive the notification as soon as all finalizers
176+
> are removed.
177+
178+
## Documentation
179+
180+
For more information, please visit the
181+
[documentation](https://buehler.github.io/dotnet-operator-sdk/).

0 commit comments

Comments
 (0)