2017-11-07 14:59:26 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using k8s;
|
|
|
|
|
using k8s.Models;
|
2017-11-03 22:01:53 -07:00
|
|
|
|
2017-11-07 14:59:26 +08:00
|
|
|
namespace @namespace
|
|
|
|
|
{
|
2017-11-03 22:01:53 -07:00
|
|
|
class NamespaceExample
|
|
|
|
|
{
|
|
|
|
|
static void ListNamespaces(IKubernetes client) {
|
2017-11-07 14:59:26 +08:00
|
|
|
var list = client.ListNamespace();
|
2017-11-03 22:01:53 -07:00
|
|
|
foreach (var item in list.Items) {
|
|
|
|
|
Console.WriteLine(item.Metadata.Name);
|
|
|
|
|
}
|
|
|
|
|
if (list.Items.Count == 0) {
|
|
|
|
|
Console.WriteLine("Empty!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 14:59:26 +08:00
|
|
|
static async Task DeleteAsync(IKubernetes client, string name, int delayMillis) {
|
2017-11-03 22:01:53 -07:00
|
|
|
while (true) {
|
|
|
|
|
await Task.Delay(delayMillis);
|
2017-11-07 14:59:26 +08:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await client.ReadNamespaceAsync(name);
|
2017-11-03 22:01:53 -07:00
|
|
|
} catch (AggregateException ex) {
|
|
|
|
|
foreach (var innerEx in ex.InnerExceptions) {
|
|
|
|
|
if (innerEx is Microsoft.Rest.HttpOperationException) {
|
|
|
|
|
var code = ((Microsoft.Rest.HttpOperationException)innerEx).Response.StatusCode;
|
|
|
|
|
if (code == HttpStatusCode.NotFound) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Microsoft.Rest.HttpOperationException ex) {
|
|
|
|
|
if (ex.Response.StatusCode == HttpStatusCode.NotFound) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-07 14:59:26 +08:00
|
|
|
static void Delete(IKubernetes client, string name, int delayMillis) {
|
|
|
|
|
DeleteAsync(client, name, delayMillis).Wait();
|
2017-11-03 22:01:53 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-07 14:59:26 +08:00
|
|
|
private static void Main(string[] args)
|
2017-11-03 22:01:53 -07:00
|
|
|
{
|
2017-11-07 14:59:26 +08:00
|
|
|
var k8SClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
|
|
|
IKubernetes client = new Kubernetes(k8SClientConfig);
|
2017-11-03 22:01:53 -07:00
|
|
|
|
|
|
|
|
ListNamespaces(client);
|
|
|
|
|
|
2017-11-24 12:19:44 +08:00
|
|
|
var ns = new V1Namespace
|
2017-11-07 14:59:26 +08:00
|
|
|
{
|
|
|
|
|
Metadata = new V1ObjectMeta
|
|
|
|
|
{
|
|
|
|
|
Name = "test"
|
|
|
|
|
}
|
|
|
|
|
};
|
2017-11-03 22:01:53 -07:00
|
|
|
|
2017-11-07 14:59:26 +08:00
|
|
|
var result = client.CreateNamespace(ns);
|
2017-11-03 22:01:53 -07:00
|
|
|
Console.WriteLine(result);
|
|
|
|
|
|
|
|
|
|
ListNamespaces(client);
|
|
|
|
|
|
2017-11-07 14:59:26 +08:00
|
|
|
var status = client.DeleteNamespace(new V1DeleteOptions(), ns.Metadata.Name);
|
|
|
|
|
|
|
|
|
|
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);
|
2017-11-03 22:01:53 -07:00
|
|
|
}
|
2017-11-07 14:59:26 +08:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(status.Message);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-03 22:01:53 -07:00
|
|
|
ListNamespaces(client);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|