Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/generic/Generic.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
13 changes: 13 additions & 0 deletions examples/generic/generic.csproj
Original file line number Diff line number Diff line change
@@ -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>
96 changes: 96 additions & 0 deletions src/KubernetesClient/GenericClient.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}