Skip to content

Commit 6ff5460

Browse files
author
Arzu Suleymanov
authored
Restart sts daemonset deployment example (#819)
* chore : csr approval example using json patch net package * chore : use top level feature, remove k8s client and use reference instead * chore : remove target framework * fix : remove redundant reference package * feat : implement restart sts, daemonset & deployment option using patch * refactor : optimize code and resolve warnings
1 parent 541a2f5 commit 6ff5460

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

examples/restart/Program.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using System.Globalization;
2+
using System.Text.Json;
3+
using Json.Patch;
4+
using k8s;
5+
using k8s.Models;
6+
7+
double ConvertToUnixTimestamp(DateTime date)
8+
{
9+
var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
10+
var diff = date.ToUniversalTime() - origin;
11+
return Math.Floor(diff.TotalSeconds);
12+
}
13+
14+
async Task RestartDaemonSetAsync(string name, string @namespace, IKubernetes client)
15+
{
16+
var daemonSet = await client.ReadNamespacedDaemonSetAsync(name, @namespace);
17+
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true };
18+
var old = JsonSerializer.SerializeToDocument(daemonSet, options);
19+
20+
var restart = new Dictionary<string, string>(daemonSet.Spec.Template.Metadata.Annotations)
21+
{
22+
["date"] = ConvertToUnixTimestamp(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture)
23+
};
24+
25+
daemonSet.Spec.Template.Metadata.Annotations = restart;
26+
27+
var expected = JsonSerializer.SerializeToDocument(daemonSet);
28+
29+
var patch = old.CreatePatch(expected);
30+
await client.PatchNamespacedDaemonSetAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), name, @namespace);
31+
}
32+
33+
async Task RestartDeploymentAsync(string name, string @namespace, IKubernetes client)
34+
{
35+
var deployment = await client.ReadNamespacedDeploymentAsync(name, @namespace);
36+
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true };
37+
var old = JsonSerializer.SerializeToDocument(deployment, options);
38+
39+
var restart = new Dictionary<string, string>(deployment.Spec.Template.Metadata.Annotations)
40+
{
41+
["date"] = ConvertToUnixTimestamp(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture)
42+
};
43+
44+
deployment.Spec.Template.Metadata.Annotations = restart;
45+
46+
var expected = JsonSerializer.SerializeToDocument(deployment);
47+
48+
var patch = old.CreatePatch(expected);
49+
await client.PatchNamespacedDeploymentAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), name, @namespace);
50+
}
51+
52+
async Task RestartStatefulSetAsync(string name, string @namespace, IKubernetes client)
53+
{
54+
var deployment = await client.ReadNamespacedStatefulSetAsync(name, @namespace);
55+
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true };
56+
var old = JsonSerializer.SerializeToDocument(deployment, options);
57+
58+
var restart = new Dictionary<string, string>(deployment.Spec.Template.Metadata.Annotations)
59+
{
60+
["date"] = ConvertToUnixTimestamp(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture)
61+
};
62+
63+
deployment.Spec.Template.Metadata.Annotations = restart;
64+
65+
var expected = JsonSerializer.SerializeToDocument(deployment);
66+
67+
var patch = old.CreatePatch(expected);
68+
await client.PatchNamespacedStatefulSetAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), name, @namespace);
69+
}
70+
71+
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
72+
IKubernetes client = new Kubernetes(config);
73+
74+
await RestartDeploymentAsync("event-exporter", "monitoring", client);
75+
await RestartDaemonSetAsync("prometheus-exporter", "monitoring", client);
76+
await RestartStatefulSetAsync("argocd-application-controlle", "argocd", client);

examples/restart/restart.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="JsonPatch.Net" Version="1.1.2" />
11+
</ItemGroup>
12+
13+
</Project>

kubernetes-client.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KubernetesClient.Classic.Te
5959
EndProject
6060
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csrApproval", "examples\csrApproval\csrApproval.csproj", "{F626860C-F141-45B3-9DDD-88AD3932ACAF}"
6161
EndProject
62+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "restart", "examples\restart\restart.csproj", "{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}"
63+
EndProject
6264
Global
6365
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6466
Debug|Any CPU = Debug|Any CPU
@@ -369,6 +371,18 @@ Global
369371
{F626860C-F141-45B3-9DDD-88AD3932ACAF}.Release|x64.Build.0 = Release|Any CPU
370372
{F626860C-F141-45B3-9DDD-88AD3932ACAF}.Release|x86.ActiveCfg = Release|Any CPU
371373
{F626860C-F141-45B3-9DDD-88AD3932ACAF}.Release|x86.Build.0 = Release|Any CPU
374+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
375+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|Any CPU.Build.0 = Debug|Any CPU
376+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|x64.ActiveCfg = Debug|Any CPU
377+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|x64.Build.0 = Debug|Any CPU
378+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|x86.ActiveCfg = Debug|Any CPU
379+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Debug|x86.Build.0 = Debug|Any CPU
380+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|Any CPU.ActiveCfg = Release|Any CPU
381+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|Any CPU.Build.0 = Release|Any CPU
382+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|x64.ActiveCfg = Release|Any CPU
383+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|x64.Build.0 = Release|Any CPU
384+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|x86.ActiveCfg = Release|Any CPU
385+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07}.Release|x86.Build.0 = Release|Any CPU
372386
EndGlobalSection
373387
GlobalSection(SolutionProperties) = preSolution
374388
HideSolutionNode = FALSE
@@ -399,6 +413,7 @@ Global
399413
{80F19E8A-F097-4AA4-A68C-D417B96BBC68} = {3D1864AA-1FFC-4512-BB13-46055E410F73}
400414
{FD90C861-56C6-4536-B7F5-AC7779296384} = {8AF4A5C2-F0CE-47D5-A4C5-FE4AB83CA509}
401415
{F626860C-F141-45B3-9DDD-88AD3932ACAF} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
416+
{973CCB4A-F344-4C4F-81A5-0F40F7F43C07} = {B70AFB57-57C9-46DC-84BE-11B7DDD34B40}
402417
EndGlobalSection
403418
GlobalSection(ExtensibilityGlobals) = postSolution
404419
SolutionGuid = {049A763A-C891-4E8D-80CF-89DD3E22ADC7}

0 commit comments

Comments
 (0)