Add an attach example. Fix the attach code. (#116)

This commit is contained in:
Brendan Burns
2018-03-23 22:07:51 -07:00
committed by GitHub
parent fcc08bc457
commit d0c17ee2d5
5 changed files with 89 additions and 36 deletions

40
examples/attach/Attach.cs Executable file
View File

@@ -0,0 +1,40 @@
using System;
using System.Threading.Tasks;
using k8s;
using k8s.Models;
using Microsoft.Rest;
namespace attach
{
internal class Attach
{
private static async Task Main(string[] args)
{
ServiceClientTracing.IsEnabled = true;
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
IKubernetes client = new Kubernetes(config);
Console.WriteLine("Starting Request!");
var list = client.ListNamespacedPod("default");
var pod = list.Items[0];
await AttachToPod(client, pod);
}
private async static Task AttachToPod(IKubernetes client, V1Pod pod) {
var webSocket = await client.WebSocketNamespacedPodAttachAsync(pod.Metadata.Name, "default", pod.Spec.Containers[0].Name);
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);
}
}
}
}

13
examples/attach/attach.csproj Executable file
View File

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

View File

@@ -1,34 +1,34 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using k8s; using k8s;
using k8s.Models; using k8s.Models;
namespace exec namespace exec
{ {
internal class Exec internal class Exec
{ {
private static async Task Main(string[] args) private static async Task Main(string[] args)
{ {
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
IKubernetes client = new Kubernetes(config); IKubernetes client = new Kubernetes(config);
Console.WriteLine("Starting Request!"); Console.WriteLine("Starting Request!");
var list = client.ListNamespacedPod("default"); var list = client.ListNamespacedPod("default");
var pod = list.Items[0]; var pod = list.Items[0];
await ExecInPod(client, pod); await ExecInPod(client, pod);
} }
private async static Task ExecInPod(IKubernetes client, V1Pod pod) { private async static Task ExecInPod(IKubernetes client, V1Pod pod) {
var webSocket = await client.WebSocketNamespacedPodExecAsync(pod.Metadata.Name, "default", "ls", pod.Spec.Containers[0].Name); var webSocket = await client.WebSocketNamespacedPodExecAsync(pod.Metadata.Name, "default", "ls", pod.Spec.Containers[0].Name);
var demux = new StreamDemuxer(webSocket); var demux = new StreamDemuxer(webSocket);
demux.Start(); demux.Start();
var buff = new byte[4096]; var buff = new byte[4096];
var stream = demux.GetStream(1, 1); var stream = demux.GetStream(1, 1);
var read = stream.Read(buff, 0, 4096); var read = stream.Read(buff, 0, 4096);
var str = System.Text.Encoding.Default.GetString(buff); var str = System.Text.Encoding.Default.GetString(buff);
Console.WriteLine(str); Console.WriteLine(str);
} }
} }
} }

View File

@@ -176,7 +176,7 @@ namespace k8s
uriBuilder.Path += "/"; uriBuilder.Path += "/";
} }
uriBuilder.Path += $"api/v1/namespaces/{@namespace}/pods/{name}/portforward"; uriBuilder.Path += $"api/v1/namespaces/{@namespace}/pods/{name}/attach";
uriBuilder.Query = QueryHelpers.AddQueryString(string.Empty, new Dictionary<string, string> uriBuilder.Query = QueryHelpers.AddQueryString(string.Empty, new Dictionary<string, string>
{ {

View File

@@ -136,7 +136,7 @@ namespace k8s.tests
}; };
Assert.Equal(mockWebSocketBuilder.PublicWebSocket, webSocket); // Did the method return the correct web socket? Assert.Equal(mockWebSocketBuilder.PublicWebSocket, webSocket); // Did the method return the correct web socket?
Assert.Equal(new Uri("ws://localhost:80/api/v1/namespaces/mynamespace/pods/mypod/portforward?container=my-container&stderr=1&stdin=1&stdout=1&tty=1"), mockWebSocketBuilder.Uri); // Did we connect to the correct URL? Assert.Equal(new Uri("ws://localhost:80/api/v1/namespaces/mynamespace/pods/mypod/attach?container=my-container&stderr=1&stdin=1&stdout=1&tty=1"), mockWebSocketBuilder.Uri); // Did we connect to the correct URL?
Assert.Empty(mockWebSocketBuilder.Certificates); // No certificates were used in this test Assert.Empty(mockWebSocketBuilder.Certificates); // No certificates were used in this test
Assert.Equal(expectedHeaders, mockWebSocketBuilder.RequestHeaders); // Did we use the expected headers Assert.Equal(expectedHeaders, mockWebSocketBuilder.RequestHeaders); // Did we use the expected headers
} }