* 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
62 lines
2.4 KiB
C#
62 lines
2.4 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using k8s;
|
|
using k8s.Models;
|
|
using k8s.Util.Common;
|
|
using k8s.Util.Common.Generic;
|
|
|
|
namespace GenericKubernetesApiExample
|
|
{
|
|
public class Program
|
|
{
|
|
private static GenericKubernetesApi _genericKubernetesApi;
|
|
|
|
public static void Main(string[] args)
|
|
{
|
|
var config = KubernetesClientConfiguration.BuildDefaultConfig();
|
|
IKubernetes client = new Kubernetes(config);
|
|
var cts = new CancellationTokenSource();
|
|
|
|
_genericKubernetesApi = new GenericKubernetesApi(
|
|
apiGroup: "pod",
|
|
apiVersion: "v1",
|
|
resourcePlural: "pods",
|
|
apiClient: client);
|
|
|
|
var aPod = GetNamespacedPod(Namespaces.NamespaceDefault, "my-pod-name", cts.Token);
|
|
var aListOfPods = ListPodsInNamespace(Namespaces.NamespaceDefault, cts.Token);
|
|
|
|
// Watch for pod actions in a namespsace
|
|
using var watch = _genericKubernetesApi.Watch<V1Pod>(
|
|
Namespaces.NamespaceDefault,
|
|
(eventType, pod) => { Console.WriteLine("The event {0} happened on pod named {1}", eventType, pod.Metadata.Name); },
|
|
exception => { Console.WriteLine("Oh no! An exception happened while watching pods. The message was '{0}'.", exception.Message); },
|
|
() => { Console.WriteLine("The server closed the connection."); });
|
|
|
|
Console.WriteLine("press ctrl + c to stop watching");
|
|
|
|
var ctrlc = new ManualResetEventSlim(false);
|
|
Console.CancelKeyPress += (sender, eventArgs) => ctrlc.Set();
|
|
ctrlc.Wait();
|
|
cts.Cancel();
|
|
}
|
|
|
|
private static V1Pod GetNamespacedPod(string @namespace, string podName, CancellationToken cancellationToken)
|
|
{
|
|
var resp = Task.Run(
|
|
async () => await _genericKubernetesApi.GetAsync<V1Pod>(@namespace, podName, cancellationToken).ConfigureAwait(false), cancellationToken);
|
|
|
|
return resp.Result;
|
|
}
|
|
|
|
private static V1PodList ListPodsInNamespace(string @namespace, CancellationToken cancellationToken)
|
|
{
|
|
var resp = Task.Run(
|
|
async () => await _genericKubernetesApi.ListAsync<V1PodList>(@namespace, cancellationToken).ConfigureAwait(false), cancellationToken);
|
|
|
|
return resp.Result;
|
|
}
|
|
}
|
|
}
|