Search in sources :

Example 1 with PortPair

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();
}
Also used : IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable) PortPair(com.openshift.restclient.capability.resources.IPortForwardable.PortPair) CapabilityVisitor(com.openshift.restclient.capability.CapabilityVisitor) IPod(com.openshift.restclient.model.IPod) Test(org.junit.Test)

Example 2 with PortPair

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);
}
Also used : IPort(com.openshift.restclient.model.IPort) IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable) PortPair(com.openshift.restclient.capability.resources.IPortForwardable.PortPair) IPod(com.openshift.restclient.model.IPod) Test(org.junit.Test)

Example 3 with PortPair

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;
}
Also used : IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable) PortPair(com.openshift.restclient.capability.resources.IPortForwardable.PortPair)

Example 4 with PortPair

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())));
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) PortForwardingUtils(org.jboss.tools.openshift.internal.core.portforwarding.PortForwardingUtils) ResourceUtils(org.jboss.tools.openshift.internal.core.util.ResourceUtils) CoreException(org.eclipse.core.runtime.CoreException) IDebugListener(org.jboss.tools.openshift.internal.core.server.debug.IDebugListener) ILaunchConfiguration(org.eclipse.debug.core.ILaunchConfiguration) DebugException(org.eclipse.debug.core.DebugException) IStatus(org.eclipse.core.runtime.IStatus) Server(org.eclipse.wst.server.core.internal.Server) ILaunch(org.eclipse.debug.core.ILaunch) AbstractSubsystemController(org.jboss.ide.eclipse.as.wtp.core.server.behavior.AbstractSubsystemController) StatusFactory(org.jboss.tools.foundation.core.plugin.log.StatusFactory) SocketUtil(org.eclipse.jdt.launching.SocketUtil) NLS(org.eclipse.osgi.util.NLS) ClassCollectingHCRListener(org.jboss.ide.eclipse.as.core.util.ClassCollectingHCRListener) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) OpenShiftServerBehaviour(org.jboss.tools.openshift.core.server.OpenShiftServerBehaviour) OpenShiftCoreActivator(org.jboss.tools.openshift.internal.core.OpenShiftCoreActivator) ILaunchManager(org.eclipse.debug.core.ILaunchManager) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Objects(java.util.Objects) IBinaryCapability(com.openshift.restclient.capability.IBinaryCapability) Optional(java.util.Optional) PortPair(com.openshift.restclient.capability.resources.IPortForwardable.PortPair) IReplicationController(com.openshift.restclient.model.IReplicationController) OpenShiftDebugMode(org.jboss.tools.openshift.internal.core.server.debug.OpenShiftDebugMode) DockerImageLabels(org.jboss.tools.openshift.core.server.DockerImageLabels) IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable) OpenShiftServerUtils.toCoreException(org.jboss.tools.openshift.core.server.OpenShiftServerUtils.toCoreException) IPod(com.openshift.restclient.model.IPod) ServerProcess(org.jboss.ide.eclipse.as.wtp.core.server.launch.ServerProcess) IProject(org.eclipse.core.resources.IProject) IModule(org.eclipse.wst.server.core.IModule) DebugLaunchConfigs(org.jboss.tools.openshift.internal.core.server.debug.DebugLaunchConfigs) IJavaDebugTarget(org.eclipse.jdt.debug.core.IJavaDebugTarget) IResource(com.openshift.restclient.model.IResource) Job(org.eclipse.core.runtime.jobs.Job) IServer(org.eclipse.wst.server.core.IServer) OpenShiftServerUtils(org.jboss.tools.openshift.core.server.OpenShiftServerUtils) IOException(java.io.IOException) ILaunchConfigurationWorkingCopy(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) IJavaHotCodeReplaceListener(org.eclipse.jdt.debug.core.IJavaHotCodeReplaceListener) OpenShiftCoreMessages(org.jboss.tools.openshift.core.OpenShiftCoreMessages) Connection(org.jboss.tools.openshift.core.connection.Connection) DebugContext(org.jboss.tools.openshift.internal.core.server.debug.DebugContext) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IDebugTarget(org.eclipse.debug.core.model.IDebugTarget) ILaunchServerController(org.jboss.ide.eclipse.as.wtp.core.server.behavior.ILaunchServerController) ISubsystemController(org.jboss.ide.eclipse.as.wtp.core.server.behavior.ISubsystemController) CoreException(org.eclipse.core.runtime.CoreException) OpenShiftServerUtils.toCoreException(org.jboss.tools.openshift.core.server.OpenShiftServerUtils.toCoreException) PortPair(com.openshift.restclient.capability.resources.IPortForwardable.PortPair) IPod(com.openshift.restclient.model.IPod)

Example 5 with PortPair

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);
}
Also used : IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable) OutputStream(java.io.OutputStream) Arrays(java.util.Arrays) MultiStatus(org.eclipse.core.runtime.MultiStatus) IPortForwardable(com.openshift.restclient.capability.resources.IPortForwardable) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) ServerSocket(java.net.ServerSocket) HashSet(java.util.HashSet) IPod(com.openshift.restclient.model.IPod) OCBinaryOperation(org.jboss.tools.openshift.internal.core.OCBinaryOperation) CapabilityVisitor(com.openshift.restclient.capability.CapabilityVisitor) Map(java.util.Map) ConnectionsRegistryUtil(org.jboss.tools.openshift.core.connection.ConnectionsRegistryUtil) OpenShiftBinaryOption(com.openshift.restclient.capability.IBinaryCapability.OpenShiftBinaryOption) PortPair(com.openshift.restclient.capability.resources.IPortForwardable.PortPair) Collections(java.util.Collections) PortPair(com.openshift.restclient.capability.resources.IPortForwardable.PortPair) PortPair(com.openshift.restclient.capability.resources.IPortForwardable.PortPair) HashSet(java.util.HashSet)

Aggregations

IPortForwardable (com.openshift.restclient.capability.resources.IPortForwardable)7 PortPair (com.openshift.restclient.capability.resources.IPortForwardable.PortPair)7 IPod (com.openshift.restclient.model.IPod)6 CapabilityVisitor (com.openshift.restclient.capability.CapabilityVisitor)4 Test (org.junit.Test)4 IOException (java.io.IOException)2 Set (java.util.Set)2 IBinaryCapability (com.openshift.restclient.capability.IBinaryCapability)1 OpenShiftBinaryOption (com.openshift.restclient.capability.IBinaryCapability.OpenShiftBinaryOption)1 IPort (com.openshift.restclient.model.IPort)1 IReplicationController (com.openshift.restclient.model.IReplicationController)1 IResource (com.openshift.restclient.model.IResource)1 OutputStream (java.io.OutputStream)1 ServerSocket (java.net.ServerSocket)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1