use of com.openshift.restclient.capability.resources.IPortForwardable in project jbosstools-openshift by jbosstools.
the class PortForwardingWizardModel method startPortForwarding.
/**
* Starts the port-forwarding for the current pod.
*/
public void startPortForwarding() {
final MessageConsole console = ConsoleUtils.findMessageConsole(getMessageConsoleName());
try (MessageConsoleStream stream = console.newMessageStream()) {
ConsoleUtils.registerConsoleListener(consoleListener);
ConsoleUtils.displayConsoleView(console);
stream.println("Starting port-forwarding...");
final IPortForwardable portForwardable = PortForwardingUtils.startPortForwarding(this.pod, this.ports, IBinaryCapability.SKIP_TLS_VERIFY);
portForwardable.getPortPairs().stream().forEach(port -> stream.println(NLS.bind("{0} {1} -> {2}", new Object[] { port.getName(), port.getLocalPort(), port.getRemotePort() })));
stream.println("done.");
firePropertyChange(PROPERTY_PORT_FORWARDING, false, getPortForwarding());
updatePortForwardingAllowed();
} catch (IOException e) {
OpenShiftUIActivator.getDefault().getLogger().logError("Error while closing the console inputstream", e);
}
}
use of com.openshift.restclient.capability.resources.IPortForwardable in project jbosstools-openshift by jbosstools.
the class PortForwardingUtils method stopPortForwarding.
/**
* Stops all port-forwarding for the given {@code pod}
*
* @param pod
* the pod on which port-forwarding is to be stopped
* @param stream the MessageConsoleStream to use to print messages
* @return the {@link IPortForwardable} referencing all ports that were
* forwarded, or <code>null</code> if port-forwarding was not already
* started on the given pod.
* @throws IOException when writing into the given {@code stream} fails
*/
public static IPortForwardable stopPortForwarding(final IPod pod, final OutputStream stream) throws IOException {
if (!PortForwardingUtils.isPortForwardingStarted(pod)) {
return null;
}
final IPortForwardable portForwarding = REGISTRY.remove(pod);
if (portForwarding != null) {
portForwarding.stop();
}
waitForPortsToGetFree(portForwarding.getPortPairs(), 5, stream);
return portForwarding;
}
use of com.openshift.restclient.capability.resources.IPortForwardable in project jbosstools-openshift by jbosstools.
the class PortForwardingUtilsTest method shouldStartPortForwardingOnSinglePort.
@Test
@SuppressWarnings("unchecked")
public void shouldStartPortForwardingOnSinglePort() {
// given
final IPod pod = Mockito.mock(IPod.class);
final PortPair port = Mockito.mock(PortPair.class);
final IPortForwardable portForwardable = Mockito.mock(IPortForwardable.class);
Mockito.when(pod.accept(Mockito.any(CapabilityVisitor.class), Mockito.any(IPortForwardable.class))).thenReturn(portForwardable);
Mockito.when(portForwardable.isForwarding()).thenReturn(true);
// when
final IPortForwardable startPortForwarding = PortForwardingUtils.startPortForwarding(pod, port);
// then
assertThat(startPortForwarding).isNotNull().isEqualTo(portForwardable);
assertThat(PortForwardingUtils.isPortForwardingStarted(pod)).isTrue();
}
use of com.openshift.restclient.capability.resources.IPortForwardable in project jbosstools-openshift by jbosstools.
the class PortForwardingUtilsTest method shouldGetForwardablePortsOnStartedStopped.
@Test
public void shouldGetForwardablePortsOnStartedStopped() {
// given
final IPod pod = Mockito.mock(IPod.class);
final IPort port = Mockito.mock(IPort.class);
final IPortForwardable portForwardable = Mockito.mock(IPortForwardable.class);
Mockito.when(pod.getContainerPorts()).thenReturn(toSet(port));
// when
final Set<PortPair> forwardablePorts = PortForwardingUtils.getForwardablePorts(pod);
// then
assertThat(forwardablePorts).isNotNull().hasSize(1);
}
use of com.openshift.restclient.capability.resources.IPortForwardable in project jbosstools-openshift by jbosstools.
the class PortForwardingWizardModel method stopPortForwarding.
/**
* Stops the port-forwarding for the current pod.
* @throws IOException
*/
public void stopPortForwarding() throws IOException {
if (!PortForwardingUtils.isPortForwardingStarted(pod)) {
return;
}
final MessageConsole console = ConsoleUtils.findMessageConsole(getMessageConsoleName());
try (final MessageConsoleStream stream = console.newMessageStream()) {
stream.println("Stopping port-forwarding...");
final IPortForwardable cap = PortForwardingUtils.stopPortForwarding(pod, stream);
if (cap != null) {
cap.getPortPairs().stream().forEach(port -> stream.println(NLS.bind("{0} {1} -> {2}", new Object[] { port.getName(), port.getLocalPort(), port.getRemotePort() })));
}
if (!PortForwardingUtils.hasPortInUse(this.ports)) {
stream.println("done.");
} else {
stream.println("Ports remain in use yet. Stopping ports is requested and eventually will be completed.");
}
ConsoleUtils.displayConsoleView(console);
firePropertyChange(PROPERTY_PORT_FORWARDING, true, getPortForwarding());
updatePortForwardingAllowed();
} finally {
ConsoleUtils.deregisterConsoleListener(consoleListener);
}
}
Aggregations