Search in sources :

Example 6 with EnvVarBuilder

use of io.fabric8.kubernetes.api.model.EnvVarBuilder in project che by eclipse.

the class KubernetesEnvVar method getEnvFrom.

/**
     * Retrieves list of {@link EnvVar} based on environment variables specified
     * in {@link ContainerConfig}
     *
     * @param envVariables
     * @return list of {@link EnvVar}
     */
public static List<EnvVar> getEnvFrom(String[] envVariables) {
    LOG.info("Container environment variables:");
    List<EnvVar> env = new ArrayList<>();
    for (String envVariable : envVariables) {
        String[] nameAndValue = envVariable.split("=", 2);
        String varName = nameAndValue[0];
        String varValue = nameAndValue[1];
        EnvVar envVar = new EnvVarBuilder().withName(varName).withValue(varValue).build();
        env.add(envVar);
        LOG.info("- {}={}", varName, varValue);
    }
    return env;
}
Also used : ArrayList(java.util.ArrayList) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder)

Example 7 with EnvVarBuilder

use of io.fabric8.kubernetes.api.model.EnvVarBuilder in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method setEnvVarNoOverride.

/**
 * Try to set an environment variable in the list or return the old value
 * if present and different from the current one.
 *
 * Environment variables will not be overridden.
 *
 * @param envVarList the list of environment variables
 * @param name the environment variable
 * @param value the value to set
 * @return the old value, if present, or null
 */
public static EnvVar setEnvVarNoOverride(List<EnvVar> envVarList, String name, String value) {
    for (EnvVar envVar : envVarList) {
        String envVarName = envVar.getName();
        if (io.fabric8.utils.Objects.equal(name, envVarName)) {
            String oldValue = envVar.getValue();
            if (io.fabric8.utils.Objects.equal(value, oldValue)) {
                // identical values
                return null;
            }
            return envVar;
        }
    }
    EnvVar env = new EnvVarBuilder().withName(name).withValue(value).build();
    envVarList.add(env);
    return null;
}
Also used : EnvVar(io.fabric8.kubernetes.api.model.EnvVar) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder)

Example 8 with EnvVarBuilder

use of io.fabric8.kubernetes.api.model.EnvVarBuilder in project fabric8-maven-plugin by fabric8io.

the class EnvVarHandlerTest method envVarHandlerWithoutNameTest.

@Test
public void envVarHandlerWithoutNameTest() {
    // Environment Variable without name in Config
    EnvVar var1 = new EnvVarBuilder().withName(null).withValue("OK").build();
    env.clear();
    env.put(null, var1.getValue());
    EnvVarHandler envVarHandler = new EnvVarHandler(project);
    new Expectations() {

        {
            externalEnvVarHandler.getExportedEnvironmentVariables(project, env);
            ret.put(null, "OK");
            result = ret;
        }
    };
    List<EnvVar> envVars = envVarHandler.getEnvironmentVariables(env);
    assertNotNull(envVars);
    assertEquals(1, envVars.size());
    assertTrue(!envVars.contains(var1));
    assertEquals("KUBERNETES_NAMESPACE", envVars.get(0).getName());
    assertTrue(envVars.contains(var4));
}
Also used : Expectations(mockit.Expectations) ExternalEnvVarHandler(io.fabric8.maven.core.extenvvar.ExternalEnvVarHandler) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) Test(org.junit.Test)

Example 9 with EnvVarBuilder

use of io.fabric8.kubernetes.api.model.EnvVarBuilder in project fabric8-maven-plugin by fabric8io.

the class EnvVarHandler method convertToEnvVarMap.

// ==============================================================================================
private Map<String, EnvVar> convertToEnvVarMap(Map<String, String> envs) {
    Map<String, EnvVar> envMap = new HashMap<>();
    for (Map.Entry<String, String> entry : envs.entrySet()) {
        String name = entry.getKey();
        String value = entry.getValue();
        if (name != null) {
            EnvVar env = new EnvVarBuilder().withName(name).withValue(value).build();
            envMap.put(name, env);
        }
    }
    return envMap;
}
Also used : EnvVar(io.fabric8.kubernetes.api.model.EnvVar) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder)

Example 10 with EnvVarBuilder

use of io.fabric8.kubernetes.api.model.EnvVarBuilder in project fabric8-maven-plugin by fabric8io.

the class MergeResourceTest method testMergeDeploymentMetadataAndEnvVars.

@Test
public void testMergeDeploymentMetadataAndEnvVars() throws Exception {
    Deployment resource = new DeploymentBuilder().withNewMetadata().withName("cheese").addToAnnotations("overwriteKey", "originalValue").addToAnnotations("unchangedKey", "shouldNotChange").addToAnnotations("unchangedBlankKey", "").addToAnnotations("deletedKey", "shouldBeDeleted").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withImage("cheese-image").addToEnv(new EnvVarBuilder().withName("ENV_UPDATED").withValue("OLD_VALUE").build()).addToEnv(new EnvVarBuilder().withName("ENV_UNMODIFIED").withValue("SHOULD_NOT_CHANGE").build()).addToEnv(new EnvVarBuilder().withName("ENV_DELETED").withValue("DELETE_ME").build()).endContainer().endSpec().endTemplate().endSpec().build();
    Deployment override = new DeploymentBuilder().withNewMetadata().withName("cheese").addToAnnotations("overwriteKey", "newValue").addToAnnotations("deletedKey", "").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().addToEnv(new EnvVarBuilder().withName("ENV_UPDATED").withValue("NEW_VALUE").build()).addToEnv(new EnvVarBuilder().withName("ENV_DELETED").withValue("").build()).addToEnv(new EnvVarBuilder().withName("ENV_ADDED").withValue("ADDED_VALUE").build()).endContainer().endSpec().endTemplate().endSpec().build();
    HasMetadata answer = KubernetesResourceUtil.mergeResources(resource, override, log, false);
    assertThat(answer).describedAs("mergeResult").isInstanceOf(Deployment.class);
    Deployment result = (Deployment) answer;
    log.info("Override metadata on Deployment generated: " + KubernetesHelper.toYaml(answer));
    Map<String, String> annotations = answer.getMetadata().getAnnotations();
    assertDataModified(annotations, "Deployment.metadata.annotations");
    assertDataNotModified(resource.getMetadata().getAnnotations(), "Original Deployment.metadata.annotations");
    assertEnvModified(result.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv(), "Deployment.spec.template.spec.containers[0].env");
    assertEnvNotModified(resource.getSpec().getTemplate().getSpec().getContainers().get(0).getEnv(), "Original Deployment.spec.template.spec.containers[0].env");
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder) Test(org.junit.Test)

Aggregations

EnvVarBuilder (io.fabric8.kubernetes.api.model.EnvVarBuilder)16 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)8 Test (org.junit.Test)7 Container (io.fabric8.kubernetes.api.model.Container)4 ArrayList (java.util.ArrayList)3 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)2 VolumeMountBuilder (io.fabric8.kubernetes.api.model.VolumeMountBuilder)2 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)2 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)2 HashMap (java.util.HashMap)2 CustomSizing (com.netflix.spinnaker.halyard.config.model.v1.node.CustomSizing)1 DeploymentEnvironment (com.netflix.spinnaker.halyard.config.model.v1.node.DeploymentEnvironment)1 ConfigSource (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ConfigSource)1 ServiceSettings (com.netflix.spinnaker.halyard.deploy.spinnaker.v1.service.ServiceSettings)1 KubernetesContainerClient (de.zalando.ep.zalenium.container.kubernetes.KubernetesContainerClient)1 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)1 HostAlias (io.fabric8.kubernetes.api.model.HostAlias)1 HostAliasBuilder (io.fabric8.kubernetes.api.model.HostAliasBuilder)1