2017-10-13 03:44:59 +08:00
# Kubernetes C# Client
2024-03-10 14:43:59 +08:00
[](https://github.com/kubernetes-client/csharp/actions/workflows/buildtest.yaml)
2017-11-14 22:10:47 -08:00
[](http://bit.ly/kubernetes-client-capabilities-badge)
[](http://bit.ly/kubernetes-client-support-badge)
2017-06-06 23:02:14 -07:00
2017-09-13 14:49:35 -07:00
# Usage
2024-02-27 19:41:49 +08:00
[](https://www.nuget.org/packages/KubernetesClient/)
2017-09-13 14:49:35 -07:00
```sh
dotnet add package KubernetesClient
```
2024-01-22 12:15:51 -08:00
## Generate with Visual Studio
```
2024-04-15 10:40:59 -07:00
dotnet msbuild /Restore /t:SlnGen kubernetes-client.proj
2024-01-22 12:15:51 -08:00
```
2019-03-22 16:58:09 -07:00
## Authentication/Configuration
2021-08-05 01:53:24 +11:00
You should be able to use a standard KubeConfig file with this library,
2019-03-22 16:58:09 -07:00
see the `BuildConfigFromConfigFile` function below. Most authentication
2022-03-29 22:01:26 +02:00
methods are currently supported, but a few are not, see the
2021-08-05 01:53:24 +11:00
[known-issues ](https://github.com/kubernetes-client/csharp#known-issues ).
2017-06-06 23:02:14 -07:00
2021-08-05 01:53:24 +11:00
You should also be able to authenticate with the in-cluster service
2019-03-22 16:58:09 -07:00
account using the `InClusterConfig` function shown below.
2018-02-16 07:34:05 +01:00
2021-04-09 08:53:05 -07:00
## Monitoring
2024-02-26 15:56:13 -08:00
Metrics are built in to HttpClient using System.Diagnostics.DiagnosticsSource.
https://learn.microsoft.com/en-us/dotnet/core/diagnostics/built-in-metrics-system-net
2021-04-09 08:53:05 -07:00
2024-02-26 15:56:13 -08:00
There are many ways these metrics can be consumed/exposed but that decision is up to the application, not KubernetesClient itself.
https://learn.microsoft.com/en-us/dotnet/core/diagnostics/metrics-collection
2021-04-09 08:53:05 -07:00
2019-03-22 16:58:09 -07:00
## Sample Code
2018-02-16 07:34:05 +01:00
2019-03-22 16:58:09 -07:00
### Creating the client
```c#
// Load from the default kubeconfig on the machine.
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
2017-06-06 23:02:14 -07:00
2019-03-22 16:58:09 -07:00
// 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);
2017-06-06 23:02:14 -07:00
```
2019-03-22 16:58:09 -07:00
### Listing Objects
```c#
2022-05-09 14:04:32 -07:00
var namespaces = client.CoreV1.ListNamespace();
2019-03-22 16:58:09 -07:00
foreach (var ns in namespaces.Items) {
Console.WriteLine(ns.Metadata.Name);
2022-05-09 14:04:32 -07:00
var list = client.CoreV1.ListNamespacedPod(ns.Metadata.Name);
2019-03-22 16:58:09 -07:00
foreach (var item in list.Items)
{
Console.WriteLine(item.Metadata.Name);
}
}
```
2017-06-06 23:02:14 -07:00
2019-03-22 16:58:09 -07:00
### Creating and Deleting Objects
```c#
var ns = new V1Namespace
{
Metadata = new V1ObjectMeta
{
Name = "test"
}
};
2022-05-09 14:04:32 -07:00
var result = client.CoreV1.CreateNamespace(ns);
2019-03-22 16:58:09 -07:00
Console.WriteLine(result);
2022-05-09 14:04:32 -07:00
var status = client.CoreV1.DeleteNamespace(ns.Metadata.Name, new V1DeleteOptions());
2017-06-06 23:02:14 -07:00
```
2017-06-22 22:41:42 +02:00
2019-03-22 16:58:09 -07:00
## Examples
There is extensive example code in the [examples directory ](https://github.com/kubernetes-client/csharp/tree/master/examples ).
2017-08-09 16:49:45 -07:00
2021-08-05 01:53:24 +11:00
### Running the examples
2017-08-09 16:49:45 -07:00
```bash
git clone git@github .com:kubernetes-client/csharp.git
cd csharp\examples\simple
dotnet run
```
2018-02-13 16:43:35 +02:00
## Known issues
2021-08-05 01:53:24 +11:00
While the preferred way of connecting to a remote cluster from local machine is:
2018-02-13 16:43:35 +02:00
2021-08-05 01:53:24 +11:00
```c#
2018-02-13 16:43:35 +02:00
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
var client = new Kubernetes(config);
```
2024-02-13 04:04:17 +10:00
Not all auth providers are supported at the moment [#91 ](https://github.com/kubernetes-client/csharp/issues/91#issuecomment-362920478 ). You can still connect to a cluster by starting the proxy command:
2018-02-13 16:43:35 +02:00
```bash
$ kubectl proxy
Starting to serve on 127.0.0.1:8001
```
and changing config:
2021-08-05 01:53:24 +11:00
```c#
2018-02-13 16:43:35 +02:00
var config = new KubernetesClientConfiguration { Host = "http://127.0.0.1:8001" };
```
2021-08-05 01:53:24 +11:00
Notice that this is a workaround and is not recommended for production use.
2018-02-13 16:43:35 +02:00
2017-08-09 16:49:45 -07:00
## Testing
2017-06-22 22:41:42 +02:00
2022-09-14 02:43:01 -06:00
The project uses [XUnit ](https://github.com/xunit/xunit ) as unit testing framework.
2017-06-22 22:41:42 +02:00
2021-08-05 01:53:24 +11:00
To run the tests:
2017-06-22 22:41:42 +02:00
```bash
2017-08-09 16:49:45 -07:00
cd csharp\tests
2017-06-22 22:41:42 +02:00
dotnet restore
2017-10-13 03:44:59 +08:00
dotnet test
2017-09-13 14:33:06 -07:00
```
2018-08-20 04:16:42 +05:30
2022-02-25 13:31:24 -08:00
# Update the API model
2019-03-22 16:58:09 -07:00
## Prerequisites
You'll need a Linux machine with Docker.
Check out the generator project into some other directory
2021-08-05 01:53:24 +11:00
(henceforth `$GEN_DIR` ).
2019-03-22 16:58:09 -07:00
```bash
cd $GEN_DIR/..
git clone https://github.com/kubernetes-client/gen
```
2022-02-25 13:31:24 -08:00
## Generating new swagger.json
2019-03-22 16:58:09 -07:00
```bash
# Where REPO_DIR points to the root of the csharp repository
2022-03-29 22:01:26 +02:00
cd
2022-02-25 13:31:24 -08:00
${GEN_DIR}/openapi/csharp.sh ${REPO_DIR}/src/KubernetesClient ${REPO_DIR}/csharp.settings
2019-03-22 16:58:09 -07:00
```
2022-03-29 22:01:26 +02:00
# Version Compatibility
2021-09-29 15:32:45 -07:00
2024-02-13 04:04:17 +10:00
| SDK Version | Kubernetes Version | .NET Targeting |
|-------------|--------------------|-----------------------------------------------------|
2025-09-22 14:18:16 -07:00
| 18.0 | 1.34 | net8.0;net9.0;net48*;netstandard2.0* |
2025-04-27 12:55:24 -07:00
| 17.0 | 1.33 | net8.0;net9.0;net48*;netstandard2.0* |
2025-01-07 07:12:29 -08:00
| 16.0 | 1.32 | net8.0;net9.0;net48*;netstandard2.0* |
| 15.0 | 1.31 | net6.0;net8.0;net48*;netstandard2.0* |
| 14.0 | 1.30 | net6.0;net8.0;net48*;netstandard2.0* |
2024-02-13 04:04:17 +10:00
| 13.0 | 1.29 | net6.0;net7.0;net8.0;net48*;netstandard2.0* |
| 12.0 | 1.28 | net6.0;net7.0;net48*;netstandard2.0* |
| 11.0 | 1.27 | net6.0;net7.0;net48*;netstandard2.0* |
| 10.0 | 1.26 | net6.0;net7.0;net48*;netstandard2.0* |
| 9.1 | 1.25 | netstandard2.1;net6.0;net7.0;net48*;netstandard2.0* |
| 9.0 | 1.25 | netstandard2.1;net5.0;net6.0;net48*;netstandard2.0* |
| 8.0 | 1.24 | netstandard2.1;net5.0;net6.0;net48*;netstandard2.0* |
| 7.2 | 1.23 | netstandard2.1;net5.0;net6.0;net48*;netstandard2.0* |
| 7.0 | 1.23 | netstandard2.1;net5.0;net6.0 |
| 6.0 | 1.22 | netstandard2.1;net5.0 |
| 5.0 | 1.21 | netstandard2.1;net5 |
| 4.0 | 1.20 | netstandard2.0;netstandard2.1 |
| 3.0 | 1.19 | netstandard2.0;net452 |
| 2.0 | 1.18 | netstandard2.0;net452 |
| 1.6 | 1.16 | netstandard1.4;netstandard2.0;net452; |
| 1.4 | 1.13 | netstandard1.4;net451 |
| 1.3 | 1.12 | netstandard1.4;net452 |
2021-09-29 15:32:45 -07:00
2021-12-13 07:31:59 -08:00
* Starting from `2.0` , [dotnet sdk versioning ](https://github.com/kubernetes-client/csharp/issues/400 ) adopted
2021-09-29 15:32:45 -07:00
* `Kubernetes Version` here means the version sdk models and apis were generated from
2024-02-13 04:04:17 +10:00
* Kubernetes api server guarantees the compatibility with `n-2` (`n-3` after 1.28) version. for example:
- 1.19 based sdk should work with 1.21 cluster, but not guaranteed to work with 1.22 cluster.< br >
and vice versa:
- 1.21 based sdk should work with 1.19 cluster, but not guaranteed to work with 1.18 cluster.< br >
Note: in practice, the sdk might work with much older clusters, at least for the more stable functionality. However, it is not guaranteed past the `n-2` (or `n-3` after 1.28 ) version. See [#1511 ](https://github.com/kubernetes-client/csharp/issues/1511 ) for additional details.< br >
see also < https: / / kubernetes . io / releases / version-skew-policy / >
* Fixes (including security fixes) are not back-ported automatically to older sdk versions. However, contributions from the community are welcomed 😊; See [Contributing ](#contributing ) for instructions on how to contribute.
2022-05-09 14:04:32 -07:00
* `*` `KubernetesClient.Classic` : netstandard2.0 and net48 are supported with limited features
2021-09-29 15:32:45 -07:00
2018-08-20 04:16:42 +05:30
## Contributing
Please see [CONTRIBUTING.md ](CONTRIBUTING.md ) for instructions on how to contribute.