|
| 1 | +using Microsoft.AspNetCore.WebUtilities; |
| 2 | +using Microsoft.Rest; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Net.WebSockets; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | + |
| 10 | +namespace k8s |
| 11 | +{ |
| 12 | + public partial class Kubernetes |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Gets a function which returns a <see cref="WebSocketBuilder"/> which <see cref="Kubernetes"/> will use to |
| 16 | + /// create a new <see cref="WebSocket"/> connection to the Kubernetes cluster. |
| 17 | + /// </summary> |
| 18 | + public Func<WebSocketBuilder> CreateWebSocketBuilder { get; set; } = () => new WebSocketBuilder(); |
| 19 | + |
| 20 | + /// <inheritdoc/> |
| 21 | + public Task<WebSocket> WebSocketNamespacedPodExecAsync(string name, string @namespace = "default", string command = "/bin/sh", string container = null, bool stderr = true, bool stdin = true, bool stdout = true, bool tty = true, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)) |
| 22 | + { |
| 23 | + if (name == null) |
| 24 | + { |
| 25 | + throw new ArgumentNullException(nameof(name)); |
| 26 | + } |
| 27 | + |
| 28 | + if (@namespace == null) |
| 29 | + { |
| 30 | + throw new ArgumentNullException(nameof(@namespace)); |
| 31 | + } |
| 32 | + |
| 33 | + if (command == null) |
| 34 | + { |
| 35 | + throw new ArgumentNullException(nameof(command)); |
| 36 | + } |
| 37 | + |
| 38 | + // Tracing |
| 39 | + bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 40 | + string _invocationId = null; |
| 41 | + if (_shouldTrace) |
| 42 | + { |
| 43 | + _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 44 | + Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 45 | + tracingParameters.Add("command", command); |
| 46 | + tracingParameters.Add("container", container); |
| 47 | + tracingParameters.Add("name", name); |
| 48 | + tracingParameters.Add("namespace", @namespace); |
| 49 | + tracingParameters.Add("stderr", stderr); |
| 50 | + tracingParameters.Add("stdin", stdin); |
| 51 | + tracingParameters.Add("stdout", stdout); |
| 52 | + tracingParameters.Add("tty", tty); |
| 53 | + tracingParameters.Add("cancellationToken", cancellationToken); |
| 54 | + ServiceClientTracing.Enter(_invocationId, this, nameof(WebSocketNamespacedPodExecAsync), tracingParameters); |
| 55 | + } |
| 56 | + |
| 57 | + // Construct URL |
| 58 | + var uriBuilder = new UriBuilder(BaseUri); |
| 59 | + uriBuilder.Scheme = BaseUri.Scheme == "https" ? "wss" : "ws"; |
| 60 | + |
| 61 | + if (!uriBuilder.Path.EndsWith("/")) |
| 62 | + { |
| 63 | + uriBuilder.Path += "/"; |
| 64 | + } |
| 65 | + |
| 66 | + uriBuilder.Path += $"api/v1/namespaces/{@namespace}/pods/{name}/exec"; |
| 67 | + |
| 68 | + |
| 69 | + uriBuilder.Query = QueryHelpers.AddQueryString(string.Empty, new Dictionary<string, string> |
| 70 | + { |
| 71 | + { "command", command}, |
| 72 | + { "container", container}, |
| 73 | + { "stderr", stderr ? "1": "0"}, |
| 74 | + { "stdin", stdin ? "1": "0"}, |
| 75 | + { "stdout", stdout ? "1": "0"}, |
| 76 | + { "tty", tty ? "1": "0"} |
| 77 | + }); |
| 78 | + |
| 79 | + return this.StreamConnectAsync(uriBuilder.Uri, _invocationId, customHeaders, cancellationToken); |
| 80 | + } |
| 81 | + |
| 82 | + /// <inheritdoc/> |
| 83 | + public Task<WebSocket> WebSocketNamespacedPodPortForwardAsync(string name, string @namespace, IEnumerable<int> ports, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)) |
| 84 | + { |
| 85 | + if (name == null) |
| 86 | + { |
| 87 | + throw new ArgumentNullException(nameof(name)); |
| 88 | + } |
| 89 | + |
| 90 | + if (@namespace == null) |
| 91 | + { |
| 92 | + throw new ArgumentNullException(nameof(@namespace)); |
| 93 | + } |
| 94 | + |
| 95 | + if (ports == null) |
| 96 | + { |
| 97 | + throw new ArgumentNullException(nameof(ports)); |
| 98 | + } |
| 99 | + |
| 100 | + // Tracing |
| 101 | + bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 102 | + string _invocationId = null; |
| 103 | + if (_shouldTrace) |
| 104 | + { |
| 105 | + _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 106 | + Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 107 | + tracingParameters.Add("name", name); |
| 108 | + tracingParameters.Add("@namespace", @namespace); |
| 109 | + tracingParameters.Add("ports", ports); |
| 110 | + tracingParameters.Add("cancellationToken", cancellationToken); |
| 111 | + ServiceClientTracing.Enter(_invocationId, this, nameof(WebSocketNamespacedPodPortForwardAsync), tracingParameters); |
| 112 | + } |
| 113 | + |
| 114 | + // Construct URL |
| 115 | + var uriBuilder = new UriBuilder(this.BaseUri); |
| 116 | + uriBuilder.Scheme = this.BaseUri.Scheme == "https" ? "wss" : "ws"; |
| 117 | + |
| 118 | + if (!uriBuilder.Path.EndsWith("/")) |
| 119 | + { |
| 120 | + uriBuilder.Path += "/"; |
| 121 | + } |
| 122 | + |
| 123 | + uriBuilder.Path += $"api/v1/namespaces/{@namespace}/pods/{name}/portforward"; |
| 124 | + |
| 125 | + foreach (var port in ports) |
| 126 | + { |
| 127 | + uriBuilder.Query += $"ports={port}&"; |
| 128 | + } |
| 129 | + |
| 130 | + return StreamConnectAsync(uriBuilder.Uri, _invocationId, customHeaders, cancellationToken); |
| 131 | + } |
| 132 | + |
| 133 | + /// <inheritdoc/> |
| 134 | + public Task<WebSocket> WebSocketNamespacedPodAttachAsync(string name, string @namespace, string container = default(string), bool stderr = true, bool stdin = false, bool stdout = true, bool tty = false, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)) |
| 135 | + { |
| 136 | + if (name == null) |
| 137 | + { |
| 138 | + throw new ArgumentNullException(nameof(name)); |
| 139 | + } |
| 140 | + |
| 141 | + if (@namespace == null) |
| 142 | + { |
| 143 | + throw new ArgumentNullException(nameof(@namespace)); |
| 144 | + } |
| 145 | + |
| 146 | + // Tracing |
| 147 | + bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 148 | + string _invocationId = null; |
| 149 | + if (_shouldTrace) |
| 150 | + { |
| 151 | + _invocationId = ServiceClientTracing.NextInvocationId.ToString(); |
| 152 | + Dictionary<string, object> tracingParameters = new Dictionary<string, object>(); |
| 153 | + tracingParameters.Add("container", container); |
| 154 | + tracingParameters.Add("name", name); |
| 155 | + tracingParameters.Add("namespace", @namespace); |
| 156 | + tracingParameters.Add("stderr", stderr); |
| 157 | + tracingParameters.Add("stdin", stdin); |
| 158 | + tracingParameters.Add("stdout", stdout); |
| 159 | + tracingParameters.Add("tty", tty); |
| 160 | + tracingParameters.Add("cancellationToken", cancellationToken); |
| 161 | + ServiceClientTracing.Enter(_invocationId, this, nameof(WebSocketNamespacedPodAttachAsync), tracingParameters); |
| 162 | + } |
| 163 | + |
| 164 | + // Construct URL |
| 165 | + var uriBuilder = new UriBuilder(this.BaseUri); |
| 166 | + uriBuilder.Scheme = this.BaseUri.Scheme == "https" ? "wss" : "ws"; |
| 167 | + |
| 168 | + if (!uriBuilder.Path.EndsWith("/")) |
| 169 | + { |
| 170 | + uriBuilder.Path += "/"; |
| 171 | + } |
| 172 | + |
| 173 | + uriBuilder.Path += $"api/v1/namespaces/{@namespace}/pods/{name}/portforward"; |
| 174 | + |
| 175 | + uriBuilder.Query = QueryHelpers.AddQueryString(string.Empty, new Dictionary<string, string> |
| 176 | + { |
| 177 | + { "container", container}, |
| 178 | + { "stderr", stderr ? "1": "0"}, |
| 179 | + { "stdin", stdin ? "1": "0"}, |
| 180 | + { "stdout", stdout ? "1": "0"}, |
| 181 | + { "tty", tty ? "1": "0"} |
| 182 | + }); |
| 183 | + |
| 184 | + return StreamConnectAsync(uriBuilder.Uri, _invocationId, customHeaders, cancellationToken); |
| 185 | + } |
| 186 | + |
| 187 | + protected async Task<WebSocket> StreamConnectAsync(Uri uri, string invocationId = null, Dictionary<string, List<string>> customHeaders = null, CancellationToken cancellationToken = default(CancellationToken)) |
| 188 | + { |
| 189 | + bool _shouldTrace = ServiceClientTracing.IsEnabled; |
| 190 | + |
| 191 | + // Create WebSocket transport objects |
| 192 | + WebSocketBuilder webSocketBuilder = this.CreateWebSocketBuilder(); |
| 193 | + |
| 194 | + // Set Headers |
| 195 | + if (customHeaders != null) |
| 196 | + { |
| 197 | + foreach (var _header in customHeaders) |
| 198 | + { |
| 199 | + webSocketBuilder.SetRequestHeader(_header.Key, string.Join(" ", _header.Value)); |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + // Set Credentials |
| 204 | + foreach (var cert in this.HttpClientHandler.ClientCertificates) |
| 205 | + { |
| 206 | + webSocketBuilder.AddClientCertificate(cert); |
| 207 | + } |
| 208 | + |
| 209 | + HttpRequestMessage message = new HttpRequestMessage(); |
| 210 | + await this.Credentials.ProcessHttpRequestAsync(message, cancellationToken); |
| 211 | + |
| 212 | + foreach (var _header in message.Headers) |
| 213 | + { |
| 214 | + webSocketBuilder.SetRequestHeader(_header.Key, string.Join(" ", _header.Value)); |
| 215 | + } |
| 216 | + |
| 217 | + // Send Request |
| 218 | + cancellationToken.ThrowIfCancellationRequested(); |
| 219 | + |
| 220 | + WebSocket webSocket = null; |
| 221 | + |
| 222 | + try |
| 223 | + { |
| 224 | + webSocket = await webSocketBuilder.BuildAndConnectAsync(uri, CancellationToken.None).ConfigureAwait(false); |
| 225 | + } |
| 226 | + catch (Exception ex) |
| 227 | + { |
| 228 | + if (_shouldTrace) |
| 229 | + { |
| 230 | + ServiceClientTracing.Error(invocationId, ex); |
| 231 | + } |
| 232 | + |
| 233 | + throw; |
| 234 | + } |
| 235 | + finally |
| 236 | + { |
| 237 | + if (_shouldTrace) |
| 238 | + { |
| 239 | + ServiceClientTracing.Exit(invocationId, null); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + return webSocket; |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments