2022-03-07 10:06:55 -08:00
|
|
|
using Json.Patch;
|
2020-12-19 11:26:25 -05:00
|
|
|
using k8s;
|
2022-02-25 13:33:23 -08:00
|
|
|
using k8s.Autorest;
|
2020-12-19 11:26:25 -05:00
|
|
|
using k8s.Models;
|
2021-10-14 18:02:51 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-03-07 10:06:55 -08:00
|
|
|
using System.Text.Json;
|
2021-10-14 18:02:51 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-12-19 11:26:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace customResource
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
|
|
|
|
private static async Task Main(string[] args)
|
|
|
|
|
{
|
2021-10-14 18:02:51 +02:00
|
|
|
Console.WriteLine("starting main()...");
|
2020-12-19 11:26:25 -05:00
|
|
|
|
|
|
|
|
// creating the k8s client
|
|
|
|
|
var k8SClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
|
|
|
IKubernetes client = new Kubernetes(k8SClientConfig);
|
|
|
|
|
|
|
|
|
|
// creating a K8s client for the CRD
|
|
|
|
|
var myCRD = Utils.MakeCRD();
|
|
|
|
|
Console.WriteLine("working with CRD: {0}.{1}", myCRD.PluralName, myCRD.Group);
|
2021-10-14 18:02:51 +02:00
|
|
|
var generic = new GenericClient(client, myCRD.Group, myCRD.Version, myCRD.PluralName);
|
2020-12-19 11:26:25 -05:00
|
|
|
|
|
|
|
|
// creating a sample custom resource content
|
|
|
|
|
var myCr = Utils.MakeCResource();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("creating CR {0}", myCr.Metadata.Name);
|
|
|
|
|
var response = await client.CreateNamespacedCustomObjectWithHttpMessagesAsync(
|
|
|
|
|
myCr,
|
|
|
|
|
myCRD.Group, myCRD.Version,
|
|
|
|
|
myCr.Metadata.NamespaceProperty ?? "default",
|
|
|
|
|
myCRD.PluralName).ConfigureAwait(false);
|
|
|
|
|
}
|
2022-02-25 13:33:23 -08:00
|
|
|
catch (HttpOperationException httpOperationException) when (httpOperationException.Message.Contains("422"))
|
2020-12-19 11:26:25 -05:00
|
|
|
{
|
|
|
|
|
var phase = httpOperationException.Response.ReasonPhrase;
|
|
|
|
|
var content = httpOperationException.Response.Content;
|
|
|
|
|
Console.WriteLine("response content: {0}", content);
|
|
|
|
|
Console.WriteLine("response phase: {0}", phase);
|
|
|
|
|
}
|
2022-02-25 13:33:23 -08:00
|
|
|
catch (HttpOperationException)
|
2020-12-19 11:26:25 -05:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// listing the cr instances
|
|
|
|
|
Console.WriteLine("CR list:");
|
|
|
|
|
var crs = await generic.ListNamespacedAsync<CustomResourceList<CResource>>(myCr.Metadata.NamespaceProperty ?? "default").ConfigureAwait(false);
|
|
|
|
|
foreach (var cr in crs.Items)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("- CR Item {0} = {1}", crs.Items.IndexOf(cr), cr.Metadata.Name);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 10:06:55 -08:00
|
|
|
var old = JsonSerializer.SerializeToDocument(myCr);
|
2020-12-19 11:26:25 -05:00
|
|
|
myCr.Metadata.Labels.TryAdd("newKey", "newValue");
|
2022-03-07 10:06:55 -08:00
|
|
|
|
|
|
|
|
var expected = JsonSerializer.SerializeToDocument(myCr);
|
|
|
|
|
var patch = old.CreatePatch(expected);
|
|
|
|
|
|
|
|
|
|
// updating the custom resource
|
2020-12-19 11:26:25 -05:00
|
|
|
var crPatch = new V1Patch(patch, V1Patch.PatchType.JsonPatch);
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var patchResponse = await client.PatchNamespacedCustomObjectAsync(
|
|
|
|
|
crPatch,
|
|
|
|
|
myCRD.Group,
|
|
|
|
|
myCRD.Version,
|
|
|
|
|
myCr.Metadata.NamespaceProperty ?? "default",
|
|
|
|
|
myCRD.PluralName,
|
|
|
|
|
myCr.Metadata.Name).ConfigureAwait(false);
|
|
|
|
|
}
|
2022-02-25 13:33:23 -08:00
|
|
|
catch (HttpOperationException httpOperationException)
|
2020-12-19 11:26:25 -05:00
|
|
|
{
|
|
|
|
|
var phase = httpOperationException.Response.ReasonPhrase;
|
|
|
|
|
var content = httpOperationException.Response.Content;
|
|
|
|
|
Console.WriteLine("response content: {0}", content);
|
|
|
|
|
Console.WriteLine("response phase: {0}", phase);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getting the updated custom resource
|
|
|
|
|
var fetchedCR = await generic.ReadNamespacedAsync<CResource>(
|
|
|
|
|
myCr.Metadata.NamespaceProperty ?? "default",
|
|
|
|
|
myCr.Metadata.Name).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("fetchedCR = {0}", fetchedCR.ToString());
|
|
|
|
|
|
|
|
|
|
// deleting the custom resource
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
myCr = await generic.DeleteNamespacedAsync<CResource>(
|
|
|
|
|
myCr.Metadata.NamespaceProperty ?? "default",
|
|
|
|
|
myCr.Metadata.Name).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("Deleted the CR");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Exception type {0}", exception);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|