diff --git a/examples/restart/Program.cs b/examples/restart/Program.cs new file mode 100644 index 0000000..0ba1e7f --- /dev/null +++ b/examples/restart/Program.cs @@ -0,0 +1,76 @@ +using System.Globalization; +using System.Text.Json; +using Json.Patch; +using k8s; +using k8s.Models; + +double ConvertToUnixTimestamp(DateTime date) +{ + var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); + var diff = date.ToUniversalTime() - origin; + return Math.Floor(diff.TotalSeconds); +} + +async Task RestartDaemonSetAsync(string name, string @namespace, IKubernetes client) +{ + var daemonSet = await client.ReadNamespacedDaemonSetAsync(name, @namespace); + var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }; + var old = JsonSerializer.SerializeToDocument(daemonSet, options); + + var restart = new Dictionary(daemonSet.Spec.Template.Metadata.Annotations) + { + ["date"] = ConvertToUnixTimestamp(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture) + }; + + daemonSet.Spec.Template.Metadata.Annotations = restart; + + var expected = JsonSerializer.SerializeToDocument(daemonSet); + + var patch = old.CreatePatch(expected); + await client.PatchNamespacedDaemonSetAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), name, @namespace); +} + +async Task RestartDeploymentAsync(string name, string @namespace, IKubernetes client) +{ + var deployment = await client.ReadNamespacedDeploymentAsync(name, @namespace); + var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }; + var old = JsonSerializer.SerializeToDocument(deployment, options); + + var restart = new Dictionary(deployment.Spec.Template.Metadata.Annotations) + { + ["date"] = ConvertToUnixTimestamp(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture) + }; + + deployment.Spec.Template.Metadata.Annotations = restart; + + var expected = JsonSerializer.SerializeToDocument(deployment); + + var patch = old.CreatePatch(expected); + await client.PatchNamespacedDeploymentAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), name, @namespace); +} + +async Task RestartStatefulSetAsync(string name, string @namespace, IKubernetes client) +{ + var deployment = await client.ReadNamespacedStatefulSetAsync(name, @namespace); + var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }; + var old = JsonSerializer.SerializeToDocument(deployment, options); + + var restart = new Dictionary(deployment.Spec.Template.Metadata.Annotations) + { + ["date"] = ConvertToUnixTimestamp(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture) + }; + + deployment.Spec.Template.Metadata.Annotations = restart; + + var expected = JsonSerializer.SerializeToDocument(deployment); + + var patch = old.CreatePatch(expected); + await client.PatchNamespacedStatefulSetAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), name, @namespace); +} + +var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); +IKubernetes client = new Kubernetes(config); + +await RestartDeploymentAsync("event-exporter", "monitoring", client); +await RestartDaemonSetAsync("prometheus-exporter", "monitoring", client); +await RestartStatefulSetAsync("argocd-application-controlle", "argocd", client); diff --git a/examples/restart/restart.csproj b/examples/restart/restart.csproj new file mode 100644 index 0000000..4f4d519 --- /dev/null +++ b/examples/restart/restart.csproj @@ -0,0 +1,13 @@ + + + + Exe + enable + enable + + + + + + + diff --git a/kubernetes-client.sln b/kubernetes-client.sln index 98edcf2..825fa11 100644 --- a/kubernetes-client.sln +++ b/kubernetes-client.sln @@ -59,6 +59,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubernetesClient.Classic.Te EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csrApproval", "examples\csrApproval\csrApproval.csproj", "{F626860C-F141-45B3-9DDD-88AD3932ACAF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "restart", "examples\restart\restart.csproj", "{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -369,6 +371,18 @@ Global {F626860C-F141-45B3-9DDD-88AD3932ACAF}.Release|x64.Build.0 = Release|Any CPU {F626860C-F141-45B3-9DDD-88AD3932ACAF}.Release|x86.ActiveCfg = Release|Any CPU {F626860C-F141-45B3-9DDD-88AD3932ACAF}.Release|x86.Build.0 = Release|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|Any CPU.Build.0 = Debug|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|x64.ActiveCfg = Debug|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|x64.Build.0 = Debug|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|x86.ActiveCfg = Debug|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|x86.Build.0 = Debug|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|Any CPU.ActiveCfg = Release|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|Any CPU.Build.0 = Release|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|x64.ActiveCfg = Release|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|x64.Build.0 = Release|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|x86.ActiveCfg = Release|Any CPU + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -399,6 +413,7 @@ Global {80F19E8A-F097-4AA4-A68C-D417B96BBC68} = {3D1864AA-1FFC-4512-BB13-46055E410F73} {FD90C861-56C6-4536-B7F5-AC7779296384} = {8AF4A5C2-F0CE-47D5-A4C5-FE4AB83CA509} {F626860C-F141-45B3-9DDD-88AD3932ACAF} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40} + {973CCB4A-F344-4C4F-81A5-0F40F7F43C07} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}