Skip to content

Commit 7f5ef64

Browse files
Add a new example of how to list a subset of pods using labels. (#110)
1 parent d65b6aa commit 7f5ef64

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

examples/labels/PodList.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using k8s;
4+
5+
namespace simple
6+
{
7+
internal class PodList
8+
{
9+
private static void Main(string[] args)
10+
{
11+
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
12+
IKubernetes client = new Kubernetes(config);
13+
Console.WriteLine("Starting Request!");
14+
15+
var list = client.ListNamespacedService("default");
16+
foreach (var item in list.Items)
17+
{
18+
Console.WriteLine("Pods for service: " + item.Metadata.Name);
19+
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=");
20+
if (item.Spec == null || item.Spec.Selector == null)
21+
{
22+
continue;
23+
}
24+
var labels = new List<string>();
25+
foreach (var key in item.Spec.Selector)
26+
{
27+
labels.Add(key.Key + "=" + key.Value);
28+
}
29+
var labelStr = string.Join(",", labels.ToArray());
30+
Console.WriteLine(labelStr);
31+
var podList = client.ListNamespacedPod("default", labelSelector: labelStr);
32+
foreach (var pod in podList.Items)
33+
{
34+
Console.WriteLine(pod.Metadata.Name);
35+
}
36+
if (podList.Items.Count == 0)
37+
{
38+
Console.WriteLine("Empty!");
39+
}
40+
Console.WriteLine();
41+
}
42+
}
43+
}
44+
}

examples/labels/labels.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\src\KubernetesClient.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<OutputType>Exe</OutputType>
9+
<TargetFramework>netcoreapp2.0</TargetFramework>
10+
</PropertyGroup>
11+
12+
</Project>

0 commit comments

Comments
 (0)