-
Notifications
You must be signed in to change notification settings - Fork 308
Add an initial generic client. #525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.