KubeOps.KubernetesClient 9.5.0

dotnet add package KubeOps.KubernetesClient --version 9.5.0
                    
NuGet\Install-Package KubeOps.KubernetesClient -Version 9.5.0
                    
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.KubernetesClient" Version="9.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KubeOps.KubernetesClient" Version="9.5.0" />
                    
Directory.Packages.props
<PackageReference Include="KubeOps.KubernetesClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add KubeOps.KubernetesClient --version 9.5.0
                    
#r "nuget: KubeOps.KubernetesClient, 9.5.0"
                    
#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.
#addin nuget:?package=KubeOps.KubernetesClient&version=9.5.0
                    
Install KubeOps.KubernetesClient as a Cake Addin
#tool nuget:?package=KubeOps.KubernetesClient&version=9.5.0
                    
Install KubeOps.KubernetesClient as a Cake Tool

KubeOps Kubernetes Client

Nuget

This package provides an enhanced, developer-friendly interface for interacting with the Kubernetes API, built on top of the official kubernetes-client/csharp library. While the official client is powerful, it often requires verbose calls, especially for Custom Resources.

The KubeOps.KubernetesClient aims to simplify common operator tasks by offering:

  • True Generic Methods: Perform operations like Get, List, Create, Update, Delete, and Watch on any Kubernetes resource type (including your custom resources defined with [KubernetesEntity]) using strongly-typed generic methods, without manually providing API group, version, and plural name.
  • Simplified API: Reduces the boilerplate needed for common CRUD operations.
  • Type Safety: Leverages C# generics for better compile-time checking.

This is an "enhanced" version of the original Google Kubernetes Client. It extends the original client with some additional features, like true generics and method variants. The original GenericClient does support "generics", but only in a limited way. The client needs to be initialized with group and kind information as well.

It acts as a wrapper, handling the complexities of determining the correct API endpoint and resource mapping based on the provided C# type.

Usage

When using the main KubeOps.Operator package, an instance of IKubernetesClient is automatically registered in the .NET Dependency Injection container. You can simply inject it into your controllers, finalizers, or webhooks:

using KubeOps.KubernetesClient;
using KubeOps.Operator.Controller;
using MyOperator.Entities; // Your custom entity
using k8s.Models; // For built-in types like V1Pod

public class MyResourceController : IResourceController<V1MyResource>
{
    private readonly IKubernetesClient _client;

    public MyResourceController(IKubernetesClient client)
    {
        _client = client;
    }

    public async Task<ResourceControllerResult?> ReconcileAsync(V1MyResource entity)
    {
        // Use the client to interact with the cluster
        var pod = await _client.GetAsync<V1Pod>("my-pod", entity.Namespace());
        if (pod == null)
        {
            var newPod = new V1Pod { /* ... */ };
            await _client.CreateAsync(newPod);
        }

        // Get a custom resource
        var otherResource = await _client.GetAsync<V1OtherResource>("other-resource-name", entity.Namespace());

        return null; // Requeue later
    }

    // ... other methods
}

Standalone Usage

If you need to use the client outside the main KubeOps operator framework (e.g., in a command-line tool or script), you can instantiate it directly. It will automatically attempt to load configuration based on standard Kubernetes conventions (Kubeconfig file or in-cluster service account).

using KubeOps.KubernetesClient;
using k8s.Models;

// Instantiate the client
IKubernetesClient client = new KubernetesClient();

// List all namespaces
var namespaces = await client.ListAsync<V1Namespace>();
foreach (var ns in namespaces)
{
    Console.WriteLine($"Namespace: {ns.Name()}");
}

// Get a specific ConfigMap
var configMap = await client.GetAsync<V1ConfigMap>("my-config", "default");
if (configMap != null)
{
    Console.WriteLine($"ConfigMap Data: {string.Join(',', configMap.Data)}");
}

For advanced configuration (e.g., custom Kubeconfig paths, timeouts), refer to the underlying k8s.KubernetesClientConfiguration documentation from the official client library.

Examples

An example of the client that lists all namespaces in the cluster:

var client = new KubernetesClient() as IKubernetesClient;

// Get all namespaces in the cluster.
var namespaces = await client.ListAsync<V1Namespace>();

Get a specific resource:

// Get a Pod in the 'production' namespace
var pod = await client.GetAsync<V1Pod>("my-app-pod-xyz", "production");

// Get a custom resource (assuming V1MyCrd is defined)
var myCrd = await client.GetAsync<V1MyCrd>("my-instance", "default");

List resources (with optional namespace):

// List all pods in the 'staging' namespace
var podsInStaging = await client.ListAsync<V1Pod>("staging");

// List all custom resources of type V1MyCrd across all namespaces (for cluster-scoped or if allowed by RBAC)
var allMyCrds = await client.ListAsync<V1MyCrd>(null);

Create a resource:

var newConfigMap = new V1ConfigMap
{
    Metadata = new V1ObjectMeta { Name = "new-map", NamespaceProperty = "default" },
    Data = new Dictionary<string, string> { { "key", "value" } }
};
var createdMap = await client.CreateAsync(newConfigMap);

Update a resource:

var existingPod = await client.GetAsync<V1Pod>("my-pod", "default");
if (existingPod != null)
{
    // Ensure Annotations dictionary exists before adding to it
    existingPod.Metadata.Annotations ??= new Dictionary<string, string>();
    existingPod.Metadata.Annotations["my-annotation"] = "updated-value";
    var updatedPod = await client.UpdateAsync(existingPod);
}

Update Resource Status:

// Assuming myCrd has a Status property
var existingCrd = await client.GetAsync<V1MyCrd>("my-instance", "default");
if (existingCrd != null)
{
    existingCrd.Status.Message = "Processing completed";
    var updatedCrd = await client.UpdateStatusAsync(existingCrd);
}

Watch Resources:

// Watch for Pod events in the 'default' namespace
await foreach (var (type, pod) in client.WatchAsync<V1Pod>(namespaceParameter: "default"))
{
    Console.WriteLine($"Event: {type}, Pod: {pod.Name()}");
    // Handle Added, Modified, Deleted events
}

Delete a resource:

await client.DeleteAsync<V1Pod>("pod-to-delete", "default");

var crdToDelete = await client.GetAsync<V1MyCrd>("crd-instance-to-delete", "dev");
if (crdToDelete != null)
{
    await client.DeleteAsync(crdToDelete);
}

Documentation

Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on KubeOps.KubernetesClient:

Package Downloads
KubeOps

This is an operator sdk written in c#. It enables a developer to create a custom controller for CRDs (CustomResourceDefinitions) that runs on kubernetes.

KubeOps.Operator

This is an operator sdk written in c#. It enables a developer to create a custom controller for CRDs (CustomResourceDefinitions) that runs on kubernetes. This operator may run without ASP.net but needs the IHost of dotnet to run.

KubernetesClient.Extensions.Configuration

Package Description

Archetypical.Software.K8s.Utilities

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.5.0 729 5/8/2025
9.4.1 1,111 4/29/2025
9.4.0 291 4/28/2025
9.3.0 7,467 3/26/2025
9.2.0 11,312 1/24/2025
9.1.5 44,953 9/10/2024
9.1.4 1,555 8/26/2024
9.1.3 20,974 6/28/2024
9.1.2 11,328 6/20/2024
9.1.1 5,785 5/22/2024
9.1.0 2,687 5/15/2024
9.0.2 272 5/13/2024
9.0.0 29,126 3/13/2024
9.0.0-pre.4 83 4/19/2024
9.0.0-pre.3 77 3/21/2024
9.0.0-pre.2 66 3/13/2024
9.0.0-pre.1 91 3/7/2024
8.0.2-pre.2 89 2/21/2024
8.0.2-pre.1 86 2/19/2024
8.0.1 11,770 2/13/2024
8.0.1-pre.7 81 2/12/2024
8.0.1-pre.6 84 2/7/2024
8.0.1-pre.5 69 2/5/2024
8.0.1-pre.4 82 1/31/2024
8.0.1-pre.3 80 1/26/2024
8.0.1-pre.2 70 1/25/2024
8.0.1-pre.1 79 1/18/2024
8.0.0 1,095 1/17/2024
8.0.0-pre.45 75 1/17/2024
8.0.0-pre.44 82 1/16/2024
8.0.0-pre.43 71 1/16/2024
8.0.0-pre.42 985 1/10/2024
8.0.0-pre.41 308 1/2/2024
8.0.0-pre.40 183 12/27/2023
8.0.0-pre.39 89 12/21/2023
8.0.0-pre.38 433 12/6/2023
8.0.0-pre.37 129 12/6/2023
8.0.0-pre.36 163 12/3/2023
8.0.0-pre.35 109 11/28/2023
8.0.0-pre.34 114 11/24/2023
8.0.0-pre.33 84 11/24/2023
8.0.0-pre.32 72 11/23/2023
8.0.0-pre.31 78 11/23/2023
8.0.0-pre.30 94 11/23/2023
8.0.0-pre.29 962 11/11/2023
8.0.0-pre.28 105 11/8/2023
8.0.0-pre.27 586 10/23/2023
8.0.0-pre.26 118 10/19/2023
8.0.0-pre.25 81 10/18/2023
8.0.0-pre.24 102 10/13/2023
8.0.0-pre.23 77 10/13/2023
8.0.0-pre.22 84 10/13/2023
8.0.0-pre.21 92 10/12/2023
8.0.0-pre.20 95 10/11/2023
8.0.0-pre.19 102 10/9/2023
8.0.0-pre.18 77 10/9/2023
8.0.0-pre.17 75 10/7/2023
8.0.0-pre.16 110 10/6/2023
8.0.0-pre.15 84 10/6/2023
8.0.0-pre.14 85 10/5/2023
8.0.0-pre.13 68 10/5/2023
8.0.0-pre.12 83 10/4/2023
8.0.0-pre.11 84 10/3/2023
8.0.0-pre.10 78 10/3/2023
8.0.0-pre.9 79 10/3/2023
8.0.0-pre.8 80 10/2/2023
8.0.0-pre.7 87 10/2/2023
8.0.0-pre.6 91 9/29/2023
7.6.1 51,931 9/29/2023
7.6.0 2,642 9/19/2023
7.5.0 887 9/13/2023
7.4.5 454 9/13/2023
7.4.4 6,981 8/28/2023
7.4.3 425 8/28/2023
7.4.2 5,045 7/17/2023
7.4.1 534 7/17/2023
7.4.0 18,442 6/26/2023
7.3.0 1,430 6/1/2023
7.2.0 9,538 4/14/2023
7.1.2 674 4/14/2023
7.1.1 6,942 3/1/2023
7.1.0 654 3/1/2023
7.0.10 728 2/27/2023
7.0.9 725 2/23/2023
7.0.8 686 2/23/2023
7.0.7 892 2/14/2023
7.0.6 1,261 2/6/2023
7.0.5 896 2/3/2023
7.0.4 789 1/30/2023
7.0.3 718 1/30/2023
7.0.2 987 1/26/2023
7.0.1 1,057 1/23/2023 7.0.1 is deprecated because it has critical bugs.
7.0.0 830 1/19/2023 7.0.0 is deprecated because it has critical bugs.

'# [9.5.0](https://github.com/buehler/dotnet-operator-sdk/compare/v9.4.1...v9.5.0) (2025-05-08)


### Bug Fixes

* Change the labels to an array ([#881](https://github.com/buehler/dotnet-operator-sdk/issues/881)) ([4e0c205](https://github.com/buehler/dotnet-operator-sdk/commit/4e0c2054f9a6bc10ac694c04c68e518c1357c687)), closes [/#diff-cf95f5a9676479797ff0ade11e4dd8d679dd1974142058976f3a35ca6a63b4f2L39-R45](https://github.com///issues/diff-cf95f5a9676479797ff0ade11e4dd8d679dd1974142058976f3a35ca6a63b4f2L39-R45) [/#diff-cf95f5a9676479797ff0ade11e4dd8d679dd1974142058976f3a35ca6a63b4f2L54-R62](https://github.com///issues/diff-cf95f5a9676479797ff0ade11e4dd8d679dd1974142058976f3a35ca6a63b4f2L54-R62) [/#diff-cf95f5a9676479797ff0ade11e4dd8d679dd1974142058976f3a35ca6a63b4f2L74-R109](https://github.com///issues/diff-cf95f5a9676479797ff0ade11e4dd8d679dd1974142058976f3a35ca6a63b4f2L74-R109) [/#diff-cf95f5a9676479797ff0ade11e4dd8d679dd1974142058976f3a35ca6a63b4f2L159-R159](https://github.com///issues/diff-cf95f5a9676479797ff0ade11e4dd8d679dd1974142058976f3a35ca6a63b4f2L159-R159) [/#diff-cf95f5a9676479797ff0ade11e4dd8d679dd1974142058976f3a35ca6a63b4f2L170-R174](https://github.com///issues/diff-cf95f5a9676479797ff0ade11e4dd8d679dd1974142058976f3a35ca6a63b4f2L170-R174)
* **deps:** update dependencies ([#853](https://github.com/buehler/dotnet-operator-sdk/issues/853)) ([5a99011](https://github.com/buehler/dotnet-operator-sdk/commit/5a99011c33bc87daa49e7ce1c8d7d4fb62663ed9))
* **deps:** update dependency sonaranalyzer.csharp to 10.9.0.115408 ([#878](https://github.com/buehler/dotnet-operator-sdk/issues/878)) ([74c0a66](https://github.com/buehler/dotnet-operator-sdk/commit/74c0a66e502431d4f372d32b6d118e9f7d9426fa))


### Features

* Add `AllExplicit` RBAC verb & state all default V1Lease RBAC verbs explicitly ([#879](https://github.com/buehler/dotnet-operator-sdk/issues/879)) ([92063d8](https://github.com/buehler/dotnet-operator-sdk/commit/92063d8b7a8f5d45f6c79e0e1a85101370679255))



'