Skip to content

Commit 9b1eeef

Browse files
authored
feat: add example for creating and resizing a Kubernetes pod (#1655)
1 parent 69afa49 commit 9b1eeef

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

examples/resize/Program.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using k8s;
2+
using k8s.Models;
3+
using System;
4+
using System.Collections.Generic;
5+
6+
7+
var config = KubernetesClientConfiguration.BuildDefaultConfig();
8+
var client = new Kubernetes(config);
9+
10+
11+
var pod = new V1Pod
12+
{
13+
Metadata = new V1ObjectMeta { Name = "nginx-pod" },
14+
Spec = new V1PodSpec
15+
{
16+
Containers =
17+
[
18+
new V1Container
19+
{
20+
Name = "nginx",
21+
Image = "nginx",
22+
Resources = new V1ResourceRequirements
23+
{
24+
Requests = new Dictionary<string, ResourceQuantity>()
25+
{
26+
["cpu"] = new ResourceQuantity("100m"),
27+
},
28+
},
29+
},
30+
],
31+
},
32+
};
33+
{
34+
var created = await client.CoreV1.CreateNamespacedPodAsync(pod, "default").ConfigureAwait(false);
35+
Console.WriteLine($"Created pod: {created.Metadata.Name}");
36+
}
37+
38+
{
39+
var patchStr = @"
40+
{
41+
""spec"": {
42+
""containers"": [
43+
{
44+
""name"": ""nginx"",
45+
""resources"": {
46+
""requests"": {
47+
""cpu"": ""200m""
48+
}
49+
}
50+
}
51+
]
52+
}
53+
}";
54+
55+
var patch = await client.CoreV1.PatchNamespacedPodResizeAsync(new V1Patch(patchStr, V1Patch.PatchType.MergePatch), "nginx-pod", "default").ConfigureAwait(false);
56+
57+
if (patch?.Spec?.Containers?.Count > 0 &&
58+
patch.Spec.Containers[0].Resources?.Requests != null &&
59+
patch.Spec.Containers[0].Resources.Requests.TryGetValue("cpu", out var cpuQty))
60+
{
61+
Console.WriteLine($"CPU request: {cpuQty}");
62+
}
63+
}

examples/resize/resize.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
</PropertyGroup>
5+
</Project>

0 commit comments

Comments
 (0)