Search in sources :

Example 51 with Logger

use of io.fabric8.maven.docker.util.Logger in project fabric8-maven-plugin by fabric8io.

the class PortForwardService method forwardPortAsync.

/**
 * Forwards a port to the newest pod matching the given selector.
 * If another pod is created, it forwards connections to the new pod once it's ready.
 */
public Closeable forwardPortAsync(final Logger externalProcessLogger, final LabelSelector podSelector, final int remotePort, final int localPort) throws Fabric8ServiceException {
    final Lock monitor = new ReentrantLock(true);
    final Condition podChanged = monitor.newCondition();
    final Pod[] nextForwardedPod = new Pod[1];
    final Thread forwarderThread = new Thread() {

        @Override
        public void run() {
            Pod currentPod = null;
            Closeable currentPortForward = null;
            try {
                monitor.lock();
                while (true) {
                    if (podEquals(currentPod, nextForwardedPod[0])) {
                        podChanged.await();
                    } else {
                        // may be null
                        Pod nextPod = nextForwardedPod[0];
                        try {
                            monitor.unlock();
                            if (currentPortForward != null) {
                                log.info("Closing port-forward from pod %s", KubernetesHelper.getName(currentPod));
                                currentPortForward.close();
                                currentPortForward = null;
                            }
                            if (nextPod != null) {
                                log.info("Starting port-forward to pod %s", KubernetesHelper.getName(nextPod));
                                currentPortForward = forwardPortAsync(externalProcessLogger, KubernetesHelper.getName(nextPod), remotePort, localPort);
                            } else {
                                log.info("Waiting for a pod to become ready before starting port-forward");
                            }
                            currentPod = nextPod;
                        } finally {
                            monitor.lock();
                        }
                    }
                }
            } catch (InterruptedException e) {
                log.debug("Port-forwarding thread interrupted", e);
                Thread.currentThread().interrupt();
            } catch (Exception e) {
                log.warn("Error while port-forwarding to pod", e);
            } finally {
                monitor.unlock();
                if (currentPortForward != null) {
                    try {
                        currentPortForward.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
    };
    // Switching forward to the current pod if present
    Pod newPod = getNewestPod(podSelector);
    nextForwardedPod[0] = newPod;
    final Watch watch = KubernetesClientUtil.withSelector(kubernetes.pods(), podSelector, log).watch(new Watcher<Pod>() {

        @Override
        public void eventReceived(Action action, Pod pod) {
            monitor.lock();
            try {
                List<Pod> candidatePods;
                if (nextForwardedPod[0] != null) {
                    candidatePods = new LinkedList<>();
                    candidatePods.add(nextForwardedPod[0]);
                    candidatePods.add(pod);
                } else {
                    candidatePods = Collections.singletonList(pod);
                }
                // may be null
                Pod newPod = getNewestPod(candidatePods);
                if (!podEquals(nextForwardedPod[0], newPod)) {
                    nextForwardedPod[0] = newPod;
                    podChanged.signal();
                }
            } finally {
                monitor.unlock();
            }
        }

        @Override
        public void onClose(KubernetesClientException e) {
        // don't care
        }
    });
    forwarderThread.start();
    final Closeable handle = new Closeable() {

        @Override
        public void close() throws IOException {
            try {
                watch.close();
            } catch (Exception e) {
            }
            try {
                forwarderThread.interrupt();
                forwarderThread.join(15000);
            } catch (Exception e) {
            }
        }
    };
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                handle.close();
            } catch (Exception e) {
            // suppress
            }
        }
    });
    return handle;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) Pod(io.fabric8.kubernetes.api.model.Pod) Closeable(java.io.Closeable) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) IOException(java.io.IOException) LinkedList(java.util.LinkedList) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Watch(io.fabric8.kubernetes.client.Watch) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) PodList(io.fabric8.kubernetes.api.model.PodList) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 52 with Logger

use of io.fabric8.maven.docker.util.Logger in project fabric8-maven-plugin by fabric8io.

the class Fabric8ServiceHubTest method testObtainBuildService.

@Test
public void testObtainBuildService() {
    Fabric8ServiceHub hub = new Fabric8ServiceHub.Builder().clusterAccess(clusterAccess).log(logger).platformMode(PlatformMode.kubernetes).dockerServiceHub(dockerServiceHub).buildServiceConfig(buildServiceConfig).build();
    BuildService buildService = hub.getBuildService();
    assertNotNull(buildService);
    assertTrue(buildService instanceof DockerBuildService);
}
Also used : OpenshiftBuildService(io.fabric8.maven.core.service.openshift.OpenshiftBuildService) DockerBuildService(io.fabric8.maven.core.service.kubernetes.DockerBuildService) DockerBuildService(io.fabric8.maven.core.service.kubernetes.DockerBuildService) Test(org.junit.Test)

Example 53 with Logger

use of io.fabric8.maven.docker.util.Logger in project fabric8-maven-plugin by fabric8io.

the class Fabric8ServiceHubTest method testObtainOpenshiftBuildService.

@Test
public void testObtainOpenshiftBuildService() {
    Fabric8ServiceHub hub = new Fabric8ServiceHub.Builder().clusterAccess(clusterAccess).log(logger).platformMode(PlatformMode.openshift).dockerServiceHub(dockerServiceHub).buildServiceConfig(buildServiceConfig).build();
    BuildService buildService = hub.getBuildService();
    assertNotNull(buildService);
    assertTrue(buildService instanceof OpenshiftBuildService);
}
Also used : OpenshiftBuildService(io.fabric8.maven.core.service.openshift.OpenshiftBuildService) DockerBuildService(io.fabric8.maven.core.service.kubernetes.DockerBuildService) OpenshiftBuildService(io.fabric8.maven.core.service.openshift.OpenshiftBuildService) Test(org.junit.Test)

Example 54 with Logger

use of io.fabric8.maven.docker.util.Logger in project fabric8-maven-plugin by fabric8io.

the class ClusterAccessTest method createClientTestKubernetes.

@Test
public void createClientTestKubernetes() throws Exception {
    RootPaths rootpaths = new RootPaths();
    rootpaths.setPaths(paths);
    mockServer.expect().get().withPath("/").andReturn(200, rootpaths).always();
    ClusterAccess clusterAccess = new ClusterAccess(null, client);
    Client outputClient = clusterAccess.createDefaultClient(logger);
    assertTrue(outputClient instanceof KubernetesClient);
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) RootPaths(io.fabric8.kubernetes.api.model.RootPaths) Client(io.fabric8.kubernetes.client.Client) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Test(org.junit.Test)

Example 55 with Logger

use of io.fabric8.maven.docker.util.Logger in project fabric8-maven-plugin by fabric8io.

the class ClusterAccessTest method kubernetesPlatformModeTest.

@Test
public void kubernetesPlatformModeTest() throws Exception {
    RootPaths rootpaths = new RootPaths();
    rootpaths.setPaths(paths);
    mockServer.expect().get().withPath("/").andReturn(200, rootpaths).always();
    ClusterAccess clusterAccess = new ClusterAccess(null, client);
    mode = clusterAccess.resolvePlatformMode(PlatformMode.kubernetes, logger);
    assertEquals(PlatformMode.kubernetes, mode);
    mode = clusterAccess.resolvePlatformMode(PlatformMode.DEFAULT, logger);
    assertEquals(PlatformMode.kubernetes, mode);
    mode = clusterAccess.resolvePlatformMode(null, logger);
    assertEquals(PlatformMode.kubernetes, mode);
}
Also used : RootPaths(io.fabric8.kubernetes.api.model.RootPaths) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)30 File (java.io.File)11 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)10 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)9 URL (java.net.URL)8 GeneratorContext (io.fabric8.maven.generator.api.GeneratorContext)7 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)6 ResourceValidator (io.fabric8.maven.core.util.validator.ResourceValidator)6 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)6 Logger (io.fabric8.maven.docker.util.Logger)6 Util.readAsString (io.fabric8.arquillian.utils.Util.readAsString)5 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)5 Pod (io.fabric8.kubernetes.api.model.Pod)5 OpenShiftMockServer (io.fabric8.openshift.client.server.mock.OpenShiftMockServer)5 Expectations (mockit.Expectations)5 Logger (io.fabric8.arquillian.kubernetes.log.Logger)4 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)4 BuildService (io.fabric8.maven.core.service.BuildService)4 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)4