Add a new example of how to list a subset of pods using labels. (#110)

This commit is contained in:
Brendan Burns
2018-03-22 16:32:38 -07:00
committed by GitHub
parent d65b6aaf66
commit 7f5ef645c1
2 changed files with 56 additions and 0 deletions

44
examples/labels/PodList.cs Executable file
View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using k8s;
namespace simple
{
internal class PodList
{
private static void Main(string[] args)
{
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
IKubernetes client = new Kubernetes(config);
Console.WriteLine("Starting Request!");
var list = client.ListNamespacedService("default");
foreach (var item in list.Items)
{
Console.WriteLine("Pods for service: " + item.Metadata.Name);
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=");
if (item.Spec == null || item.Spec.Selector == null)
{
continue;
}
var labels = new List<string>();
foreach (var key in item.Spec.Selector)
{
labels.Add(key.Key + "=" + key.Value);
}
var labelStr = string.Join(",", labels.ToArray());
Console.WriteLine(labelStr);
var podList = client.ListNamespacedPod("default", labelSelector: labelStr);
foreach (var pod in podList.Items)
{
Console.WriteLine(pod.Metadata.Name);
}
if (podList.Items.Count == 0)
{
Console.WriteLine("Empty!");
}
Console.WriteLine();
}
}
}
}

12
examples/labels/labels.csproj Executable file
View File

@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\..\src\KubernetesClient.csproj" />
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
</Project>