Skip to content

Commit

Permalink
fix fabric8io#5033: allow port-forward to work with other clients
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Apr 6, 2023
1 parent e2a85ad commit 012ba17
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Fix #4985: triggering the immediate cleanup of the okhttp idle task
* fix #5002: Jetty response completion accounts for header processing
* Fix #5009: addressing issue with serialization of wrapped polymophic types
* Fix #5033: port forwarding for clients other than okhttp needs to specify the subprotocol

#### Improvements
* Fix #4434: Update CronJobIT to use `batch/v1` CronJob instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public PortForward forward(URL resourceBaseUrl, int port, final ReadableByteChan
CompletableFuture<WebSocket> socket = client
.newWebSocketBuilder()
.uri(URI.create(URLUtils.join(resourceBaseUrl.toString(), "portforward?ports=" + port)))
.subprotocol("v4.channel.k8s.io")
.buildAsync(listener);

socket.whenComplete((w, t) -> {
Expand Down
28 changes: 28 additions & 0 deletions kubernetes-itests/src/test/java/io/fabric8/kubernetes/PodIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.api.model.Status;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.LocalPortForward;
import io.fabric8.kubernetes.client.dsl.ExecListener;
import io.fabric8.kubernetes.client.dsl.ExecWatch;
import io.fabric8.kubernetes.client.dsl.LogWatch;
Expand All @@ -44,6 +45,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -444,4 +448,28 @@ void listFromServer() {
assertEquals(pod1.getMetadata().getName(), fromServerPod.getMetadata().getName());
}

@Test
void portForward() throws IOException, InterruptedException {
client.pods().withName("nginx").waitUntilReady(POD_READY_WAIT_IN_MILLIS, TimeUnit.SECONDS);
LocalPortForward portForward = client.pods().withName("nginx").portForward(80);
try (SocketChannel channel = SocketChannel.open()) {

int localPort = portForward.getLocalPort();

URL url = new URL("http://localhost:" + localPort);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

InputStream content = (InputStream) conn.getContent();

// make sure we got data, should be the welcome page
assertTrue(content.read() != -1);
} finally {
portForward.close();

assertThat(portForward.getServerThrowables()).isEmpty();
assertThat(portForward.getClientThrowables()).isEmpty();
}
}

}
11 changes: 11 additions & 0 deletions kubernetes-itests/src/test/resources/pod-it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,14 @@ spec:
command: ["timeout", "-s", "SIGKILL", "36000", "sh", "-i"]
stdin: true
tty: true
---
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

0 comments on commit 012ba17

Please sign in to comment.