using Microsoft.AspNetCore.Mvc;
using System;
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
{
///
/// Initializes a new instance of the class.
/// 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)
{
WebSocketTestAdapter = webSocketTestAdapter ?? throw new ArgumentNullException(nameof(webSocketTestAdapter));
}
///
/// The adapter used to capture sockets accepted by the test server and provide them to the calling test.
///
private WebSocketTestAdapter WebSocketTestAdapter { get; }
///
/// Mock Kubernetes API: exec-in-pod.
///
///
/// The target pod's containing namespace.
///
///
/// The target pod's name.
///
/// A representing the asynchronous operation.
[Route("namespaces/{kubeNamespace}/pods/{podName}/exec")]
public async Task Exec(string kubeNamespace, string podName)
{
if (!HttpContext.WebSockets.IsWebSocketRequest)
{
return BadRequest("Exec requires WebSockets");
}
var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync(
WebSocketProtocol.ChannelWebSocketProtocol).ConfigureAwait(false);
WebSocketTestAdapter.AcceptedPodExecV1Connection.AcceptServerSocket(webSocket);
await WebSocketTestAdapter.TestCompleted.ConfigureAwait(false);
return Ok();
}
}
}