Search in sources :

Example 21 with EnvVar

use of io.fabric8.kubernetes.api.model.EnvVar in project zalenium by zalando.

the class KubernetesContainerClient method registerNode.

@Override
public ContainerClientRegistration registerNode(String zaleniumContainerName, URL remoteHost) {
    String podIpAddress = remoteHost.getHost();
    // The only way to lookup a pod name by IP address is by looking at all pods in the namespace it seems.
    PodList list = client.pods().withLabels(createdByZaleniumMap).list();
    String containerId = null;
    Pod currentPod = null;
    for (Pod pod : list.getItems()) {
        if (podIpAddress.equals(pod.getStatus().getPodIP())) {
            containerId = pod.getMetadata().getName();
            currentPod = pod;
            break;
        }
    }
    if (containerId == null) {
        throw new IllegalStateException("Unable to locate pod by ip address, registration will fail");
    }
    ContainerClientRegistration registration = new ContainerClientRegistration();
    List<EnvVar> podEnvironmentVariables = currentPod.getSpec().getContainers().get(0).getEnv();
    Optional<EnvVar> noVncPort = podEnvironmentVariables.stream().filter(env -> "NOVNC_PORT".equals(env.getName())).findFirst();
    if (noVncPort.isPresent()) {
        Integer noVncPortInt = Integer.decode(noVncPort.get().getValue());
        registration.setNoVncPort(noVncPortInt);
    } else {
        logger.warn(String.format("%s Couldn't find NOVNC_PORT, live preview will not work.", containerId));
    }
    registration.setIpAddress(currentPod.getStatus().getPodIP());
    registration.setContainerId(containerId);
    return registration;
}
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) PodList(io.fabric8.kubernetes.api.model.PodList) ContainerClientRegistration(de.zalando.ep.zalenium.container.ContainerClientRegistration) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 22 with EnvVar

use of io.fabric8.kubernetes.api.model.EnvVar in project shinyproxy by openanalytics.

the class KubernetesBackend method doStartProxy.

@Override
protected void doStartProxy(KubernetesContainerProxy proxy) throws Exception {
    String kubeNamespace = getProperty(PROPERTY_NAMESPACE, proxy.getApp(), DEFAULT_NAMESPACE);
    String apiVersion = getProperty(PROPERTY_API_VERSION, proxy.getApp(), DEFAULT_API_VERSION);
    String[] volumeStrings = Optional.ofNullable(proxy.getApp().getDockerVolumes()).orElse(new String[] {});
    Volume[] volumes = new Volume[volumeStrings.length];
    VolumeMount[] volumeMounts = new VolumeMount[volumeStrings.length];
    for (int i = 0; i < volumeStrings.length; i++) {
        String[] volume = volumeStrings[i].split(":");
        String hostSource = volume[0];
        String containerDest = volume[1];
        String name = "shinyproxy-volume-" + i;
        volumes[i] = new VolumeBuilder().withNewHostPath(hostSource).withName(name).build();
        volumeMounts[i] = new VolumeMountBuilder().withMountPath(containerDest).withName(name).build();
    }
    List<EnvVar> envVars = new ArrayList<>();
    for (String envString : buildEnv(proxy.getUserId(), proxy.getApp())) {
        int idx = envString.indexOf('=');
        if (idx == -1)
            log.warn("Invalid environment variable: " + envString);
        envVars.add(new EnvVar(envString.substring(0, idx), envString.substring(idx + 1), null));
    }
    SecurityContext security = new SecurityContextBuilder().withPrivileged(Boolean.valueOf(getProperty(PROPERTY_PRIVILEGED, proxy.getApp(), DEFAULT_PRIVILEGED))).build();
    ContainerBuilder containerBuilder = new ContainerBuilder().withImage(proxy.getApp().getDockerImage()).withName("shiny-container").withPorts(new ContainerPortBuilder().withContainerPort(getAppPort(proxy)).build()).withVolumeMounts(volumeMounts).withSecurityContext(security).withEnv(envVars);
    String imagePullPolicy = getProperty(PROPERTY_IMG_PULL_POLICY, proxy.getApp(), null);
    if (imagePullPolicy != null)
        containerBuilder.withImagePullPolicy(imagePullPolicy);
    if (proxy.getApp().getDockerCmd() != null)
        containerBuilder.withCommand(proxy.getApp().getDockerCmd());
    String[] imagePullSecrets = getProperty(PROPERTY_IMG_PULL_SECRETS, proxy.getApp(), String[].class, null);
    if (imagePullSecrets == null) {
        String imagePullSecret = getProperty(PROPERTY_IMG_PULL_SECRET, proxy.getApp(), null);
        if (imagePullSecret != null) {
            imagePullSecrets = new String[] { imagePullSecret };
        } else {
            imagePullSecrets = new String[0];
        }
    }
    Pod pod = kubeClient.pods().inNamespace(kubeNamespace).createNew().withApiVersion(apiVersion).withKind("Pod").withNewMetadata().withName(proxy.getName()).addToLabels("app", proxy.getName()).endMetadata().withNewSpec().withContainers(Collections.singletonList(containerBuilder.build())).withVolumes(volumes).withImagePullSecrets(Arrays.asList(imagePullSecrets).stream().map(LocalObjectReference::new).collect(Collectors.toList())).endSpec().done();
    proxy.setPod(kubeClient.resource(pod).waitUntilReady(600, TimeUnit.SECONDS));
    if (!isUseInternalNetwork()) {
        // If SP runs outside the cluster, a NodePort service is needed to access the pod externally.
        Service service = kubeClient.services().inNamespace(kubeNamespace).createNew().withApiVersion(apiVersion).withKind("Service").withNewMetadata().withName(proxy.getName() + "service").endMetadata().withNewSpec().addToSelector("app", proxy.getName()).withType("NodePort").withPorts(new ServicePortBuilder().withPort(getAppPort(proxy)).build()).endSpec().done();
        // Retry, because if this is done too fast, an 'endpoint not found' exception will be thrown.
        Utils.retry(i -> {
            try {
                proxy.setService(kubeClient.resource(service).waitUntilReady(600, TimeUnit.SECONDS));
            } catch (Exception e) {
                return false;
            }
            return true;
        }, 5, 1000);
        releasePort(proxy.getPort());
        proxy.setPort(proxy.getService().getSpec().getPorts().get(0).getNodePort());
    }
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) ArrayList(java.util.ArrayList) Service(io.fabric8.kubernetes.api.model.Service) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) IOException(java.io.IOException) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException) SecurityContextBuilder(io.fabric8.kubernetes.api.model.SecurityContextBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) ServicePortBuilder(io.fabric8.kubernetes.api.model.ServicePortBuilder) ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) SecurityContext(io.fabric8.kubernetes.api.model.SecurityContext) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 23 with EnvVar

use of io.fabric8.kubernetes.api.model.EnvVar in project fabric8 by fabric8io.

the class ListEnvVarBuilder method withEnvVar.

public void withEnvVar(String name, String value) {
    if (Strings.isNotBlank(name) && value != null) {
        EnvVar envVar = new EnvVar();
        envVar.setName(name);
        envVar.setValue(value);
        envVars.add(envVar);
    }
}
Also used : EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 24 with EnvVar

use of io.fabric8.kubernetes.api.model.EnvVar in project flink by apache.

the class KubernetesClusterDescriptorTest method testDeployHighAvailabilitySessionCluster.

@Test
public void testDeployHighAvailabilitySessionCluster() throws ClusterDeploymentException {
    flinkConfig.set(DeploymentOptions.TARGET, KubernetesDeploymentTarget.SESSION.getName());
    flinkConfig.setString(HighAvailabilityOptions.HA_MODE, HighAvailabilityMode.ZOOKEEPER.toString());
    final ClusterClient<String> clusterClient = deploySessionCluster().getClusterClient();
    checkClusterClient(clusterClient);
    final Container jmContainer = kubeClient.apps().deployments().list().getItems().get(0).getSpec().getTemplate().getSpec().getContainers().get(0);
    assertTrue("Environment " + ENV_FLINK_POD_IP_ADDRESS + " should be set.", jmContainer.getEnv().stream().map(EnvVar::getName).collect(Collectors.toList()).contains(ENV_FLINK_POD_IP_ADDRESS));
    clusterClient.close();
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) Test(org.junit.Test)

Example 25 with EnvVar

use of io.fabric8.kubernetes.api.model.EnvVar in project flink by apache.

the class KubernetesTaskManagerFactoryTest method testContainer.

@Test
public void testContainer() {
    final List<Container> resultContainers = this.resultPod.getSpec().getContainers();
    assertEquals(1, resultContainers.size());
    final Container resultMainContainer = resultContainers.get(0);
    assertEquals(Constants.MAIN_CONTAINER_NAME, resultMainContainer.getName());
    assertEquals(CONTAINER_IMAGE, resultMainContainer.getImage());
    assertEquals(CONTAINER_IMAGE_PULL_POLICY.name(), resultMainContainer.getImagePullPolicy());
    assertEquals(4, resultMainContainer.getEnv().size());
    assertTrue(resultMainContainer.getEnv().stream().anyMatch(envVar -> envVar.getName().equals("key1")));
    assertEquals(1, resultMainContainer.getPorts().size());
    assertEquals(1, resultMainContainer.getCommand().size());
    // The args list is [bash, -c, 'java -classpath $FLINK_CLASSPATH ...'].
    assertEquals(3, resultMainContainer.getArgs().size());
    assertEquals(4, resultMainContainer.getVolumeMounts().size());
}
Also used : KubernetesTaskManagerTestBase(org.apache.flink.kubernetes.kubeclient.KubernetesTaskManagerTestBase) SecurityOptions(org.apache.flink.configuration.SecurityOptions) KubernetesTestUtils(org.apache.flink.kubernetes.KubernetesTestUtils) Container(io.fabric8.kubernetes.api.model.Container) Pod(io.fabric8.kubernetes.api.model.Pod) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FlinkPod(org.apache.flink.kubernetes.kubeclient.FlinkPod) List(java.util.List) CONFIG_FILE_LOG4J_NAME(org.apache.flink.kubernetes.utils.Constants.CONFIG_FILE_LOG4J_NAME) CONFIG_FILE_LOGBACK_NAME(org.apache.flink.kubernetes.utils.Constants.CONFIG_FILE_LOGBACK_NAME) Constants(org.apache.flink.kubernetes.utils.Constants) Assert.assertEquals(org.junit.Assert.assertEquals) Container(io.fabric8.kubernetes.api.model.Container) Test(org.junit.Test)

Aggregations

EnvVar (io.fabric8.kubernetes.api.model.EnvVar)51 ArrayList (java.util.ArrayList)15 Test (org.junit.Test)15 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)11 EnvVarBuilder (io.fabric8.kubernetes.api.model.EnvVarBuilder)10 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)10 HashMap (java.util.HashMap)10 Container (io.fabric8.kubernetes.api.model.Container)9 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)9 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)9 File (java.io.File)8 List (java.util.List)8 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)7 Pod (io.fabric8.kubernetes.api.model.Pod)7 ServiceSpecBuilder (io.fabric8.kubernetes.api.model.ServiceSpecBuilder)7 Map (java.util.Map)7 Test (org.testng.annotations.Test)7 LinkedList (java.util.LinkedList)6 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)5 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)5