|
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