use of com.openshift.restclient.capability.resources.IPortForwardable.PortPair 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.PortPair 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.PortPair in project jbosstools-openshift by jbosstools.
the class OpenShiftLaunchController method mapPorts.
private boolean mapPorts(Set<PortPair> podPorts, final IProgressMonitor monitor) {
for (IPortForwardable.PortPair port : podPorts) {
if (monitor.isCanceled()) {
return false;
}
port.setLocalPort(SocketUtil.findFreePort());
monitor.worked(1);
}
return true;
}
use of com.openshift.restclient.capability.resources.IPortForwardable.PortPair in project jbosstools-openshift by jbosstools.
the class OpenShiftLaunchController method mapPortForwarding.
/**
* Map the remote port to a local port.
* Return the local port in use, or -1 if failed
* @param server
* @param remotePort
* @return the local debug port or -1 if port forwarding did not start or was cancelled.
* @throws CoreException
*/
protected int mapPortForwarding(final DebugContext context, final IProgressMonitor monitor) throws CoreException {
monitor.subTask("Starting port forwarding...");
IPod pod = context.getPod();
if (pod == null) {
throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could not find running pod to forward to in server adapter \"{0}\"", getServer().getName())));
}
Set<PortPair> podPorts = PortForwardingUtils.getForwardablePorts(pod);
int remotePort = context.getDebugPort();
if (remotePort == DebugContext.NO_DEBUG_PORT) {
throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("No pod port to forward to specified in server adapter \"{0}\"", getServer().getName())));
}
Optional<PortPair> debugPort = podPorts.stream().filter(p -> remotePort == p.getRemotePort()).findFirst();
if (!debugPort.isPresent()) {
throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Pod port specified in server adapter \"{0}\" is not present in pod \"{1}\"", getServer().getName(), pod.getName())));
}
if (PortForwardingUtils.isPortForwardingStarted(pod)) {
return debugPort.get().getLocalPort();
}
if (mapPorts(podPorts, monitor)) {
PortForwardingUtils.startPortForwarding(pod, podPorts, IBinaryCapability.SKIP_TLS_VERIFY);
if (PortForwardingUtils.isPortForwardingStarted(pod)) {
return debugPort.get().getLocalPort();
}
}
throw new CoreException(StatusFactory.errorStatus(OpenShiftCoreActivator.PLUGIN_ID, NLS.bind("Could not setup port forwarding to pod \"{0}\" in server adapter \"{1}\"", pod.getName(), getServer().getName())));
}
use of com.openshift.restclient.capability.resources.IPortForwardable.PortPair in project jbosstools-openshift by jbosstools.
the class PortForwardingUtils method getForwardablePorts.
/**
* Returns the {@link PortPair} for the given {@code pod}
* @param pod the pod to analyze
* @return an <strong>immutable</strong> {@link Set} of {@link PortPair}
*/
public static Set<IPortForwardable.PortPair> getForwardablePorts(final IPod pod) {
final Set<IPortForwardable.PortPair> ports = new HashSet<>();
final IPortForwardable forwardable = REGISTRY.get(pod);
if (forwardable != null && forwardable.getPortPairs() != null) {
ports.addAll(forwardable.getPortPairs());
} else if (pod.getContainerPorts() != null) {
pod.getContainerPorts().stream().map(containerPort -> new IPortForwardable.PortPair(containerPort)).forEach(portPair -> ports.add(portPair));
}
return Collections.unmodifiableSet(ports);
}
Aggregations