Skip to content

Commit a4a5fc2

Browse files
Implement KubectlGet functionality with tests
Co-authored-by: brendandburns <[email protected]>
1 parent 29da827 commit a4a5fc2

File tree

3 files changed

+337
-0
lines changed

3 files changed

+337
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using k8s.Models;
2+
3+
namespace k8s.kubectl.beta;
4+
5+
public partial class AsyncKubectl
6+
{
7+
/// <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.
79+
/// </summary>
80+
/// <param name="name">The name of the secret.</param>
81+
/// <param name="namespace">The namespace of the secret. Defaults to "default".</param>
82+
/// <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)
85+
{
86+
return await client.CoreV1.ReadNamespacedSecretAsync(name, @namespace, cancellationToken: cancellationToken).ConfigureAwait(false);
87+
}
88+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using k8s.Models;
2+
3+
namespace k8s.kubectl.beta;
4+
5+
public partial class Kubectl
6+
{
7+
/// <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+
/// <returns>The pod.</returns>
13+
public V1Pod GetPod(string name, string @namespace = "default")
14+
{
15+
return client.GetPodAsync(name, @namespace).GetAwaiter().GetResult();
16+
}
17+
18+
/// <summary>
19+
/// Get a deployment by name in a namespace.
20+
/// </summary>
21+
/// <param name="name">The name of the deployment.</param>
22+
/// <param name="namespace">The namespace of the deployment. Defaults to "default".</param>
23+
/// <returns>The deployment.</returns>
24+
public V1Deployment GetDeployment(string name, string @namespace = "default")
25+
{
26+
return client.GetDeploymentAsync(name, @namespace).GetAwaiter().GetResult();
27+
}
28+
29+
/// <summary>
30+
/// Get a service by name in a namespace.
31+
/// </summary>
32+
/// <param name="name">The name of the service.</param>
33+
/// <param name="namespace">The namespace of the service. Defaults to "default".</param>
34+
/// <returns>The service.</returns>
35+
public V1Service GetService(string name, string @namespace = "default")
36+
{
37+
return client.GetServiceAsync(name, @namespace).GetAwaiter().GetResult();
38+
}
39+
40+
/// <summary>
41+
/// Get a namespace by name.
42+
/// </summary>
43+
/// <param name="name">The name of the namespace.</param>
44+
/// <returns>The namespace.</returns>
45+
public V1Namespace GetNamespace(string name)
46+
{
47+
return client.GetNamespaceAsync(name).GetAwaiter().GetResult();
48+
}
49+
50+
/// <summary>
51+
/// Get a node by name.
52+
/// </summary>
53+
/// <param name="name">The name of the node.</param>
54+
/// <returns>The node.</returns>
55+
public V1Node GetNode(string name)
56+
{
57+
return client.GetNodeAsync(name).GetAwaiter().GetResult();
58+
}
59+
60+
/// <summary>
61+
/// Get a config map by name in a namespace.
62+
/// </summary>
63+
/// <param name="name">The name of the config map.</param>
64+
/// <param name="namespace">The namespace of the config map. Defaults to "default".</param>
65+
/// <returns>The config map.</returns>
66+
public V1ConfigMap GetConfigMap(string name, string @namespace = "default")
67+
{
68+
return client.GetConfigMapAsync(name, @namespace).GetAwaiter().GetResult();
69+
}
70+
71+
/// <summary>
72+
/// Get a secret by name in a namespace.
73+
/// </summary>
74+
/// <param name="name">The name of the secret.</param>
75+
/// <param name="namespace">The namespace of the secret. Defaults to "default".</param>
76+
/// <returns>The secret.</returns>
77+
public V1Secret GetSecret(string name, string @namespace = "default")
78+
{
79+
return client.GetSecretAsync(name, @namespace).GetAwaiter().GetResult();
80+
}
81+
}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
using k8s.E2E;
2+
using k8s.kubectl.beta;
3+
using k8s.Models;
4+
using Xunit;
5+
6+
namespace k8s.kubectl.Tests;
7+
8+
public partial class KubectlTests
9+
{
10+
[MinikubeFact]
11+
public void GetNamespace()
12+
{
13+
using var kubernetes = MinikubeTests.CreateClient();
14+
var client = new Kubectl(kubernetes);
15+
16+
// Get the default namespace
17+
var ns = client.GetNamespace("default");
18+
19+
Assert.NotNull(ns);
20+
Assert.Equal("default", ns.Metadata.Name);
21+
Assert.Equal("v1", ns.ApiVersion);
22+
Assert.Equal("Namespace", ns.Kind);
23+
}
24+
25+
[MinikubeFact]
26+
public void GetPod()
27+
{
28+
using var kubernetes = MinikubeTests.CreateClient();
29+
var client = new Kubectl(kubernetes);
30+
var namespaceParameter = "default";
31+
var podName = "k8scsharp-e2e-get-pod";
32+
33+
// Cleanup any existing pod
34+
try
35+
{
36+
kubernetes.CoreV1.DeleteNamespacedPod(podName, namespaceParameter);
37+
System.Threading.Thread.Sleep(2000);
38+
}
39+
catch
40+
{
41+
// Ignore if pod doesn't exist
42+
}
43+
44+
// Create a test pod
45+
var pod = new V1Pod
46+
{
47+
Metadata = new V1ObjectMeta
48+
{
49+
Name = podName,
50+
NamespaceProperty = namespaceParameter,
51+
},
52+
Spec = new V1PodSpec
53+
{
54+
Containers = new[]
55+
{
56+
new V1Container
57+
{
58+
Name = "test",
59+
Image = "nginx:latest",
60+
},
61+
},
62+
},
63+
};
64+
65+
kubernetes.CoreV1.CreateNamespacedPod(pod, namespaceParameter);
66+
67+
try
68+
{
69+
// Wait a moment for the pod to be created
70+
System.Threading.Thread.Sleep(1000);
71+
72+
// Get the pod using kubectl
73+
var retrievedPod = client.GetPod(podName, namespaceParameter);
74+
75+
Assert.NotNull(retrievedPod);
76+
Assert.Equal(podName, retrievedPod.Metadata.Name);
77+
Assert.Equal(namespaceParameter, retrievedPod.Metadata.NamespaceProperty);
78+
Assert.Equal("Pod", retrievedPod.Kind);
79+
Assert.Equal("v1", retrievedPod.ApiVersion);
80+
}
81+
finally
82+
{
83+
// Cleanup
84+
try
85+
{
86+
kubernetes.CoreV1.DeleteNamespacedPod(podName, namespaceParameter);
87+
}
88+
catch
89+
{
90+
// Ignore cleanup errors
91+
}
92+
}
93+
}
94+
95+
[MinikubeFact]
96+
public void GetService()
97+
{
98+
using var kubernetes = MinikubeTests.CreateClient();
99+
var client = new Kubectl(kubernetes);
100+
var namespaceParameter = "default";
101+
var serviceName = "k8scsharp-e2e-get-service";
102+
103+
// Cleanup any existing service
104+
try
105+
{
106+
kubernetes.CoreV1.DeleteNamespacedService(serviceName, namespaceParameter);
107+
System.Threading.Thread.Sleep(1000);
108+
}
109+
catch
110+
{
111+
// Ignore if service doesn't exist
112+
}
113+
114+
// Create a test service
115+
var service = new V1Service
116+
{
117+
Metadata = new V1ObjectMeta
118+
{
119+
Name = serviceName,
120+
NamespaceProperty = namespaceParameter,
121+
},
122+
Spec = new V1ServiceSpec
123+
{
124+
Ports = new[]
125+
{
126+
new V1ServicePort
127+
{
128+
Port = 80,
129+
TargetPort = 80,
130+
},
131+
},
132+
Selector = new Dictionary<string, string>
133+
{
134+
{ "app", "test" },
135+
},
136+
},
137+
};
138+
139+
kubernetes.CoreV1.CreateNamespacedService(service, namespaceParameter);
140+
141+
try
142+
{
143+
// Wait a moment for the service to be created
144+
System.Threading.Thread.Sleep(1000);
145+
146+
// Get the service using kubectl
147+
var retrievedService = client.GetService(serviceName, namespaceParameter);
148+
149+
Assert.NotNull(retrievedService);
150+
Assert.Equal(serviceName, retrievedService.Metadata.Name);
151+
Assert.Equal(namespaceParameter, retrievedService.Metadata.NamespaceProperty);
152+
Assert.Equal("Service", retrievedService.Kind);
153+
Assert.Equal("v1", retrievedService.ApiVersion);
154+
}
155+
finally
156+
{
157+
// Cleanup
158+
try
159+
{
160+
kubernetes.CoreV1.DeleteNamespacedService(serviceName, namespaceParameter);
161+
}
162+
catch
163+
{
164+
// Ignore cleanup errors
165+
}
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)