Search in sources :

Example 1 with WebSocketStreamHandler

use of io.kubernetes.client.util.WebSocketStreamHandler in project java by kubernetes-client.

the class Attach method attach.

/**
 * Attach to a running AttachResult in a container. If there are multiple containers in the pod,
 * uses the first container in the Pod.
 *
 * @param namespace The namespace of the Pod
 * @param name The name of the Pod
 * @param container The container in the Pod where the command is run.
 * @param stdin If true, pass a stdin stream into the container.
 * @param tty If true, stdin is a TTY (only applies if stdin is true)
 */
public AttachResult attach(String namespace, String name, String container, boolean stdin, boolean tty) throws ApiException, IOException {
    String path = makePath(namespace, name, container, stdin, tty);
    WebSocketStreamHandler handler = new WebSocketStreamHandler();
    AttachResult result = new AttachResult(handler);
    WebSockets.stream(path, "GET", apiClient, handler);
    return result;
}
Also used : WebSocketStreamHandler(io.kubernetes.client.util.WebSocketStreamHandler)

Example 2 with WebSocketStreamHandler

use of io.kubernetes.client.util.WebSocketStreamHandler in project java by kubernetes-client.

the class Exec method exec.

/**
 * Execute a command in a container. If there are multiple containers in the pod, uses the first
 * container in the Pod.
 *
 * @param namespace The namespace of the Pod
 * @param name The name of the Pod
 * @param command The command to run
 * @param container The container in the Pod where the command is run.
 * @param stdin If true, pass a stdin stream into the container.
 * @param tty If true, stdin is a TTY (only applies if stdin is true)
 */
public Process exec(String namespace, String name, String[] command, String container, boolean stdin, boolean tty) throws ApiException, IOException {
    String path = makePath(namespace, name, command, container, stdin, tty);
    ExecProcess exec = new ExecProcess();
    WebSocketStreamHandler handler = exec.getHandler();
    WebSockets.stream(path, "GET", apiClient, handler);
    return exec;
}
Also used : WebSocketStreamHandler(io.kubernetes.client.util.WebSocketStreamHandler)

Example 3 with WebSocketStreamHandler

use of io.kubernetes.client.util.WebSocketStreamHandler in project java by kubernetes-client.

the class PortForwardTest method testBrokenPortPassing.

@Test
public void testBrokenPortPassing() throws IOException, InterruptedException {
    WebSocketStreamHandler handler = new WebSocketStreamHandler();
    List<Integer> ports = new ArrayList<>();
    ports.add(80);
    final PortForwardResult result = new PortForwardResult(handler, ports);
    String msgData = "this is a test datum";
    handler.open("wss", null);
    handler.bytesMessage(makeStream(new byte[] { 66 }, msgData.getBytes(StandardCharsets.UTF_8)));
    final Object block = new Object();
    Thread t = new Thread(() -> {
        try {
            result.init();
        } catch (IOException ex) {
            thrownException = ex;
        } finally {
            synchronized (block) {
                block.notifyAll();
            }
        }
    });
    synchronized (block) {
        t.start();
        Thread.sleep(2000);
        handler.close();
        block.wait();
    }
    assertNotNull(thrownException);
    assertTrue(thrownException instanceof IOException);
}
Also used : PortForwardResult(io.kubernetes.client.PortForward.PortForwardResult) WebSocketStreamHandler(io.kubernetes.client.util.WebSocketStreamHandler) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with WebSocketStreamHandler

use of io.kubernetes.client.util.WebSocketStreamHandler in project java by kubernetes-client.

the class WebsocketStreamHandlerTest method testHandlerSendingLargeData.

@Test
public void testHandlerSendingLargeData() throws IOException {
    int testStreamId = 0;
    byte testData = 1;
    byte[] testDatas = // first byte stands for stream id,
    new byte[] { (byte) testStreamId, testData, testData };
    ByteArrayInputStream testBytesInputStream = new ByteArrayInputStream(testDatas);
    WebSocketStreamHandler handler = new WebSocketStreamHandler();
    MockWebSocket mockWebSocket = new MockWebSocket();
    handler.open(testProtocol, mockWebSocket);
    OutputStream outputStream = handler.getOutputStream(testStreamId);
    byte[] bytes = new byte[20 * 1024 * 1024];
    byte[] output = new byte[bytes.length + 2];
    for (int i = 0; i < bytes.length; i++) {
        bytes[i] = (byte) i;
    }
    // First stream header
    output[0] = 0;
    for (int i = 0; i < 15 * 1024 * 1024; i++) {
        output[i + 1] = bytes[i];
    }
    // Second stream header
    output[15 * 1024 * 1024 + 1] = 0;
    for (int i = 15 * 1024 * 1024; i < bytes.length; i++) {
        output[i + 2] = bytes[i];
    }
    outputStream.write(bytes);
    outputStream.flush();
    assertArrayEquals(output, mockWebSocket.data);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WebSocketStreamHandler(io.kubernetes.client.util.WebSocketStreamHandler) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 5 with WebSocketStreamHandler

use of io.kubernetes.client.util.WebSocketStreamHandler in project java by kubernetes-client.

the class WebsocketStreamHandlerTest method testHandlerSendingData.

@Test
public void testHandlerSendingData() throws IOException {
    int testStreamId = 0;
    byte testData = 1;
    byte[] testDatas = // first byte stands for stream id,
    new byte[] { (byte) testStreamId, testData, testData };
    ByteArrayInputStream testBytesInputStream = new ByteArrayInputStream(testDatas);
    WebSocketStreamHandler handler = new WebSocketStreamHandler();
    MockWebSocket mockWebSocket = new MockWebSocket();
    handler.open(testProtocol, mockWebSocket);
    OutputStream outputStream = handler.getOutputStream(testStreamId);
    byte[] bytes = "This is a test string".getBytes("UTF-8");
    byte[] output = new byte[bytes.length + 1];
    output[0] = 0;
    for (int i = 0; i < bytes.length; i++) {
        output[i + 1] = bytes[i];
    }
    outputStream.write(bytes);
    outputStream.flush();
    assertArrayEquals(output, mockWebSocket.data);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WebSocketStreamHandler(io.kubernetes.client.util.WebSocketStreamHandler) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Aggregations

WebSocketStreamHandler (io.kubernetes.client.util.WebSocketStreamHandler)8 Test (org.junit.Test)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 PortForwardResult (io.kubernetes.client.PortForward.PortForwardResult)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ApiException (io.kubernetes.client.openapi.ApiException)1 Pair (io.kubernetes.client.openapi.Pair)1