Files
csharp/examples/restart/Program.cs
Boshi Lian 001189de77 Version 17.0 + Generate 1.33 (#1626)
* generated based on 1.33

* Update version to 17.0 in version.json

* Remove extra API endpoint from swagger.json

* Remove ModelConverter and related AutoMapper components

* Update package versions

* Refactor code to use ConfigureAwait(false) for asynchronous calls and update target framework to net9.0

* Remove ConfigureAwait(false) from OidcAuthTests for consistency in async calls

* Update SDK version in README to reflect support for net8.0 and net9.0

* Update dotnet SDK version to 9.0.x in build workflow

* Revert Fractions package version to 7.3.0 in Directory.Packages.props

* Update target framework to netstandard2.1 for improved compatibility

* Update package references for Microsoft.CodeAnalysis in Directory.Packages.props and LibKubernetesGenerator.target

* Refactor Worker class constructor documentation and standardize Dictionary type declaration in Program.cs
2025-04-27 12:55:24 -07:00

69 lines
3.0 KiB
C#

using Json.Patch;
using k8s;
using k8s.Models;
using System.Text.Json;
async Task RestartDaemonSetAsync(string name, string @namespace, IKubernetes client)
{
var daemonSet = await client.AppsV1.ReadNamespacedDaemonSetAsync(name, @namespace).ConfigureAwait(false);
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true };
var old = JsonSerializer.SerializeToDocument(daemonSet, options);
var now = DateTimeOffset.Now.ToUnixTimeSeconds();
var restart = new Dictionary<string, string>
{
["date"] = now.ToString(),
};
daemonSet.Spec.Template.Metadata.Annotations = restart;
var expected = JsonSerializer.SerializeToDocument(daemonSet);
var patch = old.CreatePatch(expected);
await client.AppsV1.PatchNamespacedDaemonSetAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), name, @namespace).ConfigureAwait(false);
}
async Task RestartDeploymentAsync(string name, string @namespace, IKubernetes client)
{
var deployment = await client.AppsV1.ReadNamespacedDeploymentAsync(name, @namespace).ConfigureAwait(false);
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true };
var old = JsonSerializer.SerializeToDocument(deployment, options);
var now = DateTimeOffset.Now.ToUnixTimeSeconds();
var restart = new Dictionary<string, string>
{
["date"] = now.ToString(),
};
deployment.Spec.Template.Metadata.Annotations = restart;
var expected = JsonSerializer.SerializeToDocument(deployment);
var patch = old.CreatePatch(expected);
await client.AppsV1.PatchNamespacedDeploymentAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), name, @namespace).ConfigureAwait(false);
}
async Task RestartStatefulSetAsync(string name, string @namespace, IKubernetes client)
{
var deployment = await client.AppsV1.ReadNamespacedStatefulSetAsync(name, @namespace).ConfigureAwait(false);
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true };
var old = JsonSerializer.SerializeToDocument(deployment, options);
var now = DateTimeOffset.Now.ToUnixTimeSeconds();
var restart = new Dictionary<string, string>
{
["date"] = now.ToString(),
};
deployment.Spec.Template.Metadata.Annotations = restart;
var expected = JsonSerializer.SerializeToDocument(deployment);
var patch = old.CreatePatch(expected);
await client.AppsV1.PatchNamespacedStatefulSetAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), name, @namespace).ConfigureAwait(false);
}
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
IKubernetes client = new Kubernetes(config);
await RestartDeploymentAsync("event-exporter", "monitoring", client).ConfigureAwait(false);
await RestartDaemonSetAsync("prometheus-exporter", "monitoring", client).ConfigureAwait(false);
await RestartStatefulSetAsync("argocd-application-controlle", "argocd", client).ConfigureAwait(false);