|
1 | | -using k8s.Models; |
2 | | - |
3 | 1 | namespace k8s.kubectl.beta; |
4 | 2 |
|
5 | 3 | public partial class AsyncKubectl |
6 | 4 | { |
7 | 5 | /// <summary> |
8 | | - /// Get a pod by name in a namespace. |
9 | | - /// </summary> |
10 | | - /// <param name="name">The name of the pod.</param> |
11 | | - /// <param name="namespace">The namespace of the pod. Defaults to "default".</param> |
12 | | - /// <param name="cancellationToken">Cancellation token.</param> |
13 | | - /// <returns>The pod.</returns> |
14 | | - public async Task<V1Pod> GetPodAsync(string name, string @namespace = "default", CancellationToken cancellationToken = default) |
15 | | - { |
16 | | - return await client.CoreV1.ReadNamespacedPodAsync(name, @namespace, cancellationToken: cancellationToken).ConfigureAwait(false); |
17 | | - } |
18 | | - |
19 | | - /// <summary> |
20 | | - /// Get a deployment by name in a namespace. |
21 | | - /// </summary> |
22 | | - /// <param name="name">The name of the deployment.</param> |
23 | | - /// <param name="namespace">The namespace of the deployment. Defaults to "default".</param> |
24 | | - /// <param name="cancellationToken">Cancellation token.</param> |
25 | | - /// <returns>The deployment.</returns> |
26 | | - public async Task<V1Deployment> GetDeploymentAsync(string name, string @namespace = "default", CancellationToken cancellationToken = default) |
27 | | - { |
28 | | - return await client.AppsV1.ReadNamespacedDeploymentAsync(name, @namespace, cancellationToken: cancellationToken).ConfigureAwait(false); |
29 | | - } |
30 | | - |
31 | | - /// <summary> |
32 | | - /// Get a service by name in a namespace. |
33 | | - /// </summary> |
34 | | - /// <param name="name">The name of the service.</param> |
35 | | - /// <param name="namespace">The namespace of the service. Defaults to "default".</param> |
36 | | - /// <param name="cancellationToken">Cancellation token.</param> |
37 | | - /// <returns>The service.</returns> |
38 | | - public async Task<V1Service> GetServiceAsync(string name, string @namespace = "default", CancellationToken cancellationToken = default) |
39 | | - { |
40 | | - return await client.CoreV1.ReadNamespacedServiceAsync(name, @namespace, cancellationToken: cancellationToken).ConfigureAwait(false); |
41 | | - } |
42 | | - |
43 | | - /// <summary> |
44 | | - /// Get a namespace by name. |
45 | | - /// </summary> |
46 | | - /// <param name="name">The name of the namespace.</param> |
47 | | - /// <param name="cancellationToken">Cancellation token.</param> |
48 | | - /// <returns>The namespace.</returns> |
49 | | - public async Task<V1Namespace> GetNamespaceAsync(string name, CancellationToken cancellationToken = default) |
50 | | - { |
51 | | - return await client.CoreV1.ReadNamespaceAsync(name, cancellationToken: cancellationToken).ConfigureAwait(false); |
52 | | - } |
53 | | - |
54 | | - /// <summary> |
55 | | - /// Get a node by name. |
56 | | - /// </summary> |
57 | | - /// <param name="name">The name of the node.</param> |
58 | | - /// <param name="cancellationToken">Cancellation token.</param> |
59 | | - /// <returns>The node.</returns> |
60 | | - public async Task<V1Node> GetNodeAsync(string name, CancellationToken cancellationToken = default) |
61 | | - { |
62 | | - return await client.CoreV1.ReadNodeAsync(name, cancellationToken: cancellationToken).ConfigureAwait(false); |
63 | | - } |
64 | | - |
65 | | - /// <summary> |
66 | | - /// Get a config map by name in a namespace. |
67 | | - /// </summary> |
68 | | - /// <param name="name">The name of the config map.</param> |
69 | | - /// <param name="namespace">The namespace of the config map. Defaults to "default".</param> |
70 | | - /// <param name="cancellationToken">Cancellation token.</param> |
71 | | - /// <returns>The config map.</returns> |
72 | | - public async Task<V1ConfigMap> GetConfigMapAsync(string name, string @namespace = "default", CancellationToken cancellationToken = default) |
73 | | - { |
74 | | - return await client.CoreV1.ReadNamespacedConfigMapAsync(name, @namespace, cancellationToken: cancellationToken).ConfigureAwait(false); |
75 | | - } |
76 | | - |
77 | | - /// <summary> |
78 | | - /// Get a secret by name in a namespace. |
| 6 | + /// Get a Kubernetes resource by name. |
79 | 7 | /// </summary> |
80 | | - /// <param name="name">The name of the secret.</param> |
81 | | - /// <param name="namespace">The namespace of the secret. Defaults to "default".</param> |
| 8 | + /// <typeparam name="T">The type of Kubernetes resource to get.</typeparam> |
| 9 | + /// <param name="name">The name of the resource.</param> |
| 10 | + /// <param name="namespace">The namespace of the resource (for namespaced resources). Optional.</param> |
82 | 11 | /// <param name="cancellationToken">Cancellation token.</param> |
83 | | - /// <returns>The secret.</returns> |
84 | | - public async Task<V1Secret> GetSecretAsync(string name, string @namespace = "default", CancellationToken cancellationToken = default) |
| 12 | + /// <returns>The requested resource.</returns> |
| 13 | + public async Task<T> GetAsync<T>(string name, string? @namespace = null, CancellationToken cancellationToken = default) |
| 14 | + where T : IKubernetesObject |
85 | 15 | { |
86 | | - return await client.CoreV1.ReadNamespacedSecretAsync(name, @namespace, cancellationToken: cancellationToken).ConfigureAwait(false); |
| 16 | + var metadata = typeof(T).GetKubernetesTypeMetadata(); |
| 17 | + var genericClient = new GenericClient(client, metadata.Group, metadata.ApiVersion, metadata.PluralName, disposeClient: false); |
| 18 | + |
| 19 | + if (@namespace != null) |
| 20 | + { |
| 21 | + return await genericClient.ReadNamespacedAsync<T>(@namespace, name, cancellationToken).ConfigureAwait(false); |
| 22 | + } |
| 23 | + else |
| 24 | + { |
| 25 | + return await genericClient.ReadAsync<T>(name, cancellationToken).ConfigureAwait(false); |
| 26 | + } |
87 | 27 | } |
88 | 28 | } |
0 commit comments