Skip to content

Commit d0c17ee

Browse files
Add an attach example. Fix the attach code. (#116)
1 parent fcc08bc commit d0c17ee

File tree

5 files changed

+89
-36
lines changed

5 files changed

+89
-36
lines changed

examples/attach/Attach.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using k8s;
4+
using k8s.Models;
5+
using Microsoft.Rest;
6+
7+
namespace attach
8+
{
9+
internal class Attach
10+
{
11+
private static async Task Main(string[] args)
12+
{
13+
ServiceClientTracing.IsEnabled = true;
14+
15+
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
16+
IKubernetes client = new Kubernetes(config);
17+
Console.WriteLine("Starting Request!");
18+
19+
var list = client.ListNamespacedPod("default");
20+
var pod = list.Items[0];
21+
await AttachToPod(client, pod);
22+
}
23+
24+
private async static Task AttachToPod(IKubernetes client, V1Pod pod) {
25+
var webSocket = await client.WebSocketNamespacedPodAttachAsync(pod.Metadata.Name, "default", pod.Spec.Containers[0].Name);
26+
27+
var demux = new StreamDemuxer(webSocket);
28+
demux.Start();
29+
30+
var buff = new byte[4096];
31+
var stream = demux.GetStream(1, 1);
32+
while (true)
33+
{
34+
var read = stream.Read(buff, 0, 4096);
35+
var str = System.Text.Encoding.Default.GetString(buff);
36+
Console.WriteLine(str);
37+
}
38+
}
39+
}
40+
}

examples/attach/attach.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.1</TargetFramework>
10+
<LangVersion>7.1</LangVersion>
11+
</PropertyGroup>
12+
13+
</Project>

examples/exec/Exec.cs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
using System;
2-
using System.Threading.Tasks;
3-
using k8s;
4-
using k8s.Models;
5-
6-
namespace exec
7-
{
8-
internal class Exec
9-
{
10-
private static async Task Main(string[] args)
11-
{
12-
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
13-
IKubernetes client = new Kubernetes(config);
14-
Console.WriteLine("Starting Request!");
15-
16-
var list = client.ListNamespacedPod("default");
17-
var pod = list.Items[0];
18-
await ExecInPod(client, pod);
19-
}
20-
21-
private async static Task ExecInPod(IKubernetes client, V1Pod pod) {
22-
var webSocket = await client.WebSocketNamespacedPodExecAsync(pod.Metadata.Name, "default", "ls", pod.Spec.Containers[0].Name);
23-
24-
var demux = new StreamDemuxer(webSocket);
25-
demux.Start();
26-
27-
var buff = new byte[4096];
28-
var stream = demux.GetStream(1, 1);
29-
var read = stream.Read(buff, 0, 4096);
30-
var str = System.Text.Encoding.Default.GetString(buff);
31-
Console.WriteLine(str);
32-
}
33-
}
34-
}
1+
using System;
2+
using System.Threading.Tasks;
3+
using k8s;
4+
using k8s.Models;
5+
6+
namespace exec
7+
{
8+
internal class Exec
9+
{
10+
private static async Task Main(string[] args)
11+
{
12+
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();
13+
IKubernetes client = new Kubernetes(config);
14+
Console.WriteLine("Starting Request!");
15+
16+
var list = client.ListNamespacedPod("default");
17+
var pod = list.Items[0];
18+
await ExecInPod(client, pod);
19+
}
20+
21+
private async static Task ExecInPod(IKubernetes client, V1Pod pod) {
22+
var webSocket = await client.WebSocketNamespacedPodExecAsync(pod.Metadata.Name, "default", "ls", pod.Spec.Containers[0].Name);
23+
24+
var demux = new StreamDemuxer(webSocket);
25+
demux.Start();
26+
27+
var buff = new byte[4096];
28+
var stream = demux.GetStream(1, 1);
29+
var read = stream.Read(buff, 0, 4096);
30+
var str = System.Text.Encoding.Default.GetString(buff);
31+
Console.WriteLine(str);
32+
}
33+
}
34+
}

src/Kubernetes.WebSocket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public partial class Kubernetes
176176
uriBuilder.Path += "/";
177177
}
178178

179-
uriBuilder.Path += $"api/v1/namespaces/{@namespace}/pods/{name}/portforward";
179+
uriBuilder.Path += $"api/v1/namespaces/{@namespace}/pods/{name}/attach";
180180

181181
uriBuilder.Query = QueryHelpers.AddQueryString(string.Empty, new Dictionary<string, string>
182182
{

tests/Kubernetes.WebSockets.Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public async Task WebSocketNamespacedPodAttachAsync()
136136
};
137137

138138
Assert.Equal(mockWebSocketBuilder.PublicWebSocket, webSocket); // Did the method return the correct web socket?
139-
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?
139+
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?
140140
Assert.Empty(mockWebSocketBuilder.Certificates); // No certificates were used in this test
141141
Assert.Equal(expectedHeaders, mockWebSocketBuilder.RequestHeaders); // Did we use the expected headers
142142
}

0 commit comments

Comments
 (0)