KubeOps.KubernetesClient 9.8.1

dotnet add package KubeOps.KubernetesClient --version 9.8.1
                    
NuGet\Install-Package KubeOps.KubernetesClient -Version 9.8.1
                    
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.8.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KubeOps.KubernetesClient" Version="9.8.1" />
                    
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.8.1
                    
#r "nuget: KubeOps.KubernetesClient, 9.8.1"
                    
#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.8.1
                    
Install KubeOps.KubernetesClient as a Cake Addin
#tool nuget:?package=KubeOps.KubernetesClient&version=9.8.1
                    
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 simplifies common operator tasks by offering:

  • True Generic Methods: Perform operations like Get, List, Create, Update, Delete, and Watch on any Kubernetes resource type (including custom resources defined with [KubernetesEntity]) using strongly-typed generic methods, without manually specifying API group, version, and plural name.
  • Simplified API: Reduces boilerplate code 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 additional features, such as true generics and method variants. The original GenericClient supports generics in a limited way, requiring initialization with group and kind information.

The client acts as a wrapper, automatically 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 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
    }
}

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. The client automatically loads 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

List Resources

var client = new KubernetesClient() as IKubernetesClient;

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

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

// List all custom resources of type V1MyCrd across all namespaces
// Note: This requires appropriate RBAC permissions for cluster-scoped resources
var allMyCrds = await client.ListAsync<V1MyCrd>(null);

Get Resources

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

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

Create Resources

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 Resources

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

Update Resource Status

// Update the status of a custom resource
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 Resources

// Delete by name and namespace
await client.DeleteAsync<V1Pod>("pod-to-delete", "default");

// Delete using an existing resource instance
var crdToDelete = await client.GetAsync<V1MyCrd>("crd-instance-to-delete", "dev");
if (crdToDelete != null)
{
    await client.DeleteAsync(crdToDelete);
}
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.8.1 576 6/13/2025
9.8.0 665 6/10/2025
9.7.0 512 6/6/2025
9.6.0 2,383 5/23/2025
9.5.0 2,385 5/8/2025
9.4.1 3,112 4/29/2025
9.4.0 309 4/28/2025
9.3.0 11,563 3/26/2025
9.2.0 11,772 1/24/2025
9.1.5 47,079 9/10/2024
9.1.4 1,589 8/26/2024
9.1.3 21,397 6/28/2024
9.1.2 11,435 6/20/2024
9.1.1 5,827 5/22/2024
9.1.0 2,754 5/15/2024
9.0.2 281 5/13/2024
9.0.0 29,305 3/13/2024
9.0.0-pre.4 87 4/19/2024
9.0.0-pre.3 82 3/21/2024
9.0.0-pre.2 72 3/13/2024
9.0.0-pre.1 96 3/7/2024
8.0.2-pre.2 91 2/21/2024
8.0.2-pre.1 90 2/19/2024
8.0.1 11,778 2/13/2024
8.0.1-pre.7 86 2/12/2024
8.0.1-pre.6 87 2/7/2024
8.0.1-pre.5 75 2/5/2024
8.0.1-pre.4 85 1/31/2024
8.0.1-pre.3 83 1/26/2024
8.0.1-pre.2 78 1/25/2024
8.0.1-pre.1 86 1/18/2024
8.0.0 1,113 1/17/2024
8.0.0-pre.45 78 1/17/2024
8.0.0-pre.44 84 1/16/2024
8.0.0-pre.43 73 1/16/2024
8.0.0-pre.42 988 1/10/2024
8.0.0-pre.41 310 1/2/2024
8.0.0-pre.40 188 12/27/2023
8.0.0-pre.39 92 12/21/2023
8.0.0-pre.38 440 12/6/2023
8.0.0-pre.37 133 12/6/2023
8.0.0-pre.36 166 12/3/2023
8.0.0-pre.35 113 11/28/2023
8.0.0-pre.34 118 11/24/2023
8.0.0-pre.33 89 11/24/2023
8.0.0-pre.32 75 11/23/2023
8.0.0-pre.31 81 11/23/2023
8.0.0-pre.30 99 11/23/2023
8.0.0-pre.29 965 11/11/2023
8.0.0-pre.28 107 11/8/2023
8.0.0-pre.27 590 10/23/2023
8.0.0-pre.26 121 10/19/2023
8.0.0-pre.25 85 10/18/2023
8.0.0-pre.24 106 10/13/2023
8.0.0-pre.23 83 10/13/2023
8.0.0-pre.22 89 10/13/2023
8.0.0-pre.21 95 10/12/2023
8.0.0-pre.20 99 10/11/2023
8.0.0-pre.19 106 10/9/2023
8.0.0-pre.18 83 10/9/2023
8.0.0-pre.17 80 10/7/2023
8.0.0-pre.16 120 10/6/2023
8.0.0-pre.15 88 10/6/2023
8.0.0-pre.14 89 10/5/2023
8.0.0-pre.13 73 10/5/2023
8.0.0-pre.12 89 10/4/2023
8.0.0-pre.11 88 10/3/2023
8.0.0-pre.10 82 10/3/2023
8.0.0-pre.9 82 10/3/2023
8.0.0-pre.8 92 10/2/2023
8.0.0-pre.7 92 10/2/2023
8.0.0-pre.6 96 9/29/2023
7.6.1 52,561 9/29/2023
7.6.0 2,682 9/19/2023
7.5.0 891 9/13/2023
7.4.5 459 9/13/2023
7.4.4 6,985 8/28/2023
7.4.3 430 8/28/2023
7.4.2 5,055 7/17/2023
7.4.1 540 7/17/2023
7.4.0 18,477 6/26/2023
7.3.0 1,434 6/1/2023
7.2.0 9,591 4/14/2023
7.1.2 680 4/14/2023
7.1.1 6,948 3/1/2023
7.1.0 658 3/1/2023
7.0.10 732 2/27/2023
7.0.9 732 2/23/2023
7.0.8 691 2/23/2023
7.0.7 897 2/14/2023
7.0.6 1,268 2/6/2023
7.0.5 901 2/3/2023
7.0.4 793 1/30/2023
7.0.3 727 1/30/2023
7.0.2 992 1/26/2023
7.0.1 1,063 1/23/2023 7.0.1 is deprecated because it has critical bugs.
7.0.0 840 1/19/2023 7.0.0 is deprecated because it has critical bugs.