Clean up warnings in examples (#1628)
* Refactor examples to streamline code structure and improve readability * Update LangVersion to 13.0 for improved compatibility
This commit is contained in:
@@ -26,7 +26,7 @@
|
|||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||||
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
||||||
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
|
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
|
||||||
<LangVersion>11.0</LangVersion>
|
<LangVersion>13.0</LangVersion>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
|
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
|
||||||
|
|||||||
@@ -3,38 +3,29 @@ using k8s.Models;
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace attach
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
|
IKubernetes client = new Kubernetes(config);
|
||||||
|
Console.WriteLine("Starting Request!");
|
||||||
|
|
||||||
|
var list = client.CoreV1.ListNamespacedPod("default");
|
||||||
|
var pod = list.Items[0];
|
||||||
|
await AttachToPod(client, pod).ConfigureAwait(false);
|
||||||
|
|
||||||
|
async Task AttachToPod(IKubernetes client, V1Pod pod)
|
||||||
{
|
{
|
||||||
internal class Attach
|
var webSocket =
|
||||||
|
await client.WebSocketNamespacedPodAttachAsync(pod.Metadata.Name, "default",
|
||||||
|
pod.Spec.Containers[0].Name).ConfigureAwait(false);
|
||||||
|
|
||||||
|
var demux = new StreamDemuxer(webSocket);
|
||||||
|
demux.Start();
|
||||||
|
|
||||||
|
var buff = new byte[4096];
|
||||||
|
var stream = demux.GetStream(1, 1);
|
||||||
|
while (true)
|
||||||
{
|
{
|
||||||
private static async Task Main(string[] args)
|
var read = stream.Read(buff, 0, 4096);
|
||||||
{
|
var str = System.Text.Encoding.Default.GetString(buff);
|
||||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
Console.WriteLine(str);
|
||||||
IKubernetes client = new Kubernetes(config);
|
|
||||||
Console.WriteLine("Starting Request!");
|
|
||||||
|
|
||||||
var list = client.CoreV1.ListNamespacedPod("default");
|
|
||||||
var pod = list.Items[0];
|
|
||||||
await AttachToPod(client, pod).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task AttachToPod(IKubernetes client, V1Pod pod)
|
|
||||||
{
|
|
||||||
var webSocket =
|
|
||||||
await client.WebSocketNamespacedPodAttachAsync(pod.Metadata.Name, "default",
|
|
||||||
pod.Spec.Containers[0].Name).ConfigureAwait(false);
|
|
||||||
|
|
||||||
var demux = new StreamDemuxer(webSocket);
|
|
||||||
demux.Start();
|
|
||||||
|
|
||||||
var buff = new byte[4096];
|
|
||||||
var stream = demux.GetStream(1, 1);
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
var read = stream.Read(buff, 0, 4096);
|
|
||||||
var str = System.Text.Encoding.Default.GetString(buff);
|
|
||||||
Console.WriteLine(str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ string GenerateCertificate(string name)
|
|||||||
var request = new CertificateRequest(distinguishedName, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
var request = new CertificateRequest(distinguishedName, rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||||
|
|
||||||
request.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.DataEncipherment | X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DigitalSignature, false));
|
request.CertificateExtensions.Add(new X509KeyUsageExtension(X509KeyUsageFlags.DataEncipherment | X509KeyUsageFlags.KeyEncipherment | X509KeyUsageFlags.DigitalSignature, false));
|
||||||
request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension(new OidCollection { new("1.3.6.1.5.5.7.3.1") }, false));
|
request.CertificateExtensions.Add(new X509EnhancedKeyUsageExtension([new ("1.3.6.1.5.5.7.3.1")], false));
|
||||||
request.CertificateExtensions.Add(sanBuilder.Build());
|
request.CertificateExtensions.Add(sanBuilder.Build());
|
||||||
var csr = request.CreateSigningRequest();
|
var csr = request.CreateSigningRequest();
|
||||||
var pemKey = "-----BEGIN CERTIFICATE REQUEST-----\r\n" +
|
var pemKey = "-----BEGIN CERTIFICATE REQUEST-----\r\n" +
|
||||||
@@ -67,7 +67,7 @@ var old = JsonSerializer.SerializeToDocument(readCert, serializeOptions);
|
|||||||
|
|
||||||
var replace = new List<V1CertificateSigningRequestCondition>
|
var replace = new List<V1CertificateSigningRequestCondition>
|
||||||
{
|
{
|
||||||
new("True", "Approved", DateTime.UtcNow, DateTime.UtcNow, "This certificate was approved by k8s client", "Approve"),
|
new ("True", "Approved", DateTime.UtcNow, DateTime.UtcNow, "This certificate was approved by k8s client", "Approve"),
|
||||||
};
|
};
|
||||||
readCert.Status.Conditions = replace;
|
readCert.Status.Conditions = replace;
|
||||||
|
|
||||||
|
|||||||
@@ -3,35 +3,26 @@ using k8s.Models;
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace exec
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
|
IKubernetes client = new Kubernetes(config);
|
||||||
|
Console.WriteLine("Starting Request!");
|
||||||
|
|
||||||
|
var list = client.CoreV1.ListNamespacedPod("default");
|
||||||
|
var pod = list.Items[0];
|
||||||
|
await ExecInPod(client, pod).ConfigureAwait(false);
|
||||||
|
|
||||||
|
async Task ExecInPod(IKubernetes client, V1Pod pod)
|
||||||
{
|
{
|
||||||
internal class Exec
|
var webSocket =
|
||||||
{
|
await client.WebSocketNamespacedPodExecAsync(pod.Metadata.Name, "default", "ls",
|
||||||
private static async Task Main(string[] args)
|
pod.Spec.Containers[0].Name).ConfigureAwait(false);
|
||||||
{
|
|
||||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
||||||
IKubernetes client = new Kubernetes(config);
|
|
||||||
Console.WriteLine("Starting Request!");
|
|
||||||
|
|
||||||
var list = client.CoreV1.ListNamespacedPod("default");
|
var demux = new StreamDemuxer(webSocket);
|
||||||
var pod = list.Items[0];
|
demux.Start();
|
||||||
await ExecInPod(client, pod).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task ExecInPod(IKubernetes client, V1Pod pod)
|
var buff = new byte[4096];
|
||||||
{
|
var stream = demux.GetStream(1, 1);
|
||||||
var webSocket =
|
var read = stream.Read(buff, 0, 4096);
|
||||||
await client.WebSocketNamespacedPodExecAsync(pod.Metadata.Name, "default", "ls",
|
var str = System.Text.Encoding.Default.GetString(buff);
|
||||||
pod.Spec.Containers[0].Name).ConfigureAwait(false);
|
Console.WriteLine(str);
|
||||||
|
|
||||||
var demux = new StreamDemuxer(webSocket);
|
|
||||||
demux.Start();
|
|
||||||
|
|
||||||
var buff = new byte[4096];
|
|
||||||
var stream = demux.GetStream(1, 1);
|
|
||||||
var read = stream.Read(buff, 0, 4096);
|
|
||||||
var str = System.Text.Encoding.Default.GetString(buff);
|
|
||||||
Console.WriteLine(str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,16 @@
|
|||||||
using k8s;
|
using k8s;
|
||||||
using k8s.Models;
|
using k8s.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace exec
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
|
IKubernetes client = new Kubernetes(config);
|
||||||
|
var generic = new GenericClient(client, "", "v1", "nodes");
|
||||||
|
var node = await generic.ReadAsync<V1Node>("kube0").ConfigureAwait(false);
|
||||||
|
Console.WriteLine(node.Metadata.Name);
|
||||||
|
|
||||||
|
var genericPods = new GenericClient(client, "", "v1", "pods");
|
||||||
|
var pods = await genericPods.ListNamespacedAsync<V1PodList>("default").ConfigureAwait(false);
|
||||||
|
foreach (var pod in pods.Items)
|
||||||
{
|
{
|
||||||
internal class Generic
|
Console.WriteLine(pod.Metadata.Name);
|
||||||
{
|
|
||||||
private static async Task Main(string[] args)
|
|
||||||
{
|
|
||||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
||||||
IKubernetes client = new Kubernetes(config);
|
|
||||||
var generic = new GenericClient(client, "", "v1", "nodes");
|
|
||||||
var node = await generic.ReadAsync<V1Node>("kube0").ConfigureAwait(false);
|
|
||||||
Console.WriteLine(node.Metadata.Name);
|
|
||||||
|
|
||||||
var genericPods = new GenericClient(client, "", "v1", "pods");
|
|
||||||
var pods = await genericPods.ListNamespacedAsync<V1PodList>("default").ConfigureAwait(false);
|
|
||||||
foreach (var pod in pods.Items)
|
|
||||||
{
|
|
||||||
Console.WriteLine(pod.Metadata.Name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,47 +2,38 @@ using k8s;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace simple
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
|
IKubernetes client = new Kubernetes(config);
|
||||||
|
Console.WriteLine("Starting Request!");
|
||||||
|
|
||||||
|
var list = client.CoreV1.ListNamespacedService("default");
|
||||||
|
foreach (var item in list.Items)
|
||||||
{
|
{
|
||||||
internal class PodList
|
Console.WriteLine("Pods for service: " + item.Metadata.Name);
|
||||||
|
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=");
|
||||||
|
if (item.Spec == null || item.Spec.Selector == null)
|
||||||
{
|
{
|
||||||
private static void Main(string[] args)
|
continue;
|
||||||
{
|
|
||||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
||||||
IKubernetes client = new Kubernetes(config);
|
|
||||||
Console.WriteLine("Starting Request!");
|
|
||||||
|
|
||||||
var list = client.CoreV1.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.CoreV1.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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.CoreV1.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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +1,21 @@
|
|||||||
using k8s;
|
using k8s;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace logs
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
|
IKubernetes client = new Kubernetes(config);
|
||||||
|
Console.WriteLine("Starting Request!");
|
||||||
|
|
||||||
|
var list = client.CoreV1.ListNamespacedPod("default");
|
||||||
|
if (list.Items.Count == 0)
|
||||||
{
|
{
|
||||||
internal class Logs
|
Console.WriteLine("No pods!");
|
||||||
{
|
return;
|
||||||
private static async Task Main(string[] args)
|
|
||||||
{
|
|
||||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
||||||
IKubernetes client = new Kubernetes(config);
|
|
||||||
Console.WriteLine("Starting Request!");
|
|
||||||
|
|
||||||
var list = client.CoreV1.ListNamespacedPod("default");
|
|
||||||
if (list.Items.Count == 0)
|
|
||||||
{
|
|
||||||
Console.WriteLine("No pods!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var pod = list.Items[0];
|
|
||||||
|
|
||||||
var response = await client.CoreV1.ReadNamespacedPodLogWithHttpMessagesAsync(
|
|
||||||
pod.Metadata.Name,
|
|
||||||
pod.Metadata.NamespaceProperty, container: pod.Spec.Containers[0].Name, follow: true).ConfigureAwait(false);
|
|
||||||
var stream = response.Body;
|
|
||||||
stream.CopyTo(Console.OpenStandardOutput());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var pod = list.Items[0];
|
||||||
|
|
||||||
|
var response = await client.CoreV1.ReadNamespacedPodLogWithHttpMessagesAsync(
|
||||||
|
pod.Metadata.Name,
|
||||||
|
pod.Metadata.NamespaceProperty, container: pod.Spec.Containers[0].Name, follow: true).ConfigureAwait(false);
|
||||||
|
var stream = response.Body;
|
||||||
|
stream.CopyTo(Console.OpenStandardOutput());
|
||||||
|
|||||||
@@ -3,58 +3,49 @@ using System;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace metrics
|
async Task NodesMetrics(IKubernetes client)
|
||||||
{
|
{
|
||||||
internal class Program
|
var nodesMetrics = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
|
foreach (var item in nodesMetrics.Items)
|
||||||
{
|
{
|
||||||
private static async Task NodesMetrics(IKubernetes client)
|
Console.WriteLine(item.Metadata.Name);
|
||||||
|
|
||||||
|
foreach (var metric in item.Usage)
|
||||||
{
|
{
|
||||||
var nodesMetrics = await client.GetKubernetesNodesMetricsAsync().ConfigureAwait(false);
|
Console.WriteLine($"{metric.Key}: {metric.Value}");
|
||||||
|
|
||||||
foreach (var item in nodesMetrics.Items)
|
|
||||||
{
|
|
||||||
Console.WriteLine(item.Metadata.Name);
|
|
||||||
|
|
||||||
foreach (var metric in item.Usage)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"{metric.Key}: {metric.Value}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task PodsMetrics(IKubernetes client)
|
|
||||||
{
|
|
||||||
var podsMetrics = await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(false);
|
|
||||||
|
|
||||||
if (!podsMetrics.Items.Any())
|
|
||||||
{
|
|
||||||
Console.WriteLine("Empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var item in podsMetrics.Items)
|
|
||||||
{
|
|
||||||
foreach (var container in item.Containers)
|
|
||||||
{
|
|
||||||
Console.WriteLine(container.Name);
|
|
||||||
|
|
||||||
foreach (var metric in container.Usage)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"{metric.Key}: {metric.Value}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.Write(Environment.NewLine);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task Main(string[] args)
|
|
||||||
{
|
|
||||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
||||||
var client = new Kubernetes(config);
|
|
||||||
|
|
||||||
await NodesMetrics(client).ConfigureAwait(false);
|
|
||||||
Console.WriteLine(Environment.NewLine);
|
|
||||||
await PodsMetrics(client).ConfigureAwait(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async Task PodsMetrics(IKubernetes client)
|
||||||
|
{
|
||||||
|
var podsMetrics = await client.GetKubernetesPodsMetricsAsync().ConfigureAwait(false);
|
||||||
|
|
||||||
|
if (!podsMetrics.Items.Any())
|
||||||
|
{
|
||||||
|
Console.WriteLine("Empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var item in podsMetrics.Items)
|
||||||
|
{
|
||||||
|
foreach (var container in item.Containers)
|
||||||
|
{
|
||||||
|
Console.WriteLine(container.Name);
|
||||||
|
|
||||||
|
foreach (var metric in container.Usage)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{metric.Key}: {metric.Value}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.Write(Environment.NewLine);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
|
var client = new Kubernetes(config);
|
||||||
|
|
||||||
|
await NodesMetrics(client).ConfigureAwait(false);
|
||||||
|
Console.WriteLine(Environment.NewLine);
|
||||||
|
await PodsMetrics(client).ConfigureAwait(false);
|
||||||
|
|||||||
@@ -4,52 +4,37 @@ using System;
|
|||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace @namespace
|
void ListNamespaces(IKubernetes client)
|
||||||
{
|
{
|
||||||
internal class NamespaceExample
|
var list = client.CoreV1.ListNamespace();
|
||||||
|
foreach (var item in list.Items)
|
||||||
{
|
{
|
||||||
private static void ListNamespaces(IKubernetes client)
|
Console.WriteLine(item.Metadata.Name);
|
||||||
{
|
}
|
||||||
var list = client.CoreV1.ListNamespace();
|
|
||||||
foreach (var item in list.Items)
|
|
||||||
{
|
|
||||||
Console.WriteLine(item.Metadata.Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (list.Items.Count == 0)
|
if (list.Items.Count == 0)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Empty!");
|
Console.WriteLine("Empty!");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task DeleteAsync(IKubernetes client, string name, int delayMillis)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
await Task.Delay(delayMillis).ConfigureAwait(false);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await client.CoreV1.ReadNamespaceAsync(name).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
catch (AggregateException ex)
|
||||||
private static async Task DeleteAsync(IKubernetes client, string name, int delayMillis)
|
|
||||||
{
|
{
|
||||||
while (true)
|
foreach (var innerEx in ex.InnerExceptions)
|
||||||
{
|
{
|
||||||
await Task.Delay(delayMillis).ConfigureAwait(false);
|
if (innerEx is k8s.Autorest.HttpOperationException exception)
|
||||||
try
|
|
||||||
{
|
{
|
||||||
await client.CoreV1.ReadNamespaceAsync(name).ConfigureAwait(false);
|
var code = exception.Response.StatusCode;
|
||||||
}
|
if (code == HttpStatusCode.NotFound)
|
||||||
catch (AggregateException ex)
|
|
||||||
{
|
|
||||||
foreach (var innerEx in ex.InnerExceptions)
|
|
||||||
{
|
|
||||||
if (innerEx is k8s.Autorest.HttpOperationException exception)
|
|
||||||
{
|
|
||||||
var code = exception.Response.StatusCode;
|
|
||||||
if (code == HttpStatusCode.NotFound)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (k8s.Autorest.HttpOperationException ex)
|
|
||||||
{
|
|
||||||
if (ex.Response.StatusCode == HttpStatusCode.NotFound)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -58,41 +43,47 @@ namespace @namespace
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (k8s.Autorest.HttpOperationException ex)
|
||||||
private static void Delete(IKubernetes client, string name, int delayMillis)
|
|
||||||
{
|
{
|
||||||
DeleteAsync(client, name, delayMillis).Wait();
|
if (ex.Response.StatusCode == HttpStatusCode.NotFound)
|
||||||
}
|
|
||||||
|
|
||||||
private static void Main(string[] args)
|
|
||||||
{
|
|
||||||
var k8SClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
||||||
IKubernetes client = new Kubernetes(k8SClientConfig);
|
|
||||||
|
|
||||||
ListNamespaces(client);
|
|
||||||
|
|
||||||
var ns = new V1Namespace { Metadata = new V1ObjectMeta { Name = "test" } };
|
|
||||||
|
|
||||||
var result = client.CoreV1.CreateNamespace(ns);
|
|
||||||
Console.WriteLine(result);
|
|
||||||
|
|
||||||
ListNamespaces(client);
|
|
||||||
|
|
||||||
var status = client.CoreV1.DeleteNamespace(ns.Metadata.Name, new V1DeleteOptions());
|
|
||||||
|
|
||||||
if (status.HasObject)
|
|
||||||
{
|
{
|
||||||
var obj = status.ObjectView<V1Namespace>();
|
return;
|
||||||
Console.WriteLine(obj.Status.Phase);
|
|
||||||
|
|
||||||
Delete(client, ns.Metadata.Name, 3 * 1000);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine(status.Message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ListNamespaces(client);
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Delete(IKubernetes client, string name, int delayMillis)
|
||||||
|
{
|
||||||
|
DeleteAsync(client, name, delayMillis).Wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
var k8SClientConfig = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
|
IKubernetes client = new Kubernetes(k8SClientConfig);
|
||||||
|
|
||||||
|
ListNamespaces(client);
|
||||||
|
|
||||||
|
var ns = new V1Namespace { Metadata = new V1ObjectMeta { Name = "test" } };
|
||||||
|
|
||||||
|
var result = client.CoreV1.CreateNamespace(ns);
|
||||||
|
Console.WriteLine(result);
|
||||||
|
|
||||||
|
ListNamespaces(client);
|
||||||
|
|
||||||
|
var status = client.CoreV1.DeleteNamespace(ns.Metadata.Name, new V1DeleteOptions());
|
||||||
|
|
||||||
|
if (status.HasObject)
|
||||||
|
{
|
||||||
|
var obj = status.ObjectView<V1Namespace>();
|
||||||
|
Console.WriteLine(obj.Status.Phase);
|
||||||
|
|
||||||
|
Delete(client, ns.Metadata.Name, 3 * 1000);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine(status.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
ListNamespaces(client);
|
||||||
|
|||||||
@@ -3,21 +3,15 @@ using k8s.Models;
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace patch
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
{
|
IKubernetes client = new Kubernetes(config);
|
||||||
internal class Program
|
Console.WriteLine("Starting Request!");
|
||||||
{
|
|
||||||
private static void Main(string[] args)
|
|
||||||
{
|
|
||||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
|
||||||
IKubernetes client = new Kubernetes(config);
|
|
||||||
Console.WriteLine("Starting Request!");
|
|
||||||
|
|
||||||
var pod = client.CoreV1.ListNamespacedPod("default").Items.First();
|
var pod = client.CoreV1.ListNamespacedPod("default").Items.First();
|
||||||
var name = pod.Metadata.Name;
|
var name = pod.Metadata.Name;
|
||||||
PrintLabels(pod);
|
PrintLabels(pod);
|
||||||
|
|
||||||
var patchStr = @"
|
var patchStr = @"
|
||||||
{
|
{
|
||||||
""metadata"": {
|
""metadata"": {
|
||||||
""labels"": {
|
""labels"": {
|
||||||
@@ -26,19 +20,16 @@ namespace patch
|
|||||||
}
|
}
|
||||||
}";
|
}";
|
||||||
|
|
||||||
client.CoreV1.PatchNamespacedPod(new V1Patch(patchStr, V1Patch.PatchType.MergePatch), name, "default");
|
client.CoreV1.PatchNamespacedPod(new V1Patch(patchStr, V1Patch.PatchType.MergePatch), name, "default");
|
||||||
PrintLabels(client.CoreV1.ReadNamespacedPod(name, "default"));
|
PrintLabels(client.CoreV1.ReadNamespacedPod(name, "default"));
|
||||||
}
|
|
||||||
|
|
||||||
private static void PrintLabels(V1Pod pod)
|
void PrintLabels(V1Pod pod)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Labels: for {pod.Metadata.Name}");
|
Console.WriteLine($"Labels: for {pod.Metadata.Name}");
|
||||||
foreach (var (k, v) in pod.Metadata.Labels)
|
foreach (var (k, v) in pod.Metadata.Labels)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"{k} : {v}");
|
Console.WriteLine($"{k} : {v}");
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("=-=-=-=-=-=-=-=-=-=-=");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,75 +6,66 @@ using System.Net.Sockets;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace portforward
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
|
IKubernetes client = new Kubernetes(config);
|
||||||
|
Console.WriteLine("Starting port forward!");
|
||||||
|
|
||||||
|
var list = client.CoreV1.ListNamespacedPod("default");
|
||||||
|
var pod = list.Items[0];
|
||||||
|
await Forward(client, pod).ConfigureAwait(false);
|
||||||
|
|
||||||
|
async Task Forward(IKubernetes client, V1Pod pod)
|
||||||
{
|
{
|
||||||
internal class Portforward
|
// Note this is single-threaded, it won't handle concurrent requests well...
|
||||||
|
var webSocket = await client.WebSocketNamespacedPodPortForwardAsync(pod.Metadata.Name, "default", new int[] { 80 }, "v4.channel.k8s.io").ConfigureAwait(false);
|
||||||
|
var demux = new StreamDemuxer(webSocket, StreamType.PortForward);
|
||||||
|
demux.Start();
|
||||||
|
|
||||||
|
var stream = demux.GetStream((byte?)0, (byte?)0);
|
||||||
|
|
||||||
|
IPAddress ipAddress = IPAddress.Loopback;
|
||||||
|
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 8080);
|
||||||
|
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
listener.Bind(localEndPoint);
|
||||||
|
listener.Listen(100);
|
||||||
|
|
||||||
|
Socket handler = null;
|
||||||
|
|
||||||
|
// Note this will only accept a single connection
|
||||||
|
var accept = Task.Run(() =>
|
||||||
{
|
{
|
||||||
private static async Task Main(string[] args)
|
while (true)
|
||||||
{
|
{
|
||||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
handler = listener.Accept();
|
||||||
IKubernetes client = new Kubernetes(config);
|
var bytes = new byte[4096];
|
||||||
Console.WriteLine("Starting port forward!");
|
while (true)
|
||||||
|
|
||||||
var list = client.CoreV1.ListNamespacedPod("default");
|
|
||||||
var pod = list.Items[0];
|
|
||||||
await Forward(client, pod).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static async Task Forward(IKubernetes client, V1Pod pod)
|
|
||||||
{
|
|
||||||
// Note this is single-threaded, it won't handle concurrent requests well...
|
|
||||||
var webSocket = await client.WebSocketNamespacedPodPortForwardAsync(pod.Metadata.Name, "default", new int[] { 80 }, "v4.channel.k8s.io").ConfigureAwait(false);
|
|
||||||
var demux = new StreamDemuxer(webSocket, StreamType.PortForward);
|
|
||||||
demux.Start();
|
|
||||||
|
|
||||||
var stream = demux.GetStream((byte?)0, (byte?)0);
|
|
||||||
|
|
||||||
IPAddress ipAddress = IPAddress.Loopback;
|
|
||||||
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 8080);
|
|
||||||
Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
|
|
||||||
listener.Bind(localEndPoint);
|
|
||||||
listener.Listen(100);
|
|
||||||
|
|
||||||
Socket handler = null;
|
|
||||||
|
|
||||||
// Note this will only accept a single connection
|
|
||||||
var accept = Task.Run(() =>
|
|
||||||
{
|
{
|
||||||
while (true)
|
int bytesRec = handler.Receive(bytes);
|
||||||
|
stream.Write(bytes, 0, bytesRec);
|
||||||
|
if (bytesRec == 0 || Encoding.ASCII.GetString(bytes, 0, bytesRec).IndexOf("<EOF>") > -1)
|
||||||
{
|
{
|
||||||
handler = listener.Accept();
|
break;
|
||||||
var bytes = new byte[4096];
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
int bytesRec = handler.Receive(bytes);
|
|
||||||
stream.Write(bytes, 0, bytesRec);
|
|
||||||
if (bytesRec == 0 || Encoding.ASCII.GetString(bytes, 0, bytesRec).IndexOf("<EOF>") > -1)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
var copy = Task.Run(() =>
|
|
||||||
{
|
|
||||||
var buff = new byte[4096];
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
var read = stream.Read(buff, 0, 4096);
|
|
||||||
handler.Send(buff, read, 0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
await accept.ConfigureAwait(false);
|
|
||||||
await copy.ConfigureAwait(false);
|
|
||||||
if (handler != null)
|
|
||||||
{
|
|
||||||
handler.Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
listener.Close();
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
var copy = Task.Run(() =>
|
||||||
|
{
|
||||||
|
var buff = new byte[4096];
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
var read = stream.Read(buff, 0, 4096);
|
||||||
|
handler.Send(buff, read, 0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await accept.ConfigureAwait(false);
|
||||||
|
await copy.ConfigureAwait(false);
|
||||||
|
if (handler != null)
|
||||||
|
{
|
||||||
|
handler.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
listener.Close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,17 @@
|
|||||||
using k8s;
|
using k8s;
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace simple
|
var config = KubernetesClientConfiguration.BuildDefaultConfig();
|
||||||
|
IKubernetes client = new Kubernetes(config);
|
||||||
|
Console.WriteLine("Starting Request!");
|
||||||
|
|
||||||
|
var list = client.CoreV1.ListNamespacedPod("default");
|
||||||
|
foreach (var item in list.Items)
|
||||||
{
|
{
|
||||||
internal class PodList
|
Console.WriteLine(item.Metadata.Name);
|
||||||
{
|
}
|
||||||
private static void Main(string[] args)
|
|
||||||
{
|
if (list.Items.Count == 0)
|
||||||
var config = KubernetesClientConfiguration.BuildDefaultConfig();
|
{
|
||||||
IKubernetes client = new Kubernetes(config);
|
Console.WriteLine("Empty!");
|
||||||
Console.WriteLine("Starting Request!");
|
|
||||||
|
|
||||||
var list = client.CoreV1.ListNamespacedPod("default");
|
|
||||||
foreach (var item in list.Items)
|
|
||||||
{
|
|
||||||
Console.WriteLine(item.Metadata.Name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (list.Items.Count == 0)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Empty!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,49 +4,37 @@ using System;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace watch
|
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
||||||
|
|
||||||
|
IKubernetes client = new Kubernetes(config);
|
||||||
|
|
||||||
|
var podlistResp = client.CoreV1.ListNamespacedPodWithHttpMessagesAsync("default", watch: true);
|
||||||
|
// C# 8 required https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8
|
||||||
|
await foreach (var (type, item) in podlistResp.WatchAsync<V1Pod, V1PodList>().ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
internal class Program
|
Console.WriteLine("==on watch event==");
|
||||||
|
Console.WriteLine(type);
|
||||||
|
Console.WriteLine(item.Metadata.Name);
|
||||||
|
Console.WriteLine("==on watch event==");
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma warning disable CS8321 // Remove unused private members
|
||||||
|
void WatchUsingCallback(IKubernetes client)
|
||||||
|
#pragma warning restore CS8321 // Remove unused private members
|
||||||
|
{
|
||||||
|
var podlistResp = client.CoreV1.ListNamespacedPodWithHttpMessagesAsync("default", watch: true);
|
||||||
|
using (podlistResp.Watch<V1Pod, V1PodList>((type, item) =>
|
||||||
{
|
{
|
||||||
private static async Task Main(string[] args)
|
Console.WriteLine("==on watch event==");
|
||||||
{
|
Console.WriteLine(type);
|
||||||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
|
Console.WriteLine(item.Metadata.Name);
|
||||||
|
Console.WriteLine("==on watch event==");
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
Console.WriteLine("press ctrl + c to stop watching");
|
||||||
|
|
||||||
IKubernetes client = new Kubernetes(config);
|
var ctrlc = new ManualResetEventSlim(false);
|
||||||
|
Console.CancelKeyPress += (sender, eventArgs) => ctrlc.Set();
|
||||||
var podlistResp = client.CoreV1.ListNamespacedPodWithHttpMessagesAsync("default", watch: true);
|
ctrlc.Wait();
|
||||||
// C# 8 required https://docs.microsoft.com/en-us/archive/msdn-magazine/2019/november/csharp-iterating-with-async-enumerables-in-csharp-8
|
|
||||||
await foreach (var (type, item) in podlistResp.WatchAsync<V1Pod, V1PodList>().ConfigureAwait(false))
|
|
||||||
{
|
|
||||||
Console.WriteLine("==on watch event==");
|
|
||||||
Console.WriteLine(type);
|
|
||||||
Console.WriteLine(item.Metadata.Name);
|
|
||||||
Console.WriteLine("==on watch event==");
|
|
||||||
}
|
|
||||||
|
|
||||||
// uncomment if you prefer callback api
|
|
||||||
// WatchUsingCallback(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma warning disable IDE0051 // Remove unused private members
|
|
||||||
private static void WatchUsingCallback(IKubernetes client)
|
|
||||||
#pragma warning restore IDE0051 // Remove unused private members
|
|
||||||
{
|
|
||||||
var podlistResp = client.CoreV1.ListNamespacedPodWithHttpMessagesAsync("default", watch: true);
|
|
||||||
using (podlistResp.Watch<V1Pod, V1PodList>((type, item) =>
|
|
||||||
{
|
|
||||||
Console.WriteLine("==on watch event==");
|
|
||||||
Console.WriteLine(type);
|
|
||||||
Console.WriteLine(item.Metadata.Name);
|
|
||||||
Console.WriteLine("==on watch event==");
|
|
||||||
}))
|
|
||||||
{
|
|
||||||
Console.WriteLine("press ctrl + c to stop watching");
|
|
||||||
|
|
||||||
var ctrlc = new ManualResetEventSlim(false);
|
|
||||||
Console.CancelKeyPress += (sender, eventArgs) => ctrlc.Set();
|
|
||||||
ctrlc.Wait();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,22 +10,23 @@ namespace webApiDependencyInjection.Controllers
|
|||||||
private readonly IKubernetes kubernetesClient;
|
private readonly IKubernetes kubernetesClient;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Inject the kubernets class in the constructor.
|
/// Initializes a new instance of the <see cref="ExampleDependencyInjectionOnConstructorController"/> class.
|
||||||
|
/// Injects the Kubernetes client into the controller.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="kubernetesClient"></param>
|
/// <param name="kubernetesClient">The Kubernetes client to interact with the Kubernetes API.</param>
|
||||||
public ExampleDependencyInjectionOnConstructorController(IKubernetes kubernetesClient)
|
public ExampleDependencyInjectionOnConstructorController(IKubernetes kubernetesClient)
|
||||||
{
|
{
|
||||||
this.kubernetesClient = kubernetesClient;
|
this.kubernetesClient = kubernetesClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Example using the kubernetes client obtained from the constructor (this.kubernetesClient).
|
/// Retrieves the names of all pods in the default namespace using the injected Kubernetes client.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns>A collection of pod names in the default namespace.</returns>
|
||||||
[HttpGet()]
|
[HttpGet]
|
||||||
public IEnumerable<string> GetPods()
|
public IEnumerable<string> GetPods()
|
||||||
{
|
{
|
||||||
// Read the list of pods contained in default namespace
|
// Read the list of pods contained in the default namespace
|
||||||
var podList = this.kubernetesClient.CoreV1.ListNamespacedPod("default");
|
var podList = this.kubernetesClient.CoreV1.ListNamespacedPod("default");
|
||||||
|
|
||||||
// Return names of pods
|
// Return names of pods
|
||||||
|
|||||||
@@ -10,11 +10,13 @@ namespace webApiDependencyInjection.Controllers
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Example using the kubernetes client injected directly into the method ([FromServices] IKubernetes kubernetesClient).
|
/// Example using the kubernetes client injected directly into the method ([FromServices] IKubernetes kubernetesClient).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="kubernetes"></param>
|
/// <param name="kubernetesClient">The Kubernetes client instance injected via dependency injection.</param>
|
||||||
/// <returns></returns>
|
/// <returns>A collection of pod names in the default namespace.</returns>
|
||||||
[HttpGet()]
|
[HttpGet]
|
||||||
public IEnumerable<string> GetPods([FromServices] IKubernetes kubernetesClient)
|
public IEnumerable<string> GetPods([FromServices] IKubernetes kubernetesClient)
|
||||||
{
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(kubernetesClient);
|
||||||
|
|
||||||
// Read the list of pods contained in default namespace
|
// Read the list of pods contained in default namespace
|
||||||
var podList = kubernetesClient.CoreV1.ListNamespacedPod("default");
|
var podList = kubernetesClient.CoreV1.ListNamespacedPod("default");
|
||||||
|
|
||||||
|
|||||||
@@ -2,27 +2,17 @@ using k8s;
|
|||||||
using k8s.Models;
|
using k8s.Models;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace yaml
|
var typeMap = new Dictionary<string, Type>
|
||||||
{
|
{
|
||||||
internal class Program
|
{ "v1/Pod", typeof(V1Pod) },
|
||||||
{
|
{ "v1/Service", typeof(V1Service) },
|
||||||
private static async Task Main(string[] args)
|
{ "apps/v1/Deployment", typeof(V1Deployment) },
|
||||||
{
|
};
|
||||||
var typeMap = new Dictionary<string, Type>
|
|
||||||
{
|
|
||||||
{ "v1/Pod", typeof(V1Pod) },
|
|
||||||
{ "v1/Service", typeof(V1Service) },
|
|
||||||
{ "apps/v1/Deployment", typeof(V1Deployment) },
|
|
||||||
};
|
|
||||||
|
|
||||||
var objects = await KubernetesYaml.LoadAllFromFileAsync(args[0], typeMap).ConfigureAwait(false);
|
var objects = await KubernetesYaml.LoadAllFromFileAsync(args[0], typeMap).ConfigureAwait(false);
|
||||||
|
|
||||||
foreach (var obj in objects)
|
foreach (var obj in objects)
|
||||||
{
|
{
|
||||||
Console.WriteLine(obj);
|
Console.WriteLine(obj);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user