Skip to content

Commit 3fd47e5

Browse files
Add an initial generic client. (#525)
1 parent 5be3cff commit 3fd47e5

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

examples/generic/Generic.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using k8s;
4+
using k8s.Models;
5+
6+
namespace exec
7+
{
8+
internal class Generic
9+
{
10+
private static async Task Main(string[] args)
11+
{
12+
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
13+
var generic = new GenericClient(config, "", "v1", "nodes");
14+
var node = await generic.ReadAsync<V1Node>("kube0").ConfigureAwait(false);
15+
Console.WriteLine(node.Metadata.Name);
16+
17+
var genericPods = new GenericClient(config, "", "v1", "pods");
18+
var pods = await genericPods.ListNamespacedAsync<V1PodList>("default").ConfigureAwait(false);
19+
foreach (var pod in pods.Items)
20+
{
21+
Console.WriteLine(pod.Metadata.Name);
22+
}
23+
}
24+
}
25+
}

examples/generic/generic.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\src\KubernetesClient\KubernetesClient.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<OutputType>Exe</OutputType>
9+
<TargetFramework>net5</TargetFramework>
10+
<LangVersion>7.1</LangVersion>
11+
</PropertyGroup>
12+
13+
</Project>

src/KubernetesClient/GenericClient.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
using Microsoft.Rest.Serialization;
2+
using System;
3+
using System.Net.Http;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
8+
namespace k8s
9+
{
10+
public class GenericClient : IDisposable
11+
{
12+
internal class TweakApiHandler : DelegatingHandler
13+
{
14+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage msg, CancellationToken cancel)
15+
{
16+
msg.RequestUri = new Uri(msg.RequestUri, msg.RequestUri.AbsolutePath.Replace("/apis//", "/api/"));
17+
return base.SendAsync(msg, cancel);
18+
}
19+
}
20+
21+
private IKubernetes kubernetes;
22+
private string group;
23+
private string version;
24+
private string plural;
25+
26+
27+
public GenericClient(KubernetesClientConfiguration config, string group, string version, string plural)
28+
{
29+
this.group = group;
30+
this.version = version;
31+
this.plural = plural;
32+
33+
if (string.IsNullOrEmpty(group))
34+
{
35+
this.kubernetes = new Kubernetes(config, new DelegatingHandler[] { new TweakApiHandler() });
36+
}
37+
else
38+
{
39+
this.kubernetes = new Kubernetes(config);
40+
}
41+
}
42+
43+
public async Task<T> ListAsync<T>(CancellationToken cancel = default(CancellationToken))
44+
where T : IKubernetesObject
45+
{
46+
var resp = await this.kubernetes.ListClusterCustomObjectWithHttpMessagesAsync(this.group, this.version, this.plural, cancellationToken: cancel).ConfigureAwait(false);
47+
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
48+
}
49+
50+
public async Task<T> ListNamespacedAsync<T>(string ns, CancellationToken cancel = default(CancellationToken))
51+
where T : IKubernetesObject
52+
{
53+
var resp = await this.kubernetes.ListNamespacedCustomObjectWithHttpMessagesAsync(this.group, this.version, ns, this.plural, cancellationToken: cancel).ConfigureAwait(false);
54+
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
55+
}
56+
57+
public async Task<T> ReadNamespacedAsync<T>(string ns, string name, CancellationToken cancel = default(CancellationToken))
58+
where T : IKubernetesObject
59+
{
60+
var resp = await this.kubernetes.GetNamespacedCustomObjectWithHttpMessagesAsync(this.group, this.version, ns, this.plural, name, cancellationToken: cancel).ConfigureAwait(false);
61+
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
62+
}
63+
64+
public async Task<T> ReadAsync<T>(string name, CancellationToken cancel = default(CancellationToken))
65+
where T : IKubernetesObject
66+
{
67+
var resp = await this.kubernetes.GetClusterCustomObjectWithHttpMessagesAsync(this.group, this.version, this.plural, name, cancellationToken: cancel).ConfigureAwait(false);
68+
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
69+
}
70+
71+
public async Task<T> DeleteAsync<T>(string name, CancellationToken cancel = default(CancellationToken))
72+
where T : IKubernetesObject
73+
{
74+
var resp = await this.kubernetes.DeleteClusterCustomObjectWithHttpMessagesAsync(this.group, this.version, this.plural, name, cancellationToken: cancel).ConfigureAwait(false);
75+
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
76+
}
77+
78+
public async Task<T> DeleteNamespacedAsync<T>(string ns, string name, CancellationToken cancel = default(CancellationToken))
79+
where T : IKubernetesObject
80+
{
81+
var resp = await this.kubernetes.DeleteNamespacedCustomObjectWithHttpMessagesAsync(this.group, this.version, ns, this.plural, name, cancellationToken: cancel).ConfigureAwait(false);
82+
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
83+
}
84+
85+
public void Dispose()
86+
{
87+
Dispose(true);
88+
GC.SuppressFinalize(this);
89+
}
90+
91+
protected virtual void Dispose(bool disposing)
92+
{
93+
this.kubernetes.Dispose();
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)