Search in sources :

Example 51 with Logger

use of io.fabric8.arquillian.kubernetes.log.Logger in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method mergeResources.

/**
 * Merges the given resources together into a single resource.
 *
 * If switchOnLocalCustomisation is false then the overrides from item2 are merged into item1
 *
 * @return the newly merged resources
 */
public static HasMetadata mergeResources(HasMetadata item1, HasMetadata item2, Logger log, boolean switchOnLocalCustomisation) {
    if (item1 instanceof Deployment && item2 instanceof Deployment) {
        return mergeDeployments((Deployment) item1, (Deployment) item2, log, switchOnLocalCustomisation);
    }
    if (item1 instanceof ConfigMap && item2 instanceof ConfigMap) {
        ConfigMap cm1 = (ConfigMap) item1;
        ConfigMap cm2 = (ConfigMap) item2;
        return mergeConfigMaps(cm1, cm2, log, switchOnLocalCustomisation);
    }
    mergeMetadata(item1, item2);
    return item1;
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment)

Example 52 with Logger

use of io.fabric8.arquillian.kubernetes.log.Logger in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method mergeConfigMaps.

protected static HasMetadata mergeConfigMaps(ConfigMap cm1, ConfigMap cm2, Logger log, boolean switchOnLocalCustomisation) {
    ConfigMap cm1OrCopy = cm1;
    if (!switchOnLocalCustomisation) {
        // lets copy the original to avoid modifying it
        cm1OrCopy = new ConfigMapBuilder(cm1OrCopy).build();
    }
    log.info("Merging 2 resources for " + getKind(cm1OrCopy) + " " + getName(cm1OrCopy) + " from " + getSourceUrlAnnotation(cm1OrCopy) + " and " + getSourceUrlAnnotation(cm2) + " and removing " + getSourceUrlAnnotation(cm1OrCopy));
    cm1OrCopy.setData(mergeMapsAndRemoveEmptyStrings(cm2.getData(), cm1OrCopy.getData()));
    mergeMetadata(cm1OrCopy, cm2);
    return cm1OrCopy;
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder)

Example 53 with Logger

use of io.fabric8.arquillian.kubernetes.log.Logger in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method addPort.

public static boolean addPort(List<ContainerPort> ports, String portNumberText, String portName, Logger log) {
    if (Strings.isNullOrBlank(portNumberText)) {
        return false;
    }
    int portValue;
    try {
        portValue = Integer.parseInt(portNumberText);
    } catch (NumberFormatException e) {
        log.warn("Could not parse remote debugging port %s as an integer: %s", portNumberText, e);
        return false;
    }
    for (ContainerPort port : ports) {
        String name = port.getName();
        Integer containerPort = port.getContainerPort();
        if (containerPort != null && containerPort.intValue() == portValue) {
            return false;
        }
    }
    ports.add(new ContainerPortBuilder().withName(portName).withContainerPort(portValue).build());
    return true;
}
Also used : ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort)

Example 54 with Logger

use of io.fabric8.arquillian.kubernetes.log.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 55 with Logger

use of io.fabric8.arquillian.kubernetes.log.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)

Aggregations

Test (org.junit.Test)47 File (java.io.File)26 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)20 Verifications (mockit.Verifications)17 ArrayList (java.util.ArrayList)15 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)12 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)10 IOException (java.io.IOException)9 Expectations (mockit.Expectations)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 AssemblyConfiguration (io.fabric8.maven.docker.config.AssemblyConfiguration)6 Logger (io.fabric8.maven.docker.util.Logger)6 MojoParameters (io.fabric8.maven.docker.util.MojoParameters)6 Properties (java.util.Properties)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