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;
}
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;
}
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));
}
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;
}
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");
}
Aggregations