From 9b1eeef94d92dde920bcbc74a74f7ac14d29cfde Mon Sep 17 00:00:00 2001 From: Boshi Lian Date: Mon, 1 Sep 2025 08:17:12 -0700 Subject: [PATCH] feat: add example for creating and resizing a Kubernetes pod (#1655) --- examples/resize/Program.cs | 63 +++++++++++++++++++++++++++++++++++ examples/resize/resize.csproj | 5 +++ 2 files changed, 68 insertions(+) create mode 100644 examples/resize/Program.cs create mode 100644 examples/resize/resize.csproj diff --git a/examples/resize/Program.cs b/examples/resize/Program.cs new file mode 100644 index 000000000..58682c5b7 --- /dev/null +++ b/examples/resize/Program.cs @@ -0,0 +1,63 @@ +using k8s; +using k8s.Models; +using System; +using System.Collections.Generic; + + +var config = KubernetesClientConfiguration.BuildDefaultConfig(); +var client = new Kubernetes(config); + + +var pod = new V1Pod +{ + Metadata = new V1ObjectMeta { Name = "nginx-pod" }, + Spec = new V1PodSpec + { + Containers = + [ + new V1Container + { + Name = "nginx", + Image = "nginx", + Resources = new V1ResourceRequirements + { + Requests = new Dictionary() + { + ["cpu"] = new ResourceQuantity("100m"), + }, + }, + }, + ], + }, +}; +{ + var created = await client.CoreV1.CreateNamespacedPodAsync(pod, "default").ConfigureAwait(false); + Console.WriteLine($"Created pod: {created.Metadata.Name}"); +} + +{ + var patchStr = @" + { + ""spec"": { + ""containers"": [ + { + ""name"": ""nginx"", + ""resources"": { + ""requests"": { + ""cpu"": ""200m"" + } + } + } + ] + } + }"; + + var patch = await client.CoreV1.PatchNamespacedPodResizeAsync(new V1Patch(patchStr, V1Patch.PatchType.MergePatch), "nginx-pod", "default").ConfigureAwait(false); + + if (patch?.Spec?.Containers?.Count > 0 && + patch.Spec.Containers[0].Resources?.Requests != null && + patch.Spec.Containers[0].Resources.Requests.TryGetValue("cpu", out var cpuQty)) + { + Console.WriteLine($"CPU request: {cpuQty}"); + } +} diff --git a/examples/resize/resize.csproj b/examples/resize/resize.csproj new file mode 100644 index 000000000..d1e5b4724 --- /dev/null +++ b/examples/resize/resize.csproj @@ -0,0 +1,5 @@ + + + Exe + + \ No newline at end of file