KubeOps.Operator.Web 8.0.0-pre.31

This is a prerelease version of KubeOps.Operator.Web.
There is a newer version of this package available.
See the version list below for details.
dotnet add package KubeOps.Operator.Web --version 8.0.0-pre.31
NuGet\Install-Package KubeOps.Operator.Web -Version 8.0.0-pre.31
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="KubeOps.Operator.Web" Version="8.0.0-pre.31" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add KubeOps.Operator.Web --version 8.0.0-pre.31
#r "nuget: KubeOps.Operator.Web, 8.0.0-pre.31"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install KubeOps.Operator.Web as a Cake Addin
#addin nuget:?package=KubeOps.Operator.Web&version=8.0.0-pre.31&prerelease

// Install KubeOps.Operator.Web as a Cake Tool
#tool nuget:?package=KubeOps.Operator.Web&version=8.0.0-pre.31&prerelease

KubeOps Operator Web

The KubeOps Operator Web package provides a webserver to enable webhooks for your Kubernetes operator.

Usage

To enable webhooks and external access to your operator, you need to use ASP.net. The project file needs to reference Microsoft.NET.Sdk.Web instead of Microsoft.NET.Sdk and the Program.cs needs to be changed.

To allow webhooks, the MVC controllers need to be registered and mapped.

The basic Program.cs setup looks like this:

using KubeOps.Operator;

var builder = WebApplication.CreateBuilder(args);
builder.Services
    .AddKubernetesOperator()
    .RegisterComponents();

builder.Services
    .AddControllers();

var app = builder.Build();

app.UseRouting();
app.MapControllers();

await app.RunAsync();

Note the .AddControllers and .MapControllers call. Without them, your webhooks will not be reachable.

Validation Hooks

To create a validation webhook, first create a new class that implements the ValidationWebhook<T> base class. Then decorate the webhook with the ValidationWebhookAttribute to set the route correctly.

After that setup, you may overwrite any of the following methods:

  • Create
  • CreateAsync
  • Update
  • UpdateAsync
  • Delete
  • DeleteAsync

The async methods take precedence over the sync methods.

An example of such a validation webhook looks like:

[ValidationWebhook(typeof(V1TestEntity))]
public class TestValidationWebhook : ValidationWebhook<V1TestEntity>
{
    public override ValidationResult Create(V1TestEntity entity, bool dryRun)
    {
        if (entity.Spec.Username == "forbidden")
        {
            return Fail("name may not be 'forbidden'.", 422);
        }

        return Success();
    }

    public override ValidationResult Update(V1TestEntity oldEntity, V1TestEntity newEntity, bool dryRun)
    {
        if (newEntity.Spec.Username == "forbidden")
        {
            return Fail("name may not be 'forbidden'.");
        }

        return Success();
    }
}

To create the validation results, use the protected methods (Success and Fail) like "normal" IActionResult creation methods.

Mutation Hooks

To create a mutation webhook, first create a new class that implements the MutationWebhook<T> base class. Then decorate the webhook with the MutationWebhookAttribute to set the route correctly.

After that setup, you may overwrite any of the following methods:

  • Create
  • CreateAsync
  • Update
  • UpdateAsync
  • Delete
  • DeleteAsync

The async methods take precedence over the sync methods.

An example of such a mutation webhook looks like:

[MutationWebhook(typeof(V1TestEntity))]
public class TestMutationWebhook : MutationWebhook<V1TestEntity>
{
    public override MutationResult<V1TestEntity> Create(V1TestEntity entity, bool dryRun)
    {
        if (entity.Spec.Username == "overwrite")
        {
            entity.Spec.Username = "random overwritten";
            return Modified(entity);
        }

        return NoChanges();
    }
}

To create the mutation results, use the protected methods (NoChanges, Modified, and Fail) like "normal" IActionResult creation methods.

Conversion Hooks

TODO.

Installing In The Cluster

When creating an operator with webhooks, certain special resources must be provided to run in the cluster. When this package is referenced and KubeOps.Cli is installed, these resources should be generated automatically. Basically, instead of generating a dockerfile with dotnet:runtime as final image, you'll need dotnet:aspnet and the operator needs a service and the certificates for the HTTPS connection since webhooks only operate over HTTPS.

With the KubeOps.Cli package you can generate the required resources or let the customized Build targets do it for you.

The targets create a CA certificate and a server certificate (with respective keys), a service, and the webhook registrations required for you.

[!WARNING] The generated certificate has a validity of 5 years. After that time, the certificate needs to be renewed. For now, there is no automatic renewal process.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.0 864 3/13/2024
9.0.0-pre.4 47 4/19/2024
9.0.0-pre.3 44 3/21/2024
9.0.0-pre.2 44 3/13/2024
9.0.0-pre.1 63 3/7/2024
8.0.2-pre.2 53 2/21/2024
8.0.2-pre.1 40 2/19/2024
8.0.1 2,396 2/13/2024
8.0.1-pre.7 54 2/12/2024
8.0.1-pre.6 53 2/7/2024
8.0.1-pre.5 60 2/5/2024
8.0.1-pre.4 50 1/31/2024
8.0.1-pre.3 51 1/26/2024
8.0.1-pre.2 46 1/25/2024
8.0.1-pre.1 52 1/18/2024
8.0.0 349 1/17/2024
8.0.0-pre.45 47 1/17/2024
8.0.0-pre.44 57 1/16/2024
8.0.0-pre.43 49 1/16/2024
8.0.0-pre.42 185 1/10/2024
8.0.0-pre.41 132 1/2/2024
8.0.0-pre.40 110 12/27/2023
8.0.0-pre.39 60 12/21/2023
8.0.0-pre.38 138 12/6/2023
8.0.0-pre.37 53 12/6/2023
8.0.0-pre.36 53 12/3/2023
8.0.0-pre.35 60 11/28/2023
8.0.0-pre.34 78 11/24/2023
8.0.0-pre.33 52 11/24/2023
8.0.0-pre.32 48 11/23/2023
8.0.0-pre.31 56 11/23/2023
8.0.0-pre.30 54 11/23/2023
8.0.0-pre.29 134 11/11/2023
8.0.0-pre.28 56 11/8/2023
8.0.0-pre.27 149 10/23/2023
8.0.0-pre.26 60 10/19/2023
8.0.0-pre.25 55 10/18/2023
8.0.0-pre.24 71 10/13/2023
8.0.0-pre.23 61 10/13/2023
8.0.0-pre.22 62 10/13/2023
8.0.0-pre.21 57 10/12/2023
8.0.0-pre.20 63 10/11/2023
8.0.0-pre.19 58 10/9/2023
8.0.0-pre.18 55 10/9/2023
8.0.0-pre.17 57 10/7/2023
8.0.0-pre.16 56 10/6/2023
8.0.0-pre.15 56 10/6/2023
8.0.0-pre.14 56 10/5/2023
8.0.0-pre.13 50 10/5/2023
8.0.0-pre.12 53 10/4/2023
8.0.0-pre.11 55 10/3/2023
8.0.0-pre.10 57 10/3/2023
8.0.0-pre.9 58 10/3/2023
8.0.0-pre.8 54 10/2/2023
8.0.0-pre.7 59 10/2/2023
8.0.0-pre.6 60 9/29/2023
8.0.0-pre.5 54 9/28/2023
8.0.0-pre.4 52 9/28/2023
8.0.0-pre.3 56 9/27/2023
8.0.0-pre.2 40 9/26/2023
8.0.0-pre.1 53 9/22/2023