Kubernetes API Interaction

This section outlines how the Kubernetes client library for C# interacts with the Kubernetes API, including the methods available for creating, reading, updating, and deleting resources.

Core Concepts:

  • Kubernetes Client: The Kubernetes class is the core of the library, providing access to all Kubernetes API endpoints. It’s instantiated using a KubernetesClientConfiguration object.
  • KubernetesClientConfiguration: This class handles the connection to the Kubernetes cluster, allowing you to specify the configuration, such as the kubeconfig file, the API server endpoint, and the current context.

Configuration Options:

  • Loading from Kubeconfig:
    // Load from the default kubeconfig on the machine.
              var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
              
              // Load from a specific file:
              var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(Environment.GetEnvironmentVariable("KUBECONFIG")); 
              
  • In-Cluster Configuration:
    // Load from in-cluster configuration:
              var config = KubernetesClientConfiguration.InClusterConfig();
              
  • Creating a Client:
    // Use the config object to create a client.
              var client = new Kubernetes(config);
              

API Interaction:

The library exposes various API endpoints through the I{{name}}Operations interface, where {{name}} represents the resource type you want to interact with (e.g., IDeploymentOperations, IConfigMapOperations, IJobOperations).

CRUD Operations:

  • Create: The Create method is used to create a new resource. It takes the resource object as an argument and returns the created resource.
  • Read: The Get method is used to retrieve an existing resource. It takes the resource name and namespace (if applicable) as arguments and returns the resource object.
  • Update: The Patch or Replace methods are used to update an existing resource. Patch applies partial modifications, while Replace overwrites the entire resource.
  • Delete: The Delete method is used to delete a resource. It takes the resource name and namespace (if applicable) as arguments.

Example:

// Create a new deployment:
          var deployment = new V1Deployment { 
              // ... populate deployment object 
          };
          var createdDeployment = client.Create(deployment);
          
          // Read the deployment:
          var retrievedDeployment = client.Get(deployment.Name, deployment.Namespace);
          
          // Update the deployment:
          retrievedDeployment.Spec.Replicas = 3;
          client.Replace(retrievedDeployment);
          
          // Delete the deployment:
          client.Delete(deployment.Name, deployment.Namespace);
          

API Compatibility:

The library aims to support a range of Kubernetes versions, but the exact compatibility depends on the specific SDK version and Kubernetes cluster version. Generally, the SDK should work with clusters at least two versions behind the SDK version. For example, an SDK based on Kubernetes 1.21 should work with a 1.19 cluster but not guaranteed to work with a 1.18 cluster.

Important Note: Fixes and security updates are not automatically back-ported to older SDK versions. However, community contributions are welcome. For more information on contributing, see the Contributing guide.

Top-Level Directory Explanations

examples/ - This directory contains example projects and usage scenarios for the Kubernetes client library.

examples/aks-kubelogin/ - This subdirectory contains an example of using the Kubernetes client library with Azure Kubernetes Service (AKS) and kubelogin.

examples/aot/ - This subdirectory contains examples using ahead-of-time (AOT) compilation.

examples/attach/ - This subdirectory contains examples of attaching to running Kubernetes processes.

examples/exec/ - This subdirectory contains examples of executing commands on Kubernetes clusters.

examples/watch/ - This subdirectory contains examples of watching Kubernetes resources for changes.

examples/webApiDependencyInjection/ - This subdirectory contains an example of using dependency injection in a web API using the Kubernetes client library.

examples/webApiDependencyInjection/Controllers/ - This subdirectory contains controller classes for the web API example.

examples/workerServiceDependencyInjection/ - This subdirectory contains an example of using dependency injection in a worker service using the Kubernetes client library.

src/ - This directory contains the source code for the project.

src/KubernetesClient.Aot/ - This subdirectory contains ahead-of-time (AOT) compiled code for the Kubernetes client library.

src/KubernetesClient.Aot/KubeConfigModels/ - This subdirectory contains AOT compiled model classes for working with Kubernetes configuration files.

src/KubernetesClient.Classic/ - This subdirectory contains the classic (non-AOT) implementation of the Kubernetes client library.

src/KubernetesClient.Kubectl/ - This subdirectory contains code for implementing kubectl functionality in the Kubernetes client library.

src/KubernetesClient.Kubectl/Beta/ - This subdirectory contains beta (experimental) functionality for the kubectl implementation.

src/KubernetesClient/ - This subdirectory contains the main Kubernetes client library source code.

src/KubernetesClient/Authentication/ - This subdirectory contains authentication-related code for the Kubernetes client library.

src/KubernetesClient/Autorest/ - This subdirectory contains Autorest code for generating client code from OpenAPI definitions.

src/KubernetesClient/KubeConfigModels/ - This subdirectory contains model classes for working with Kubernetes configuration files.

src/KubernetesClient/LeaderElection/ - This subdirectory contains code for implementing leader election in the Kubernetes client library.

src/KubernetesClient/Models/ - This subdirectory contains model classes for various Kubernetes resources and objects.

src/LibKubernetesGenerator/ - This subdirectory contains code for generating C# code from OpenAPI definitions.

tests/ - This directory contains test code for the project.

tests/E2E.Aot.Tests/ - This subdirectory contains end-to-end tests for the AOT compiled code.

tests/E2E.Tests/ - This subdirectory contains end-to-end tests for the non-AOT code.

tests/KubernetesClient.Tests/ - This subdirectory contains tests for the main implementation of the Kubernetes client library.

tests/KubernetesClient.Tests/assets/ - This subdirectory contains test assets.

tests/KubernetesClient.Tests/Mock/ - This subdirectory contains tests using mocks.