This documentation outlines the configuration options available for the Kubernetes client in a C# development environment. The following sections detail the steps to configure the client, including necessary code examples.

Setting Up Your Configuration

Using KubeConfig Files

To start configuring your Kubernetes client, you can use the BuildConfigFromConfigFile() function, which allows your application to read from a kubeconfig file.

  1. Ensure the KUBECONFIG environment variable is set, which defines the path to your kubeconfig file.

    In a console application, this can be set up as follows:

    Environment.SetEnvironmentVariable("KUBECONFIG", "/path/to/your/kubeconfig.yaml");
    
  2. Build the configuration using the following code:

    var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
    

    This will load the configurations specified in your kubeconfig file.

Environment Variables

The Kubernetes client can also be configured using environment variables, which allows for flexibility in how configurations are managed across different environments.

  1. You can set environment variables programmatically within your application:

    var execInfo = new {
        apiVersion = "client.authentication.k8s.io/v1alpha1",
        kind = "ExecCredentials",
        spec = new { interactive = Environment.UserInteractive }
    };
    
    process.StartInfo.EnvironmentVariables.Add("KUBERNETES_EXEC_INFO", JsonSerializer.Serialize(execInfo));
    
  2. If you have multiple environment variables defined, the client will utilize them as follows:

    if (config.EnvironmentVariables != null)
    {
        foreach (var configEnvironmentVariable in config.EnvironmentVariables)
        {
            if (configEnvironmentVariable.ContainsKey("name") && configEnvironmentVariable.ContainsKey("value"))
            {
                var name = configEnvironmentVariable["name"];
                process.StartInfo.EnvironmentVariables[name] = configEnvironmentVariable["value"];
            }
        }
    }
    

Testing Configuration

If you want to verify that your environment variables or kubeconfig file are loaded correctly, consider implementing unit tests to validate this configuration. For example:

[Fact]
public void LoadKubeConfigFromEnvironmentVariableMultipleConfigs()
{
    var filePath = Path.GetFullPath("assets/kubeconfig.relative.yml");
    var environmentVariable = "KUBECONFIG_LoadKubeConfigFromEnvironmentVariable_MultipleConfigs";

    Environment.SetEnvironmentVariable(environmentVariable, $"{filePath};{filePath}");
    KubernetesClientConfiguration.KubeConfigEnvironmentVariable = environmentVariable;

    var cfg = KubernetesClientConfiguration.BuildDefaultConfig();

    Assert.NotNull(cfg);
}

This test ensures that multiple configurations can be loaded without errors.

Advanced Configuration

For advanced usage, you may want to include specific settings in your kubeconfig file. This scenario could include specifying context, clusters, or user details as shown below:

apiVersion: v1
clusters:
- cluster:
    server: https://example.com
  name: my-cluster
contexts:
- context:
    cluster: my-cluster
    user: my-user
  name: my-context
current-context: my-context
kind: Config
preferences: {}
users:
- name: my-user
  user:
    password: my-password
    username: my-username

The above example illustrates a typical structure of a kubeconfig where you define clusters, contexts, and users. The client will utilize these configurations when connecting to the Kubernetes cluster.

By following these guidelines, developers can efficiently set up the Kubernetes client and work with Kubernetes resources in a C# environment.

Source: Kubernetes Client for C#