Kubernetes C# Client

repository·master·Indexed 22 days ago

https://github.com/kubernetes-client/csharp

A .NET client library for interacting with the Kubernetes API server. It provides strongly-typed models and API wrappers for managing Kubernetes resources, including support for Custom Resource Definitions (CRDs) via a generic client. The library supports multiple configuration methods, including local kubeconfig, in-cluster configuration, and authentication to AKS using kubelogin and Managed Identity (MSI). Available as the KubernetesClient NuGet package.

Tokens
2.2K
Snippets
8
Records
12
Agent score
77%

What's inside kubernetes-client-csharp

  1. Manage Custom Resources with the Generic Client

    master

    The Kubernetes C# Client provides a generic mechanism to create, get, list, and delete custom resources. This allows you to interact with any resource defined by a CRD without needing pre-generated strongly-typed classes for every specific custom type.

    Typical workflow:

    1. Initialize configuration via BuildConfigFromConfigFile().
    2. Use the generic client to perform CRUD operations on the custom resource group (e.g., customresources.csharp.com).
    3. Access the Spec and Labels of the retrieved resource.
  2. Create and verify a Custom Resource Definition (CRD)

    master

    Before interacting with custom resources via the client, you must ensure the Custom Resource Definition (CRD) is registered in your Kubernetes cluster.

    1. Create the CRD using kubectl:
    kubectl create -f ./config/crd.yaml
    1. Verify the CRD exists by creating an instance and checking the resource type:
    kubectl create -f ./config/yaml-cr-instance.yaml
    kubectl get customresources.csharp.com
    kubectl create -f ./config/crd.yaml
    kubectl create -f ./config/yaml-cr-instance.yaml
    kubectl get customresources.csharp.com
  3. Configure the Kubernetes client

    master

    You can initialize the client using several configuration methods depending on your environment:

    1. Default KubeConfig: Loads the standard configuration from your local machine.
    2. Specific KubeConfig File: Loads configuration from a path specified by the KUBECONFIG environment variable or a provided path.
    3. In-Cluster Configuration: Used when your application is running inside a Kubernetes cluster, utilizing the service account.

    Once configured, pass the configuration object to the Kubernetes class to create the client.

    var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
    
    // Load from a specific file:
    var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(Environment.GetEnvironmentVariable("KUBECONFIG"));
    
    // Load from in-cluster configuration:
    var config = KubernetesClientConfiguration.InClusterConfig();
    
    // Use the config object to create a client.
    var client = new Kubernetes(config);
  4. Authenticate to AKS using kubelogin and Managed Identity (MSI)

    master

    This example demonstrates how to use the kubelogin tool to authenticate against an Azure Kubernetes Service (AKS) cluster using Managed Identities (MSI) within a C# application.

    Prerequisites

    Before running the example, ensure the following Azure and local environment configurations are met:

    1. AKS Configuration: Enable Azure Active Directory (AAD) support for your AKS cluster.
    2. Managed Identity Setup:
      • Create a managed identity for the AKS cluster.
      • Assign the managed identity the Azure Kubernetes Service RBAC Cluster Admin role (or appropriate RBAC permissions) on the AKS cluster.
      • Assign the managed identity to the Virtual Machine (VM) where the code will execute.
    3. Local Tooling: Install the kubelogin executable on your machine.

    Running the Example

    Note: The code must be executed on a VM that has the Managed Identity assigned.

    1. Open the example code and update the following placeholders:
      • server: The API server address of your AKS cluster.
      • clientid: The Client ID of your managed identity.
      • kubelogin: The local file path to the kubelogin executable.
    2. Execute the application using the .NET CLI:
    dotnet run
  5. Workaround for unsupported authentication providers

    master

    If you encounter an authentication provider that is not yet supported by BuildConfigFromConfigFile, you can use kubectl proxy as a workaround.

    1. Start the proxy locally:
      kubectl proxy
    2. Configure the client to point to the local proxy host (usually http://127.0.0.1:8001):
      var config = new KubernetesClientConfiguration { Host = "http://127.0.0.1:8001" };
      var client = new Kubernetes(config);
      Note: This is a workaround and is not recommended for production use.
    var config = new KubernetesClientConfiguration { Host = "http://127.0.0.1:8001" };
  6. List Kubernetes objects

    master

    Use the API groups (e.g., CoreV1) to list resources. For example, you can iterate through all namespaces and then list the pods within each namespace.

    var namespaces = client.CoreV1.ListNamespace();
    foreach (var ns in namespaces.Items) {
        Console.WriteLine(ns.Metadata.Name);
        var list = client.CoreV1.ListNamespacedPod(ns.Metadata.Name);
        foreach (var item in list.Items)
        {
            Console.WriteLine(item.Metadata.Name);
        }
    }
  7. Create and delete Kubernetes objects

    master

    You can create and delete resources by passing the appropriate model objects (like V1Namespace) to the corresponding API methods.

    var ns = new V1Namespace
    {
        Metadata = new V1ObjectMeta
        {
            Name = "test"
        }
    };
    
    var result = client.CoreV1.CreateNamespace(ns);
    Console.WriteLine(result);
    
    var status = client.CoreV1.DeleteNamespace(ns.Metadata.Name, new V1DeleteOptions());
  8. SDK and Kubernetes Version Compatibility

    master

    The SDK follows a version skew policy. Generally, an SDK version is compatible with Kubernetes clusters within an n-2 or n-3 version range (depending on the specific version).

    SDK VersionKubernetes Version.NET Targeting
    20.01.36net8.0;net9.0;net10.0;net48*;netstandard2.0*
    19.01.35net8.0;net9.0;net10.0;net48*;netstandard2.0*
    18.01.34net8.0;net9.0;net10.0;net48*;netstandard2.0*
    17.01.33net8.0;net9.0;net48*
    16.01.32net8.0;net9.0;net48*
    15.01.31net6.0;net8.0;net48*;netstandard2.0*
    14.01.30net6.0;net8.0;net48*;netstandard2.0*
    13.01.29net6.0;net7.0;net8.0;net48*
    12.01.28net6.0;net7.0;net48*
    11.01.27net6.0;net7.0;net48*
    10.01.26net6.0;net7.0;net48*
    9.11.25netstandard2.1;net6.0;net48*;netstandard2.0*
    9.01.25netstandard2.1;net5.0;net6.0;net48*;netstandard2.0*
    8.01.24netstandard2.1;net5.0;net6.0;net48*;netstandard2.0*
    7.21.23netstandard2.1;net5.0;net6.0;net48*;netstandard2.0*
    7.01.23netstandard2.1;net5.0;net6.0
    6.01.22netstandard2.1;net5.0
    5.01.21netstandard2.1;net5
    4.01.20netstandard2.0;netstandard2.1
    3.01.19netstandard2.0;net452
    2.01.18netstandard2.0;net452
    1.61.16netstandard1.4;netstandard2.0;net452
    1.41.13netstandard1.4;net451
    1.31.12netstandard1.4;net452