Client Configuration and Initialization
This document outlines how to configure and initialize the Kubernetes client library for C#. The library provides several mechanisms for obtaining configuration details, including loading from a kubeconfig file, in-cluster configuration, and custom configuration options.
Loading from a kubeconfig file
The most common approach is to load configuration from a kubeconfig file. Kubeconfig files contain information about Kubernetes clusters, users, and contexts. This allows you to switch between different clusters and user accounts easily.
Load from the default kubeconfig file
The library automatically searches for the default kubeconfig file in standard locations (~/.kube/config
on Linux/macOS, %USERPROFILE%\.kube\config
on Windows).
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
Load from a specific kubeconfig file
You can explicitly specify the path to the kubeconfig file using the KUBECONFIG
environment variable:
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(Environment.GetEnvironmentVariable("KUBECONFIG"));
Load from in-cluster configuration
When running inside a Kubernetes cluster, you can load configuration from the in-cluster environment:
var config = KubernetesClientConfiguration.InClusterConfig();
Creating a Kubernetes Client
Once you have obtained the client configuration, you can create a new Kubernetes
client:
var client = new Kubernetes(config);
Authentication Methods
The Kubernetes client library supports various authentication methods:
- Basic Authentication: Use basic authentication with a username and password.
- Token Authentication: Use a bearer token for authentication.
- Certificate Authentication: Authenticate with a client certificate and private key.
- Service Account Authentication: Use the service account credentials provided by the Kubernetes cluster.
- OpenID Connect (OIDC): Authenticate using OIDC flow.
The specific authentication method used depends on the configuration options specified in the kubeconfig file or provided manually.
Example: Using kubeconfig file
// Load from the default kubeconfig file
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
// Create a Kubernetes client
var client = new Kubernetes(config);
// Access resources using the client
var pods = client.ListNamespacedPod("default");
Example: Using custom configuration
// Create a new KubernetesClientConfiguration instance
var config = new KubernetesClientConfiguration
{
Host = "https://your-cluster-endpoint",
Namespace = "your-namespace",
// Add other configuration options
};
// Create a Kubernetes client
var client = new Kubernetes(config);
// Access resources using the client
var pods = client.ListNamespacedPod("your-namespace");
Further Exploration
For more advanced configuration options, including custom authentication methods and handling of multiple clusters, you can refer to the documentation on the Kubernetes client library GitHub repository.
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/metrics/ - This subdirectory contains examples of working with metrics in Kubernetes.
examples/openTelemetryConsole/ - This subdirectory contains an example of using OpenTelemetry Console with the Kubernetes client library.
examples/restart/ - This subdirectory contains examples of restarting Kubernetes pods.
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/ - 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/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/LibKubernetesGenerator/ - This subdirectory contains code for generating C# code from OpenAPI definitions.
tests/ - This directory contains test code for the project.
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.