|
| 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); |
0 commit comments