using Microsoft.AspNetCore.Mvc;
using System;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
namespace k8s.Tests.Mock.Server.Controllers
{
///
/// Controller for the mock Kubernetes exec-in-pod API.
///
[Route("api/v1")]
public class PodExecController
: Controller
{
///
/// Create a new .
///
///
/// The adapter used to capture sockets accepted by the test server and provide them to the calling test.
///
public PodExecController(WebSocketTestAdapter webSocketTestAdapter)
{
if (webSocketTestAdapter == null)
throw new ArgumentNullException(nameof(webSocketTestAdapter));
WebSocketTestAdapter = webSocketTestAdapter;
}
///
/// The adapter used to capture sockets accepted by the test server and provide them to the calling test.
///
WebSocketTestAdapter WebSocketTestAdapter { get; }
///
/// Mock Kubernetes API: exec-in-pod.
///
///
/// The target pod's containing namespace.
///
///
/// The target pod's name.
///
[Route("namespaces/{kubeNamespace}/pods/{podName}/exec")]
public async Task Exec(string kubeNamespace, string podName)
{
if (!HttpContext.WebSockets.IsWebSocketRequest)
return BadRequest("Exec requires WebSockets");
WebSocket webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(
subProtocol: K8sProtocol.ChannelV1
);
WebSocketTestAdapter.AcceptedPodExecV1Connection.AcceptServerSocket(webSocket);
await WebSocketTestAdapter.TestCompleted;
return Ok();
}
}
}