Search in sources :

Example 1 with V1EnvVar

use of io.kubernetes.client.openapi.models.V1EnvVar in project heron by twitter.

the class V1Controller method getExecutorEnvVars.

/**
 * Generates a list of <code>Environment Variables</code> required by Heron to function.
 * @return A list of configured <code>Environment Variables</code> required by Heron to function.
 */
@VisibleForTesting
protected static List<V1EnvVar> getExecutorEnvVars() {
    final V1EnvVar envVarHost = new V1EnvVar();
    envVarHost.name(KubernetesConstants.ENV_HOST).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath(KubernetesConstants.POD_IP)));
    final V1EnvVar envVarPodName = new V1EnvVar();
    envVarPodName.name(KubernetesConstants.ENV_POD_NAME).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath(KubernetesConstants.POD_NAME)));
    return Arrays.asList(envVarHost, envVarPodName);
}
Also used : V1EnvVarSource(io.kubernetes.client.openapi.models.V1EnvVarSource) V1ObjectFieldSelector(io.kubernetes.client.openapi.models.V1ObjectFieldSelector) V1EnvVar(io.kubernetes.client.openapi.models.V1EnvVar) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with V1EnvVar

use of io.kubernetes.client.openapi.models.V1EnvVar in project heron by twitter.

the class V1ControllerTest method testConfigureContainerEnvVars.

@Test
public void testConfigureContainerEnvVars() {
    final List<V1EnvVar> heronEnvVars = Collections.unmodifiableList(V1Controller.getExecutorEnvVars());
    final V1EnvVar additionEnvVar = new V1EnvVar().name("env-variable-to-be-kept").valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("env-variable-was-kept")));
    final List<V1EnvVar> inputEnvVars = Arrays.asList(new V1EnvVar().name(KubernetesConstants.ENV_HOST).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("env-host-to-be-replaced"))), new V1EnvVar().name(KubernetesConstants.ENV_POD_NAME).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("pod-name-to-be-replaced"))), additionEnvVar);
    // Null env vars. This is the default case.
    V1Container containerWithNullEnvVars = new V1ContainerBuilder().build();
    v1ControllerWithPodTemplate.configureContainerEnvVars(containerWithNullEnvVars);
    Assert.assertTrue("ENV_HOST & ENV_POD_NAME in container with null Env Vars should match", CollectionUtils.containsAll(containerWithNullEnvVars.getEnv(), heronEnvVars));
    // Empty env vars.
    V1Container containerWithEmptyEnvVars = new V1ContainerBuilder().withEnv(new LinkedList<>()).build();
    v1ControllerWithPodTemplate.configureContainerEnvVars(containerWithEmptyEnvVars);
    Assert.assertTrue("ENV_HOST & ENV_POD_NAME in container with empty Env Vars should match", CollectionUtils.containsAll(containerWithEmptyEnvVars.getEnv(), heronEnvVars));
    // Env Var overriding.
    final List<V1EnvVar> expectedOverriding = new LinkedList<>(heronEnvVars);
    expectedOverriding.add(additionEnvVar);
    V1Container containerWithEnvVars = new V1ContainerBuilder().withEnv(inputEnvVars).build();
    v1ControllerWithPodTemplate.configureContainerEnvVars(containerWithEnvVars);
    Assert.assertTrue("ENV_HOST & ENV_POD_NAME in container with Env Vars should be overridden", CollectionUtils.containsAll(containerWithEnvVars.getEnv(), expectedOverriding));
}
Also used : V1Container(io.kubernetes.client.openapi.models.V1Container) V1EnvVarSource(io.kubernetes.client.openapi.models.V1EnvVarSource) V1ObjectFieldSelector(io.kubernetes.client.openapi.models.V1ObjectFieldSelector) V1ContainerBuilder(io.kubernetes.client.openapi.models.V1ContainerBuilder) V1EnvVar(io.kubernetes.client.openapi.models.V1EnvVar) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with V1EnvVar

use of io.kubernetes.client.openapi.models.V1EnvVar in project twister2 by DSC-SPIDAL.

the class RequestObjectBuilder method constructEnvironmentVariables.

/**
 * set environment variables for containers
 */
public static List<V1EnvVar> constructEnvironmentVariables(String containerName, int workerPort, int jvmMem) {
    ArrayList<V1EnvVar> envVars = getCommonEnvVars();
    envVars.add(new V1EnvVar().name(K8sEnvVariables.CONTAINER_NAME.name()).value(containerName));
    envVars.add(new V1EnvVar().name(K8sEnvVariables.JOB_MASTER_IP.name()).value(jobMasterIP));
    String classToRun = "edu.iu.dsc.tws.rsched.schedulers.k8s.worker.K8sWorkerStarter";
    if (SchedulerContext.usingOpenMPI(config)) {
        classToRun = "edu.iu.dsc.tws.rsched.schedulers.k8s.mpi.MPIMasterStarter";
    }
    envVars.add(new V1EnvVar().name(K8sEnvVariables.CLASS_TO_RUN.name()).value(classToRun));
    envVars.add(new V1EnvVar().name(K8sEnvVariables.WORKER_PORT.name()).value(workerPort + ""));
    envVars.add(new V1EnvVar().name(K8sEnvVariables.JVM_MEMORY_MB.name()).value(jvmMem + ""));
    return envVars;
}
Also used : V1EnvVar(io.kubernetes.client.openapi.models.V1EnvVar)

Example 4 with V1EnvVar

use of io.kubernetes.client.openapi.models.V1EnvVar in project heron by twitter.

the class KubernetesUtilsTest method testMergeListsDedupe.

@Test
public void testMergeListsDedupe() {
    final String description = "Pod Template Environment Variables";
    final List<V1EnvVar> heronEnvVars = Collections.unmodifiableList(V1Controller.getExecutorEnvVars());
    final V1EnvVar additionEnvVar = new V1EnvVar().name("env-variable-to-be-kept").valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("env-variable-was-kept")));
    final List<V1EnvVar> expectedEnvVars = Collections.unmodifiableList(new LinkedList<V1EnvVar>(V1Controller.getExecutorEnvVars()) {

        {
            add(additionEnvVar);
        }
    });
    final List<V1EnvVar> inputEnvVars = Arrays.asList(new V1EnvVar().name(KubernetesConstants.ENV_HOST).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("env-host-to-be-replaced"))), new V1EnvVar().name(KubernetesConstants.ENV_POD_NAME).valueFrom(new V1EnvVarSource().fieldRef(new V1ObjectFieldSelector().fieldPath("pod-name-to-be-replaced"))), additionEnvVar);
    KubernetesUtils.V1ControllerUtils<V1EnvVar> v1ControllerUtils = new KubernetesUtils.V1ControllerUtils<>();
    // Both input lists are null.
    Assert.assertNull("Both input lists are <null>", v1ControllerUtils.mergeListsDedupe(null, null, Comparator.comparing(V1EnvVar::getName), description));
    // <primaryList> is <null>.
    Assert.assertEquals("<primaryList> is null and <secondaryList> should be returned", inputEnvVars, v1ControllerUtils.mergeListsDedupe(null, inputEnvVars, Comparator.comparing(V1EnvVar::getName), description));
    // <primaryList> is empty.
    Assert.assertEquals("<primaryList> is empty and <secondaryList> should be returned", inputEnvVars, v1ControllerUtils.mergeListsDedupe(new LinkedList<>(), inputEnvVars, Comparator.comparing(V1EnvVar::getName), description));
    // <secondaryList> is <null>.
    Assert.assertEquals("<secondaryList> is null and <primaryList> should be returned", heronEnvVars, v1ControllerUtils.mergeListsDedupe(heronEnvVars, null, Comparator.comparing(V1EnvVar::getName), description));
    // <secondaryList> is empty.
    Assert.assertEquals("<secondaryList> is empty and <primaryList> should be returned", heronEnvVars, v1ControllerUtils.mergeListsDedupe(heronEnvVars, new LinkedList<>(), Comparator.comparing(V1EnvVar::getName), description));
    // Merge both lists.
    Assert.assertTrue("<primaryList> and <secondaryList> merged and deduplicated", expectedEnvVars.containsAll(v1ControllerUtils.mergeListsDedupe(heronEnvVars, inputEnvVars, Comparator.comparing(V1EnvVar::getName), description)));
    // Expect thrown error.
    String errorMessage = "";
    try {
        v1ControllerUtils.mergeListsDedupe(heronEnvVars, Collections.singletonList(new V1EnvVar()), Comparator.comparing(V1EnvVar::getName), description);
    } catch (TopologySubmissionException e) {
        errorMessage = e.getMessage();
    }
    Assert.assertTrue("Expecting error to be thrown for null deduplication key", errorMessage.contains(description));
}
Also used : TopologySubmissionException(org.apache.heron.scheduler.TopologySubmissionException) V1EnvVarSource(io.kubernetes.client.openapi.models.V1EnvVarSource) V1ObjectFieldSelector(io.kubernetes.client.openapi.models.V1ObjectFieldSelector) V1EnvVar(io.kubernetes.client.openapi.models.V1EnvVar) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 5 with V1EnvVar

use of io.kubernetes.client.openapi.models.V1EnvVar in project heron by twitter.

the class V1Controller method setSecretKeyRefs.

/**
 * Adds <code>Secret Key</code> references to a <code>container</code>.
 * @param container <code>container</code> to be configured.
 */
private void setSecretKeyRefs(V1Container container) {
    final Config config = getConfiguration();
    final Map<String, String> podSecretKeyRefs = KubernetesContext.getPodSecretKeyRefs(config);
    for (Map.Entry<String, String> secret : podSecretKeyRefs.entrySet()) {
        final String[] keyRefParts = secret.getValue().split(":");
        if (keyRefParts.length != 2) {
            LOG.log(Level.SEVERE, "SecretKeyRef must be in the form name:key. <" + secret.getValue() + ">");
            throw new TopologyRuntimeManagementException("SecretKeyRef must be in the form name:key. <" + secret.getValue() + ">");
        }
        String name = keyRefParts[0];
        String key = keyRefParts[1];
        final V1EnvVar envVar = new V1EnvVar().name(secret.getKey()).valueFrom(new V1EnvVarSource().secretKeyRef(new V1SecretKeySelector().key(key).name(name)));
        container.addEnvItem(envVar);
    }
}
Also used : V1EnvVarSource(io.kubernetes.client.openapi.models.V1EnvVarSource) TopologyRuntimeManagementException(org.apache.heron.scheduler.TopologyRuntimeManagementException) Config(org.apache.heron.spi.common.Config) V1SecretKeySelector(io.kubernetes.client.openapi.models.V1SecretKeySelector) V1EnvVar(io.kubernetes.client.openapi.models.V1EnvVar) Map(java.util.Map) HashMap(java.util.HashMap) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap)

Aggregations

V1EnvVar (io.kubernetes.client.openapi.models.V1EnvVar)7 V1EnvVarSource (io.kubernetes.client.openapi.models.V1EnvVarSource)5 V1ObjectFieldSelector (io.kubernetes.client.openapi.models.V1ObjectFieldSelector)4 LinkedList (java.util.LinkedList)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)1 V1Container (io.kubernetes.client.openapi.models.V1Container)1 V1ContainerBuilder (io.kubernetes.client.openapi.models.V1ContainerBuilder)1 V1SecretKeySelector (io.kubernetes.client.openapi.models.V1SecretKeySelector)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TopologyRuntimeManagementException (org.apache.heron.scheduler.TopologyRuntimeManagementException)1 TopologySubmissionException (org.apache.heron.scheduler.TopologySubmissionException)1 Config (org.apache.heron.spi.common.Config)1