update namespace example to obj view and formating
This commit is contained in:
@@ -1,17 +1,15 @@
|
|||||||
namespace simple
|
|
||||||
{
|
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using k8s;
|
using k8s;
|
||||||
using k8s.Models;
|
using k8s.Models;
|
||||||
|
|
||||||
|
namespace @namespace
|
||||||
|
{
|
||||||
class NamespaceExample
|
class NamespaceExample
|
||||||
{
|
{
|
||||||
static void ListNamespaces(IKubernetes client) {
|
static void ListNamespaces(IKubernetes client) {
|
||||||
var listTask = client.ListNamespaceWithHttpMessagesAsync().Result;
|
var list = client.ListNamespace();
|
||||||
var list = listTask.Body;
|
|
||||||
foreach (var item in list.Items) {
|
foreach (var item in list.Items) {
|
||||||
Console.WriteLine(item.Metadata.Name);
|
Console.WriteLine(item.Metadata.Name);
|
||||||
}
|
}
|
||||||
@@ -20,13 +18,12 @@ namespace simple
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async Task asyncAwaitDelete(IKubernetes client, string name, int delayMillis) {
|
static async Task DeleteAsync(IKubernetes client, string name, int delayMillis) {
|
||||||
while (true) {
|
while (true) {
|
||||||
await Task.Delay(delayMillis);
|
await Task.Delay(delayMillis);
|
||||||
try {
|
try
|
||||||
var result = await client.ReadNamespaceWithHttpMessagesAsync(name);
|
{
|
||||||
var ns = result.Body;
|
await client.ReadNamespaceAsync(name);
|
||||||
Console.WriteLine(ns);
|
|
||||||
} catch (AggregateException ex) {
|
} catch (AggregateException ex) {
|
||||||
foreach (var innerEx in ex.InnerExceptions) {
|
foreach (var innerEx in ex.InnerExceptions) {
|
||||||
if (innerEx is Microsoft.Rest.HttpOperationException) {
|
if (innerEx is Microsoft.Rest.HttpOperationException) {
|
||||||
@@ -46,34 +43,44 @@ namespace simple
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void awaitDelete(IKubernetes client, string name, int delayMillis) {
|
static void Delete(IKubernetes client, string name, int delayMillis) {
|
||||||
asyncAwaitDelete(client, name, delayMillis).Wait();
|
DeleteAsync(client, name, delayMillis).Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
{
|
{
|
||||||
var k8sClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
var k8SClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
IKubernetes client = new Kubernetes(k8sClientConfig);
|
IKubernetes client = new Kubernetes(k8SClientConfig);
|
||||||
|
|
||||||
ListNamespaces(client);
|
ListNamespaces(client);
|
||||||
|
|
||||||
var ns = new Corev1Namespace();
|
var ns = new Corev1Namespace
|
||||||
ns.Metadata = new V1ObjectMeta();
|
{
|
||||||
ns.Metadata.Name = "test";
|
Metadata = new V1ObjectMeta
|
||||||
|
{
|
||||||
|
Name = "test"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
var result = client.CreateNamespaceWithHttpMessagesAsync(ns).Result;
|
var result = client.CreateNamespace(ns);
|
||||||
Console.WriteLine(result);
|
Console.WriteLine(result);
|
||||||
|
|
||||||
ListNamespaces(client);
|
ListNamespaces(client);
|
||||||
|
|
||||||
var task = client.DeleteNamespaceWithHttpMessagesAsync(new V1DeleteOptions(), ns.Metadata.Name);
|
var status = client.DeleteNamespace(new V1DeleteOptions(), ns.Metadata.Name);
|
||||||
var obj = ObjectOrStatus<Corev1Namespace>.ReadObjectOrStatus(task);
|
|
||||||
|
|
||||||
if (obj.Status != null) {
|
if (status.HasObject)
|
||||||
Console.WriteLine(obj.Status);
|
{
|
||||||
} else {
|
var obj = status.ObjectView<Corev1Namespace>();
|
||||||
awaitDelete(client, ns.Metadata.Name, 3 * 1000);
|
Console.WriteLine(obj.Status.Phase);
|
||||||
|
|
||||||
|
Delete(client, ns.Metadata.Name, 3 * 1000);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine(status.Message);
|
||||||
|
}
|
||||||
|
|
||||||
ListNamespaces(client);
|
ListNamespaces(client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user