Frederik Carlier 4e58609159 Support wildcard IPv4 and IPv6 addresses (#550)
Some tools can generate kubeconfig files which use wildcard IPv4 or IPv6 addresses. For example, using k3d with --api-server=https://0.0.0.0:6433/ would generate a kubeconfig file like this:

```
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: (...)
    server: https://0.0.0.0:6433
  name: k3d-k3s-default
```

Standard Kubernetes tools (like kubectl or Helm) correctly parse the 0.0.0.0 IP address and transform it 127.0.0.1; 3rd party tools like curl or wget will do the same on Unix systems.

This is default behavior on Unix but not on Windows. As a result, the .NET Kubernetes client will fail to work with kubeconfig files like this and you'll get HTTP exceptions.

Go has explicit workarounds for this (see 1a0b1cca4c), and this PR attemps to replicate these workarounds in the .NET client.
2021-01-28 00:19:48 -08:00
2020-12-19 08:26:25 -08:00
2017-10-24 17:05:26 +08:00
2018-08-19 15:46:42 -07:00
2020-04-22 13:41:45 -07:00
2020-11-18 17:26:50 -08:00
2021-01-19 14:07:59 -08:00
2017-05-12 10:18:12 -07:00
2019-03-24 15:43:05 -07:00
2018-06-25 07:14:27 -07:00

Kubernetes C# Client

Travis Client Capabilities Client Support Level

Usage

Nuget Package

dotnet add package KubernetesClient

Authentication/Configuration

You should be able to use an standard KubeConfig file with this library, see the BuildConfigFromConfigFile function below. Most authentication methods are currently supported, but a few are not, see the known-issues

You should also be able to authenticate using the in-cluster service account using the InClusterConfig function shown below.

Sample Code

Creating the client

// Load from the default kubeconfig on the machine.
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);

Listing Objects

var namespaces = client.ListNamespace();
foreach (var ns in namespaces.Items) {
    Console.WriteLine(ns.Metadata.Name);
    var list = client.ListNamespacedPod(ns.Metadata.Name);
    foreach (var item in list.Items)
    {
        Console.WriteLine(item.Metadata.Name);
    }
}

Creating and Deleting Objects

var ns = new V1Namespace
{
    Metadata = new V1ObjectMeta
    {
        Name = "test"
    }
};

var result = client.CreateNamespace(ns);
Console.WriteLine(result);

var status = client.DeleteNamespace(ns.Metadata.Name, new V1DeleteOptions());

Examples

There is extensive example code in the examples directory.

Running the Examples

git clone git@github.com:kubernetes-client/csharp.git
cd csharp\examples\simple
dotnet run

Known issues

While preferred way of connecting to a remote cluster from local machine is:

var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
var client = new Kubernetes(config);

Not all auth providers are supported at moment #91, but you still can connect to cluster by starting proxy:

$ kubectl proxy
Starting to serve on 127.0.0.1:8001

and changing config:

var config = new KubernetesClientConfiguration {  Host = "http://127.0.0.1:8001" };

Notice that this is a workaround and is not recommended for production use

Testing

The project uses XUnit as unit testing framework.

To run the tests

cd csharp\tests
dotnet restore
dotnet test

Generating the Client Code

Prerequisites

You'll need a Linux machine with Docker.

The generated code works on all platforms supported by .NET or .NET Core.

Check out the generator project into some other directory (henceforth $GEN_DIR)

cd $GEN_DIR/..
git clone https://github.com/kubernetes-client/gen

Generating code

# Where REPO_DIR points to the root of the csharp repository
cd ${REPO_DIR}/csharp/src/KubernetesClient
${GEN_DIR}/openapi/csharp.sh generated ../csharp.settings

Contributing

Please see CONTRIBUTING.md for instructions on how to contribute.

Description
No description provided
Readme Apache-2.0 11 MiB
Languages
C# 100%