io.fabric8.kubernetes.client.LocalPortForward Java Examples

The following examples show how to use io.fabric8.kubernetes.client.LocalPortForward. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PortForwardExample.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
public static void main(String args[]) {
  String master = "https://localhost:8443";

  Config config = new ConfigBuilder().withMasterUrl(master).build();
  try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
    String namespace = "default";
    log("namespace", namespace);
    Pod pod = client.pods().inNamespace(namespace).load(PortForwardExample.class.getResourceAsStream("/portforward-example-pod.yml")).get();
    client.pods().inNamespace(namespace).create(pod);
    log("Pod created");

    int containerPort =  pod.getSpec().getContainers().get(0).getPorts().get(0).getContainerPort();
    client.pods().inNamespace(namespace).withName("testpod").waitUntilReady(10, TimeUnit.SECONDS);

    LocalPortForward portForward = client.pods().inNamespace("default").withName("testpod").portForward(containerPort, 8080);
    log("Port forwarded for 60 seconds at http://127.0.0.1:" + portForward.getLocalPort());

    log("Checking forwarded port:-");
    Response response =  new OkHttpClient().newCall(new Request.Builder().get().url("http://127.0.0.1:" + portForward.getLocalPort()).build()).execute();
    log(response.body().string());
    Thread.sleep(60 * 1000);
  } catch (Exception e) {
    log("Exception occurred: ", e.getMessage());
    e.printStackTrace();
  }
}
 
Example #2
Source File: PodTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testPortForward() throws IOException {

  server.expect().withPath("/api/v1/namespaces/test/pods/pod1/portforward?ports=123")
    .andUpgradeToWebSocket()
    .open()
    .waitFor(10).andEmit(portForwardEncode(true, "12")) // data channel info
    .waitFor(10).andEmit(portForwardEncode(false, "12")) // error channel info
    .waitFor(10).andEmit(portForwardEncode(true, "Hell"))
    .waitFor(10).andEmit(portForwardEncode(true, "o World"))
    .done()
    .once();

  KubernetesClient client = server.getClient();

  try(LocalPortForward portForward = client.pods().withName("pod1").portForward(123)) {
    int localPort = portForward.getLocalPort();
    SocketChannel channel = SocketChannel.open();
    assertTrue(channel.connect(new InetSocketAddress("localhost", localPort)));

    ByteBuffer buffer = ByteBuffer.allocate(1024);
    int read;
    do {
      read = channel.read(buffer);
    } while(read >= 0);
    buffer.flip();
    String data = ByteString.of(buffer).utf8();
    assertEquals("Hello World", data);
    assertFalse(portForward.errorOccurred());
    assertEquals(portForward.getClientThrowables().size(), 0);
    assertEquals(portForward.getServerThrowables().size(), 0);
  }

}
 
Example #3
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LocalPortForward portForward(int port) {
  try {
    return new PortForwarderWebsocket(client).forward(getResourceUrl(), port);
  } catch (Throwable t) {
    throw KubernetesClientException.launderThrowable(t);
  }
}
 
Example #4
Source File: PodOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public LocalPortForward portForward(int port, int localPort) {
  try {
    return new PortForwarderWebsocket(client).forward(getResourceUrl(), port, localPort);
  } catch (Throwable t) {
    throw KubernetesClientException.launderThrowable(t);
  }
}
 
Example #5
Source File: Tiller.java    From microbean-helm with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a {@link ManagedChannel} for communication with Tiller
 * from the information contained in the supplied {@link
 * LocalPortForward}.
 *
 * <p><strong>Note:</strong> This method is (deliberately) called
 * from constructors so must have stateless semantics.</p>
 *
 * <p>This method never returns {@code null}.</p>
 *
 * <p>Overrides of this method must not return {@code null}.</p>
 *
 * @param portForward a {@link LocalPortForward}; must not be {@code
 * null}
 *
 * @return a non-{@code null} {@link ManagedChannel}
 *
 * @exception NullPointerException if {@code portForward} is {@code
 * null}
 *
 * @exception IllegalArgumentException if {@code portForward}'s
 * {@link LocalPortForward#getLocalAddress()} method returns {@code
 * null}
 */
@Issue(id = "42", uri = "https://github.com/microbean/microbean-helm/issues/42")
protected ManagedChannel buildChannel(final LocalPortForward portForward) {
  Objects.requireNonNull(portForward);
  @Issue(id = "43", uri = "https://github.com/microbean/microbean-helm/issues/43")
  final InetAddress localAddress = portForward.getLocalAddress();
  if (localAddress == null) {
    throw new IllegalArgumentException("portForward", new IllegalStateException("portForward.getLocalAddress() == null"));
  }
  final String hostAddress = localAddress.getHostAddress();
  if (hostAddress == null) {
    throw new IllegalArgumentException("portForward", new IllegalStateException("portForward.getLocalAddress().getHostAddress() == null"));
  }
  return ManagedChannelBuilder.forAddress(hostAddress, portForward.getLocalPort())
    .idleTimeout(5L, TimeUnit.SECONDS)
    .keepAliveTime(30L, TimeUnit.SECONDS)
    .maxInboundMessageSize(MAX_MESSAGE_SIZE)
    .usePlaintext(true)
    .build();
}
 
Example #6
Source File: PortForwarderWebsocket.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public LocalPortForward forward(URL resourceBaseUrl, int port) {
  return forward(resourceBaseUrl, port, 0);
}
 
Example #7
Source File: PortForwarderWebsocket.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public LocalPortForward forward(URL resourceBaseUrl, int port, int localPort) {
  return forward(resourceBaseUrl, port, null, localPort);
}
 
Example #8
Source File: Tiller.java    From microbean-helm with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new {@link Tiller} that will use information from the
 * supplied {@link LocalPortForward} to establish a communications
 * channel with the Tiller server.
 *
 * @param portForward the {@link LocalPortForward} to use; must not
 * be {@code null}
 *
 * @param channelBuilder a {@link Function} capable of accepting a
 * {@link LocalPortForward} and returning a new {@link
 * ManagedChannel}; if {@code null} the {@link
 * #buildChannel(LocalPortForward)} method will be used instead; if
 * non-{@code null} then the {@link #buildChannel(LocalPortForward)}
 * method will never be called
 *
 * @exception NullPointerException if {@code portForward} is {@code
 * null}
 *
 * @see #Tiller(LocalPortForward, Function)
 */
public Tiller(final LocalPortForward portForward, final Function<? super LocalPortForward, ? extends ManagedChannel> channelBuilder) {
  super();
  Objects.requireNonNull(portForward);
  this.config = null;
  this.portForward = null; // yes, null
  if (channelBuilder == null) {
    this.channel = this.buildChannel(portForward);
  } else {
    this.channel = channelBuilder.apply(portForward);
  }
}
 
Example #9
Source File: Tiller.java    From microbean-helm with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new {@link Tiller} that will forward a local port to
 * the supplied (remote) port on a Pod housing Tiller in the supplied
 * namespace running in the Kubernetes cluster with which the
 * supplied {@link KubernetesClient} is capable of communicating.
 *
 * <p>The {@linkplain Pods#getFirstReadyPod(Listable) first ready
 * Pod} with labels matching the supplied {@code tillerLabels} is
 * deemed to be the pod housing the Tiller instance to connect
 * to.</p>
 *
 * @param <T> a {@link KubernetesClient} implementation that is also
 * an {@link HttpClientAware} implementation, such as {@link
 * DefaultKubernetesClient}
 *
 * @param client the {@link KubernetesClient}-and-{@link
 * HttpClientAware} implementation that can communicate with a
 * Kubernetes cluster; must not be {@code null}; no reference to
 * this object is retained by this {@link Tiller} instance
 *
 * @param namespaceHousingTiller the namespace within which a Tiller
 * instance is hopefully running; if {@code null}, then the value of
 * {@link #DEFAULT_NAMESPACE} will be used instead
 *
 * @param tillerPort the remote port to attempt to forward a local
 * port to; normally {@code 44134}
 *
 * @param tillerLabels a {@link Map} representing the Kubernetes
 * labels (and their values) identifying a Pod housing a Tiller
 * instance; if {@code null} then the value of {@link
 * #DEFAULT_LABELS} will be used instead
 *
 * @param channelBuilder a {@link Function} capable of accepting a
 * {@link LocalPortForward} and returning a new {@link
 * ManagedChannel}; if {@code null} the {@link
 * #buildChannel(LocalPortForward)} method will be used instead; if
 * non-{@code null} then the {@link #buildChannel(LocalPortForward)}
 * method will never be called
 *
 * @exception MalformedURLException if there was a problem
 * identifying a Pod within the cluster that houses a Tiller instance
 *
 * @exception NullPointerException if {@code client} is {@code null}
 *
 * @exception KubernetesClientException if there was a problem
 * connecting to Kubernetes
 *
 * @exception TillerException if a ready Tiller pod could not be
 * found and consequently a connection could not be established
 */
public <T extends HttpClientAware & KubernetesClient> Tiller(final T client,
                                                             String namespaceHousingTiller,
                                                             int tillerPort,
                                                             Map<String, String> tillerLabels,
                                                             Function<? super LocalPortForward, ? extends ManagedChannel> channelBuilder) throws MalformedURLException {  
  super();
  Objects.requireNonNull(client);
  this.config = client.getConfiguration();
  if (namespaceHousingTiller == null || namespaceHousingTiller.isEmpty()) {
    namespaceHousingTiller = DEFAULT_NAMESPACE;
  }
  if (tillerPort <= 0) {
    tillerPort = DEFAULT_PORT;
  }
  if (tillerLabels == null) {
    tillerLabels = DEFAULT_LABELS;
  }
  final OkHttpClient httpClient = client.getHttpClient();
  if (httpClient == null) {
    throw new IllegalArgumentException("client", new IllegalStateException("client.getHttpClient() == null"));
  }
  LocalPortForward portForward = null;
  
  this.portForward = Pods.forwardPort(httpClient, client.pods().inNamespace(namespaceHousingTiller).withLabels(tillerLabels), tillerPort);
  if (this.portForward == null) {
    throw new TillerException("Could not forward port to a Ready Tiller pod's port " + tillerPort + " in namespace " + namespaceHousingTiller + " with labels " + tillerLabels);
  }
  if (channelBuilder == null) {
    this.channel = this.buildChannel(this.portForward);
  } else {
    this.channel = channelBuilder.apply(this.portForward);
  }
}
 
Example #10
Source File: Tiller.java    From microbean-helm with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@link Tiller} that will use information from the
 * supplied {@link LocalPortForward} to establish a communications
 * channel with the Tiller server.
 *
 * @param portForward the {@link LocalPortForward} to use; must not
 * be {@code null}
 *
 * @exception NullPointerException if {@code portForward} is {@code
 * null}
 *
 * @see #Tiller(LocalPortForward, Function)
 */
public Tiller(final LocalPortForward portForward) {
  this(portForward, null);
}
 
Example #11
Source File: PortForwarder.java    From kubernetes-client with Apache License 2.0 votes vote down vote up
LocalPortForward forward(URL resourceBaseUrl, int port); 
Example #12
Source File: PortForwarder.java    From kubernetes-client with Apache License 2.0 votes vote down vote up
LocalPortForward forward(URL resourceBaseUrl, int port, int localPort); 
Example #13
Source File: PortForwarder.java    From kubernetes-client with Apache License 2.0 votes vote down vote up
LocalPortForward forward(URL resourceBaseUrl, int port, InetAddress localHost, int localPort);