-
Notifications
You must be signed in to change notification settings - Fork 33
/
ResourceClientWebSocketExtensions.cs
145 lines (138 loc) · 6.21 KB
/
ResourceClientWebSocketExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using HTTPlease;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace KubeClient
{
using ResourceClients;
using Extensions.WebSockets;
using System.Net.WebSockets;
/// <summary>
/// WebSocket-related extension methods for Kubernetes resource clients.
/// </summary>
public static class ResourceClientWebSocketExtensions
{
/// <summary>
/// Start a new process in a Pod's container and open a multiplexed connection to its STDIN / STDOUT / STDERR.
/// </summary>
/// <param name="podClient">
/// The <see cref="PodClientV1"/>.
/// </param>
/// <param name="podName">
/// The name of the target pod.
/// </param>
/// <param name="command">
/// The command to execute (command-line arguments not supported yet).
/// </param>
/// <param name="stdin">
/// Create an output stream connected to the target container's STDIN?
/// </param>
/// <param name="stdout">
/// Create an output stream connected to the target container's STDOUT?
/// </param>
/// <param name="stderr">
/// Create an output stream connected to the target container's STDERR?
/// </param>
/// <param name="tty">
/// Attach a TTY to the process?
/// </param>
/// <param name="container">
/// The name of the target container within the pod.
///
/// Optional if the pod only has a single container.
/// </param>
/// <param name="kubeNamespace">
/// The name of the target Kubernetes namespace that contains the pod.
///
/// If not specified, the <see cref="KubeApiClient.DefaultNamespace"/> is used.
/// </param>
/// <param name="cancellation">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// A <see cref="K8sMultiplexer"/> that provides access to the input and output streams.
/// </returns>
public static Task<K8sMultiplexer> ExecAndConnect(this IPodClientV1 podClient, string podName, string command, bool stdin = false, bool stdout = true, bool stderr = false, bool tty = false, string container = null, string kubeNamespace = null, CancellationToken cancellation = default)
{
if (podClient == null)
throw new ArgumentNullException(nameof(podClient));
byte[] outputStreamIndexes = stdin ? new byte[1] { 0 } : new byte[0];
byte[] inputStreamIndexes;
if (stdout && stderr)
inputStreamIndexes = new byte[2] { 1, 2 };
else if (stdout)
inputStreamIndexes = new byte[1] { 1 };
else if (stderr)
inputStreamIndexes = new byte[1] { 2 };
else if (!stdin)
throw new InvalidOperationException("Must specify at least one of STDIN, STDOUT, or STDERR.");
else
inputStreamIndexes = new byte[0];
return podClient.ExecAndConnectRaw(podName, command, stdin, stdout, stderr, tty, container, kubeNamespace, cancellation)
.Multiplexed(inputStreamIndexes, outputStreamIndexes,
loggerFactory: podClient.KubeClient.LoggerFactory
);
}
/// <summary>
/// Start a new process in a Pod's container and open a WebSocket connection to its STDIN / STDOUT / STDERR.
/// </summary>
/// <param name="podClient">
/// The <see cref="PodClientV1"/>.
/// </param>
/// <param name="podName">
/// The name of the target pod.
/// </param>
/// <param name="command">
/// The command to execute (command-line arguments not supported yet).
/// </param>
/// <param name="stdin">
/// Create an output stream connected to the target container's STDIN?
/// </param>
/// <param name="stdout">
/// Create an output stream connected to the target container's STDOUT?
/// </param>
/// <param name="stderr">
/// Create an output stream connected to the target container's STDERR?
/// </param>
/// <param name="tty">
/// Attach a TTY to the process?
/// </param>
/// <param name="container">
/// The name of the target container within the pod.
///
/// Optional if the pod only has a single container.
/// </param>
/// <param name="kubeNamespace">
/// The name of the target Kubernetes namespace that contains the pod.
///
/// If not specified, the <see cref="KubeApiClient.DefaultNamespace"/> is used.
/// </param>
/// <param name="cancellation">
/// An optional <see cref="CancellationToken"/> that can be used to cancel the request.
/// </param>
/// <returns>
/// The connected <see cref="WebSocket"/>.
/// </returns>
public static async Task<WebSocket> ExecAndConnectRaw(this IPodClientV1 podClient, string podName, string command, bool stdin = false, bool stdout = true, bool stderr = false, bool tty = false, string container = null, string kubeNamespace = null, CancellationToken cancellation = default)
{
if (podClient == null)
throw new ArgumentNullException(nameof(podClient));
var uriTemplateParameters = new
{
PodName = podName,
Command = command,
StdIn = stdin,
StdOut = stdout,
StdErr = stderr,
TTY = tty,
Container = container,
KubeNamespace = kubeNamespace ?? podClient.KubeClient.DefaultNamespace
};
return await podClient.KubeClient.ConnectWebSocket(
"api/v1/namespaces/{KubeNamespace}/pods/{PodName}/exec?stdin={StdIn?}&stdout={StdOut?}&stderr={StdErr?}&tty={TTY?}&command={Command}&container={Container?}",
uriTemplateParameters,
cancellation
);
}
}
}