From d0c17ee2d5883aaf80edf0de67052cd721855eff Mon Sep 17 00:00:00 2001 From: Brendan Burns Date: Fri, 23 Mar 2018 22:07:51 -0700 Subject: [PATCH] Add an attach example. Fix the attach code. (#116) --- examples/attach/Attach.cs | 40 ++++++++++++++++ examples/attach/attach.csproj | 13 ++++++ examples/exec/Exec.cs | 68 ++++++++++++++-------------- src/Kubernetes.WebSocket.cs | 2 +- tests/Kubernetes.WebSockets.Tests.cs | 2 +- 5 files changed, 89 insertions(+), 36 deletions(-) create mode 100755 examples/attach/Attach.cs create mode 100755 examples/attach/attach.csproj diff --git a/examples/attach/Attach.cs b/examples/attach/Attach.cs new file mode 100755 index 0000000..301fe50 --- /dev/null +++ b/examples/attach/Attach.cs @@ -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); + } + } + } +} diff --git a/examples/attach/attach.csproj b/examples/attach/attach.csproj new file mode 100755 index 0000000..4ae8b99 --- /dev/null +++ b/examples/attach/attach.csproj @@ -0,0 +1,13 @@ + + + + + + + + Exe + netcoreapp2.1 + 7.1 + + + diff --git a/examples/exec/Exec.cs b/examples/exec/Exec.cs index 7bcf239..61394aa 100755 --- a/examples/exec/Exec.cs +++ b/examples/exec/Exec.cs @@ -1,34 +1,34 @@ -using System; -using System.Threading.Tasks; -using k8s; -using k8s.Models; - -namespace exec -{ - internal class Exec - { - private static async Task Main(string[] args) - { - var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); - IKubernetes client = new Kubernetes(config); - Console.WriteLine("Starting Request!"); - - var list = client.ListNamespacedPod("default"); - var pod = list.Items[0]; - await ExecInPod(client, 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 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); - } - } -} +using System; +using System.Threading.Tasks; +using k8s; +using k8s.Models; + +namespace exec +{ + internal class Exec + { + private static async Task Main(string[] args) + { + var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); + IKubernetes client = new Kubernetes(config); + Console.WriteLine("Starting Request!"); + + var list = client.ListNamespacedPod("default"); + var pod = list.Items[0]; + await ExecInPod(client, 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 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); + } + } +} diff --git a/src/Kubernetes.WebSocket.cs b/src/Kubernetes.WebSocket.cs index 0e51ed7e..b3fc877 100644 --- a/src/Kubernetes.WebSocket.cs +++ b/src/Kubernetes.WebSocket.cs @@ -176,7 +176,7 @@ namespace k8s 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 { diff --git a/tests/Kubernetes.WebSockets.Tests.cs b/tests/Kubernetes.WebSockets.Tests.cs index fd23c3b..48ce080 100644 --- a/tests/Kubernetes.WebSockets.Tests.cs +++ b/tests/Kubernetes.WebSockets.Tests.cs @@ -136,7 +136,7 @@ namespace k8s.tests }; 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.Equal(expectedHeaders, mockWebSocketBuilder.RequestHeaders); // Did we use the expected headers }