Skip to content

Commit 4ff1ea4

Browse files
authored
Ported GenericKubernetesApi from java along with other utilities (#682)
* Ported GenericKubernetesApi from java along with other utilities * Replace DeleteOptions for V1DeleteOptions * Clean up and add clear() * Clean up * Removed TweakApiHandler * Rename methods to follow "async" pattern * Fix method naming * Remove unneeded json property * Rearrange httpsuccess logic * Simplify dispose pattern * Treat MockKubeServerFlags as flags * Clean up flags logic * Remove unneeded json properties * Fix cs formatting * Remove unused variable * Move MockApi server options to seperate class and revert MockApi back to original * Remove IRunnable * Refactor config constants to use existing service account path
1 parent c23baaf commit 4ff1ea4

23 files changed

+1359
-108
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net5.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\KubernetesClient\KubernetesClient.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using k8s;
5+
using k8s.Models;
6+
using k8s.Util.Common;
7+
using k8s.Util.Common.Generic;
8+
9+
namespace GenericKubernetesApiExample
10+
{
11+
public class Program
12+
{
13+
private static GenericKubernetesApi _genericKubernetesApi;
14+
15+
public static void Main(string[] args)
16+
{
17+
var config = KubernetesClientConfiguration.BuildDefaultConfig();
18+
IKubernetes client = new Kubernetes(config);
19+
var cts = new CancellationTokenSource();
20+
21+
_genericKubernetesApi = new GenericKubernetesApi(
22+
apiGroup: "pod",
23+
apiVersion: "v1",
24+
resourcePlural: "pods",
25+
apiClient: client);
26+
27+
var aPod = GetNamespacedPod(Namespaces.NamespaceDefault, "my-pod-name", cts.Token);
28+
var aListOfPods = ListPodsInNamespace(Namespaces.NamespaceDefault, cts.Token);
29+
30+
// Watch for pod actions in a namespsace
31+
using var watch = _genericKubernetesApi.Watch<V1Pod>(
32+
Namespaces.NamespaceDefault,
33+
(eventType, pod) => { Console.WriteLine("The event {0} happened on pod named {1}", eventType, pod.Metadata.Name); },
34+
exception => { Console.WriteLine("Oh no! An exception happened while watching pods. The message was '{0}'.", exception.Message); },
35+
() => { Console.WriteLine("The server closed the connection."); });
36+
37+
Console.WriteLine("press ctrl + c to stop watching");
38+
39+
var ctrlc = new ManualResetEventSlim(false);
40+
Console.CancelKeyPress += (sender, eventArgs) => ctrlc.Set();
41+
ctrlc.Wait();
42+
cts.Cancel();
43+
}
44+
45+
private static V1Pod GetNamespacedPod(string @namespace, string podName, CancellationToken cancellationToken)
46+
{
47+
var resp = Task.Run(
48+
async () => await _genericKubernetesApi.GetAsync<V1Pod>(@namespace, podName, cancellationToken).ConfigureAwait(false), cancellationToken);
49+
50+
return resp.Result;
51+
}
52+
53+
private static V1PodList ListPodsInNamespace(string @namespace, CancellationToken cancellationToken)
54+
{
55+
var resp = Task.Run(
56+
async () => await _genericKubernetesApi.ListAsync<V1PodList>(@namespace, cancellationToken).ConfigureAwait(false), cancellationToken);
57+
58+
return resp.Result;
59+
}
60+
}
61+
}

kubernetes-client.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "customResource", "examples\
4343
EndProject
4444
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "KubernetesGenerator", "gen\KubernetesGenerator\KubernetesGenerator.csproj", "{79BA7C4A-98AA-467E-80D4-0E4F03EE6DDE}"
4545
EndProject
46+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenericKubernetesApi", "examples\GenericKubernetesApi\GenericKubernetesApi.csproj", "{F81AE4C4-E044-4225-BD76-385A0DE621FD}"
47+
EndProject
4648
Global
4749
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4850
Debug|Any CPU = Debug|Any CPU
@@ -245,6 +247,18 @@ Global
245247
{79BA7C4A-98AA-467E-80D4-0E4F03EE6DDE}.Release|x64.Build.0 = Release|Any CPU
246248
{79BA7C4A-98AA-467E-80D4-0E4F03EE6DDE}.Release|x86.ActiveCfg = Release|Any CPU
247249
{79BA7C4A-98AA-467E-80D4-0E4F03EE6DDE}.Release|x86.Build.0 = Release|Any CPU
250+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
251+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
252+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Debug|x64.ActiveCfg = Debug|Any CPU
253+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Debug|x64.Build.0 = Debug|Any CPU
254+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Debug|x86.ActiveCfg = Debug|Any CPU
255+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Debug|x86.Build.0 = Debug|Any CPU
256+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
257+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Release|Any CPU.Build.0 = Release|Any CPU
258+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Release|x64.ActiveCfg = Release|Any CPU
259+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Release|x64.Build.0 = Release|Any CPU
260+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Release|x86.ActiveCfg = Release|Any CPU
261+
{F81AE4C4-E044-4225-BD76-385A0DE621FD}.Release|x86.Build.0 = Release|Any CPU
248262
EndGlobalSection
249263
GlobalSection(SolutionProperties) = preSolution
250264
HideSolutionNode = FALSE
@@ -266,6 +280,7 @@ Global
266280
{4D2AE427-F856-49E5-B61D-EA6B17D89051} = {8AF4A5C2-F0CE-47D5-A4C5-FE4AB83CA509}
267281
{95672061-5799-4454-ACDB-D6D330DB1EC4} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
268282
{79BA7C4A-98AA-467E-80D4-0E4F03EE6DDE} = {879F8787-C3BB-43F3-A92D-6D4C7D3A5285}
283+
{F81AE4C4-E044-4225-BD76-385A0DE621FD} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
269284
EndGlobalSection
270285
GlobalSection(ExtensibilityGlobals) = postSolution
271286
SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace k8s.Util.Common
4+
{
5+
public class BadNotificationException : Exception
6+
{
7+
public BadNotificationException()
8+
{
9+
}
10+
11+
public BadNotificationException(string message)
12+
: base(message)
13+
{
14+
}
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace k8s.Util.Common
2+
{
3+
public static class Config
4+
{
5+
public static string ServiceAccountCaPath => KubernetesClientConfiguration.ServiceAccountPath + "/ca.crt";
6+
public static string ServiceAccountTokenPath => KubernetesClientConfiguration.ServiceAccountPath + "/token";
7+
public static string ServiceAccountNamespacePath => KubernetesClientConfiguration.ServiceAccountPath + "/namespace";
8+
public static string EnvKubeconfig => "KUBECONFIG";
9+
public static string EnvServiceHost => "KUBERNETES_SERVICE_HOST";
10+
public static string EnvServicePort => "KUBERNETES_SERVICE_PORT";
11+
12+
// The last resort host to try
13+
public static string DefaultFallbackHost => "http://localhost:8080";
14+
}
15+
}

0 commit comments

Comments
 (0)