2018-09-27 10:50:39 -07:00
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using k8s;
|
|
|
|
|
using k8s.Models;
|
|
|
|
|
|
|
|
|
|
namespace @namespace
|
|
|
|
|
{
|
2020-10-23 08:31:57 -07:00
|
|
|
internal class NamespaceExample
|
2018-09-27 10:50:39 -07:00
|
|
|
{
|
2020-10-23 08:31:57 -07:00
|
|
|
private static void ListNamespaces(IKubernetes client)
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
2018-09-27 10:50:39 -07:00
|
|
|
var list = client.ListNamespace();
|
2020-04-22 12:15:45 -07:00
|
|
|
foreach (var item in list.Items)
|
|
|
|
|
{
|
2018-09-27 10:50:39 -07:00
|
|
|
Console.WriteLine(item.Metadata.Name);
|
|
|
|
|
}
|
2020-04-23 11:40:06 -07:00
|
|
|
|
2020-04-22 12:15:45 -07:00
|
|
|
if (list.Items.Count == 0)
|
|
|
|
|
{
|
2018-09-27 10:50:39 -07:00
|
|
|
Console.WriteLine("Empty!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 08:31:57 -07:00
|
|
|
private static async Task DeleteAsync(IKubernetes client, string name, int delayMillis)
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
2020-10-23 08:31:57 -07:00
|
|
|
await Task.Delay(delayMillis).ConfigureAwait(false);
|
2018-09-27 10:50:39 -07:00
|
|
|
try
|
|
|
|
|
{
|
2020-10-23 08:31:57 -07:00
|
|
|
await client.ReadNamespaceAsync(name).ConfigureAwait(false);
|
2020-04-22 12:15:45 -07:00
|
|
|
}
|
|
|
|
|
catch (AggregateException ex)
|
|
|
|
|
{
|
|
|
|
|
foreach (var innerEx in ex.InnerExceptions)
|
|
|
|
|
{
|
2022-02-25 13:33:23 -08:00
|
|
|
if (innerEx is k8s.Autorest.HttpOperationException)
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
2022-02-25 13:33:23 -08:00
|
|
|
var code = ((k8s.Autorest.HttpOperationException)innerEx).Response.StatusCode;
|
2020-04-22 12:15:45 -07:00
|
|
|
if (code == HttpStatusCode.NotFound)
|
|
|
|
|
{
|
2018-09-27 10:50:39 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2020-04-23 11:40:06 -07:00
|
|
|
|
2020-10-23 08:31:57 -07:00
|
|
|
throw;
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
2020-04-22 12:15:45 -07:00
|
|
|
}
|
2022-02-25 13:33:23 -08:00
|
|
|
catch (k8s.Autorest.HttpOperationException ex)
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
|
|
|
|
if (ex.Response.StatusCode == HttpStatusCode.NotFound)
|
|
|
|
|
{
|
2018-09-27 10:50:39 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2020-04-23 11:40:06 -07:00
|
|
|
|
2020-10-23 08:31:57 -07:00
|
|
|
throw;
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-23 08:31:57 -07:00
|
|
|
private static void Delete(IKubernetes client, string name, int delayMillis)
|
2020-04-22 12:15:45 -07:00
|
|
|
{
|
2018-09-27 10:50:39 -07:00
|
|
|
DeleteAsync(client, name, delayMillis).Wait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
var k8SClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
|
|
|
IKubernetes client = new Kubernetes(k8SClientConfig);
|
|
|
|
|
|
|
|
|
|
ListNamespaces(client);
|
|
|
|
|
|
2020-04-23 11:40:06 -07:00
|
|
|
var ns = new V1Namespace { Metadata = new V1ObjectMeta { Name = "test" } };
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
var result = client.CreateNamespace(ns);
|
|
|
|
|
Console.WriteLine(result);
|
|
|
|
|
|
|
|
|
|
ListNamespaces(client);
|
|
|
|
|
|
2018-12-11 21:10:39 -08:00
|
|
|
var status = client.DeleteNamespace(ns.Metadata.Name, new V1DeleteOptions());
|
2018-09-27 10:50:39 -07:00
|
|
|
|
|
|
|
|
if (status.HasObject)
|
|
|
|
|
{
|
2017-11-24 12:19:44 +08:00
|
|
|
var obj = status.ObjectView<V1Namespace>();
|
2017-11-07 14:59:26 +08:00
|
|
|
Console.WriteLine(obj.Status.Phase);
|
|
|
|
|
|
|
|
|
|
Delete(client, ns.Metadata.Name, 3 * 1000);
|
2018-09-27 10:50:39 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(status.Message);
|
2017-11-07 14:59:26 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-27 10:50:39 -07:00
|
|
|
ListNamespaces(client);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|