Skip to content

Christian-Oleson/csharp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Kubernetes C# Client

Travis Client Capabilities Client Support Level

Usage

Nuget Package

dotnet add package KubernetesClient

Authentication/Configuration

You should be able to use a 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 with the in-cluster service account using the InClusterConfig function shown below.

Monitoring

There is optional built-in metric generation for prometheus client metrics. The exported metrics are:

  • k8s_dotnet_request_total - Counter of request, broken down by HTTP Method
  • k8s_dotnet_response_code_total - Counter of responses, broken down by HTTP Method and response code
  • k8s_request_latency_seconds - Latency histograms broken down by method, api group, api version and resource kind

There is an example integrating these monitors in the examples/prometheus directory.

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 [email protected]:kubernetes-client/csharp.git
cd csharp\examples\simple
dotnet run

Known issues

While the 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. You can still connect to a cluster by starting the proxy command:

$ 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.

About

Work In Progress

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.9%
  • Other 0.1%