Skip to content

Commit 6d2900f

Browse files
committed
Add an initial generic client.
1 parent 5be3cff commit 6d2900f

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

examples/generic/Generic.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
Console.WriteLine(await generic.ReadAsync("kube0").ConfigureAwait(false));
15+
16+
var genericPods = new GenericClient(config, "", "v1", "pods");
17+
Console.WriteLine(await genericPods.ListNamespacedAsync("default").ConfigureAwait(false));
18+
}
19+
}
20+
}

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

0 commit comments

Comments
 (0)