Search in sources :

Example 1 with ContainerCreationStatus

use of de.zalando.ep.zalenium.container.ContainerCreationStatus in project zalenium by zalando.

the class DockerSeleniumStarterRemoteProxy method startDockerSeleniumContainer.

@VisibleForTesting
public boolean startDockerSeleniumContainer(TimeZone timeZone, Dimension screenSize, boolean forceCreation) {
    NetworkUtils networkUtils = new NetworkUtils();
    String hostIpAddress = networkUtils.getIp4NonLoopbackAddressOfThisMachine().getHostAddress();
    String nodePolling = String.valueOf(RandomUtils.nextInt(90, 120) * 1000);
    String nodeRegisterCycle = String.valueOf(RandomUtils.nextInt(15, 25) * 1000);
    String seleniumNodeParams = getSeleniumNodeParameters();
    String latestImage = getLatestDownloadedImage(getDockerSeleniumImageName());
    int attempts = 0;
    int maxAttempts = 2;
    while (attempts < maxAttempts) {
        attempts++;
        if (forceCreation || validateAmountOfDockerSeleniumContainers()) {
            final int nodePort = findFreePortInRange(LOWER_PORT_BOUNDARY, UPPER_PORT_BOUNDARY);
            Map<String, String> envVars = buildEnvVars(timeZone, screenSize, hostIpAddress, sendAnonymousUsageInfo, nodePolling, nodeRegisterCycle, nodePort, seleniumNodeParams);
            ContainerCreationStatus creationStatus = containerClient.createContainer(getContainerName(), latestImage, envVars, String.valueOf(nodePort));
            if (creationStatus.isCreated() && checkContainerStatus(creationStatus)) {
                return true;
            } else {
                LOGGER.debug(String.format("%sContainer creation failed, retrying...", getId()));
            }
        } else {
            LOGGER.info(String.format("%sNo container was created, will try again in a moment...", getId()));
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                LOGGER.debug("Exception while making a pause during container creation.", e);
            }
        }
    }
    LOGGER.info(String.format("%sNo container was created after 3 attempts, will wait until request is " + "processed again...", getId()));
    return false;
}
Also used : ContainerCreationStatus(de.zalando.ep.zalenium.container.ContainerCreationStatus) NetworkUtils(org.openqa.selenium.net.NetworkUtils) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with ContainerCreationStatus

use of de.zalando.ep.zalenium.container.ContainerCreationStatus in project zalenium by zalando.

the class KubernetesContainerClient method createContainer.

@Override
public ContainerCreationStatus createContainer(String zaleniumContainerName, String image, Map<String, String> envVars, String nodePort) {
    String containerIdPrefix = String.format("%s-%s-", zaleniumAppName, nodePort);
    // Convert the environment variables into the Kubernetes format.
    List<EnvVar> flattenedEnvVars = envVars.entrySet().stream().map(e -> new EnvVar(e.getKey(), e.getValue(), null)).collect(Collectors.toList());
    Map<String, String> podSelector = new HashMap<>();
    PodConfiguration config = new PodConfiguration();
    config.setNodePort(nodePort);
    config.setClient(client);
    config.setContainerIdPrefix(containerIdPrefix);
    config.setImage(image);
    config.setEnvVars(flattenedEnvVars);
    Map<String, String> labels = new HashMap<>();
    labels.putAll(createdByZaleniumMap);
    labels.putAll(appLabelMap);
    labels.putAll(podSelector);
    config.setLabels(labels);
    config.setMountedSharedFoldersMap(mountedSharedFoldersMap);
    config.setHostAliases(hostAliases);
    config.setNodeSelector(nodeSelector);
    config.setPodLimits(seleniumPodLimits);
    config.setPodRequests(seleniumPodRequests);
    DoneablePod doneablePod = createDoneablePod.apply(config);
    // Create the container
    Pod createdPod = doneablePod.done();
    String containerName = createdPod.getMetadata() == null ? containerIdPrefix : createdPod.getMetadata().getName();
    return new ContainerCreationStatus(true, containerName, nodePort);
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Environment(de.zalando.ep.zalenium.util.Environment) URL(java.net.URL) ContainerClientRegistration(de.zalando.ep.zalenium.container.ContainerClientRegistration) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) HostAlias(io.fabric8.kubernetes.api.model.HostAlias) Map(java.util.Map) Response(okhttp3.Response) ExecListener(io.fabric8.kubernetes.client.dsl.ExecListener) ContainerClient(de.zalando.ep.zalenium.container.ContainerClient) Volume(io.fabric8.kubernetes.api.model.Volume) Logger(org.slf4j.Logger) Pod(io.fabric8.kubernetes.api.model.Pod) ExecWatch(io.fabric8.kubernetes.client.dsl.ExecWatch) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) PodList(io.fabric8.kubernetes.api.model.PodList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Optional(java.util.Optional) ContainerCreationStatus(de.zalando.ep.zalenium.container.ContainerCreationStatus) InputStream(java.io.InputStream) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) HashMap(java.util.HashMap) ContainerCreationStatus(de.zalando.ep.zalenium.container.ContainerCreationStatus) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Aggregations

ContainerCreationStatus (de.zalando.ep.zalenium.container.ContainerCreationStatus)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ContainerClient (de.zalando.ep.zalenium.container.ContainerClient)1 ContainerClientRegistration (de.zalando.ep.zalenium.container.ContainerClientRegistration)1 Environment (de.zalando.ep.zalenium.util.Environment)1 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)1 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)1 HostAlias (io.fabric8.kubernetes.api.model.HostAlias)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 Quantity (io.fabric8.kubernetes.api.model.Quantity)1 Volume (io.fabric8.kubernetes.api.model.Volume)1 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 ExecListener (io.fabric8.kubernetes.client.dsl.ExecListener)1 ExecWatch (io.fabric8.kubernetes.client.dsl.ExecWatch)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 InetAddress (java.net.InetAddress)1 URL (java.net.URL)1