feat: add example for creating and resizing a Kubernetes pod (#1655)

This commit is contained in:
Boshi Lian
2025-09-01 08:17:12 -07:00
committed by GitHub
parent 69afa49337
commit 9b1eeef94d
2 changed files with 68 additions and 0 deletions

View File

@@ -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<string, ResourceQuantity>()
{
["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}");
}
}

View File

@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
</Project>