Add an initial generic client. (#525)

This commit is contained in:
Brendan Burns
2020-11-23 02:59:34 -08:00
committed by GitHub
parent 5be3cff425
commit 3fd47e5644
3 changed files with 134 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
using System;
using System.Threading.Tasks;
using k8s;
using k8s.Models;
namespace exec
{
internal class Generic
{
private static async Task Main(string[] args)
{
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
var generic = new GenericClient(config, "", "v1", "nodes");
var node = await generic.ReadAsync<V1Node>("kube0").ConfigureAwait(false);
Console.WriteLine(node.Metadata.Name);
var genericPods = new GenericClient(config, "", "v1", "pods");
var pods = await genericPods.ListNamespacedAsync<V1PodList>("default").ConfigureAwait(false);
foreach (var pod in pods.Items)
{
Console.WriteLine(pod.Metadata.Name);
}
}
}
}

View File

@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\src\KubernetesClient\KubernetesClient.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,96 @@
using Microsoft.Rest.Serialization;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace k8s
{
public class GenericClient : IDisposable
{
internal class TweakApiHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage msg, CancellationToken cancel)
{
msg.RequestUri = new Uri(msg.RequestUri, msg.RequestUri.AbsolutePath.Replace("/apis//", "/api/"));
return base.SendAsync(msg, cancel);
}
}
private IKubernetes kubernetes;
private string group;
private string version;
private string plural;
public GenericClient(KubernetesClientConfiguration config, string group, string version, string plural)
{
this.group = group;
this.version = version;
this.plural = plural;
if (string.IsNullOrEmpty(group))
{
this.kubernetes = new Kubernetes(config, new DelegatingHandler[] { new TweakApiHandler() });
}
else
{
this.kubernetes = new Kubernetes(config);
}
}
public async Task<T> ListAsync<T>(CancellationToken cancel = default(CancellationToken))
where T : IKubernetesObject
{
var resp = await this.kubernetes.ListClusterCustomObjectWithHttpMessagesAsync(this.group, this.version, this.plural, cancellationToken: cancel).ConfigureAwait(false);
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
}
public async Task<T> ListNamespacedAsync<T>(string ns, CancellationToken cancel = default(CancellationToken))
where T : IKubernetesObject
{
var resp = await this.kubernetes.ListNamespacedCustomObjectWithHttpMessagesAsync(this.group, this.version, ns, this.plural, cancellationToken: cancel).ConfigureAwait(false);
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
}
public async Task<T> ReadNamespacedAsync<T>(string ns, string name, CancellationToken cancel = default(CancellationToken))
where T : IKubernetesObject
{
var resp = await this.kubernetes.GetNamespacedCustomObjectWithHttpMessagesAsync(this.group, this.version, ns, this.plural, name, cancellationToken: cancel).ConfigureAwait(false);
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
}
public async Task<T> ReadAsync<T>(string name, CancellationToken cancel = default(CancellationToken))
where T : IKubernetesObject
{
var resp = await this.kubernetes.GetClusterCustomObjectWithHttpMessagesAsync(this.group, this.version, this.plural, name, cancellationToken: cancel).ConfigureAwait(false);
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
}
public async Task<T> DeleteAsync<T>(string name, CancellationToken cancel = default(CancellationToken))
where T : IKubernetesObject
{
var resp = await this.kubernetes.DeleteClusterCustomObjectWithHttpMessagesAsync(this.group, this.version, this.plural, name, cancellationToken: cancel).ConfigureAwait(false);
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
}
public async Task<T> DeleteNamespacedAsync<T>(string ns, string name, CancellationToken cancel = default(CancellationToken))
where T : IKubernetesObject
{
var resp = await this.kubernetes.DeleteNamespacedCustomObjectWithHttpMessagesAsync(this.group, this.version, ns, this.plural, name, cancellationToken: cancel).ConfigureAwait(false);
return SafeJsonConvert.DeserializeObject<T>(resp.Body.ToString());
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
this.kubernetes.Dispose();
}
}
}