|
| 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