* Improve SSL customisation for WebSockets kubernetes-client/csharp#102 * First test for exec-in-pod over WebSockets. Also, implement basic mock server for testing WebSockets. kubernetes-client/csharp#102 * Attempt to handle raciness of Watcher tests. kubernetes-client/csharp#102 * Attempt to handle raciness of ByteBuffer test. kubernetes-client/csharp#102
66 lines
2.3 KiB
C#
66 lines
2.3 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net.WebSockets;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace k8s.Tests.Mock.Server.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Controller for the mock Kubernetes pod-port-forward API.
|
|
/// </summary>
|
|
[Route("api/v1")]
|
|
public class PodPortForwardController
|
|
: Controller
|
|
{
|
|
/// <summary>
|
|
/// Create a new <see cref="PodPortForwardController"/>.
|
|
/// </summary>
|
|
/// <param name="webSocketTestAdapter">
|
|
/// The adapter used to capture sockets accepted by the test server and provide them to the calling test.
|
|
/// </param>
|
|
public PodPortForwardController(WebSocketTestAdapter webSocketTestAdapter)
|
|
{
|
|
if (webSocketTestAdapter == null)
|
|
throw new ArgumentNullException(nameof(webSocketTestAdapter));
|
|
|
|
WebSocketTestAdapter = webSocketTestAdapter;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The adapter used to capture sockets accepted by the test server and provide them to the calling test.
|
|
/// </summary>
|
|
WebSocketTestAdapter WebSocketTestAdapter { get; }
|
|
|
|
/// <summary>
|
|
/// Mock Kubernetes API: port-forward for pod.
|
|
/// </summary>
|
|
/// <param name="kubeNamespace">
|
|
/// The target pod's containing namespace.
|
|
/// </param>
|
|
/// <param name="podName">
|
|
/// The target pod's name.
|
|
/// </param>
|
|
/// <param name="ports">
|
|
/// The port(s) to forward to the pod.
|
|
/// </param>
|
|
[Route("namespaces/{kubeNamespace}/pods/{podName}/portforward")]
|
|
public async Task<IActionResult> Exec(string kubeNamespace, string podName, IEnumerable<string> ports)
|
|
{
|
|
if (!HttpContext.WebSockets.IsWebSocketRequest)
|
|
return BadRequest("PortForward requires WebSockets");
|
|
|
|
WebSocket webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(
|
|
subProtocol: K8sProtocol.ChannelV1
|
|
);
|
|
|
|
WebSocketTestAdapter.AcceptedPodPortForwardV1Connection.AcceptServerSocket(webSocket);
|
|
|
|
await WebSocketTestAdapter.TestCompleted;
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
}
|