AbrA
AbrA

Reputation: 480

Port forward from OpenShift via Java

How to programmatically execute

oc port-forward <podName> <podPort>:<localPort> 

via Java?
At the moment I managed to get the list of pods under through the library io.fabric8:openshift-client.

OpenShiftClient config = new DefaultOpenShiftClient();
client.pods().inNamespace("namespace").list().getItems();

But in this library don't exist port-forwarding functionality and I didn't find any information on the net

Upvotes: 0

Views: 433

Answers (1)

Mildju
Mildju

Reputation: 51

Something like this works like a charm :

OpenShiftClient client = new KubernetesClientBuilder().build().adapt(OpenShiftClient.class);

Resource<Namespace> openShiftProject = client.namespaces().withName(yourNamespace);

PodResource openShiftPod = client.pods().inNamespace(yourNamespace).withName(yourPodName);

openShiftPod.portForward(REMOTE_PORT_NBR, LOCAL_PORT_NBR);

Feel free to add some checks (isReady for instance) to secure your code. And don't forget to close the client once its job is over.

Upvotes: 1

Related Questions