Search in sources :

Example 11 with EnvVar

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

the class ImageEnricher method mergeEnvVariables.

private void mergeEnvVariables(Container container) {
    List<EnvVar> env = container.getEnv();
    if (env == null) {
        env = new LinkedList<>();
        container.setEnv(env);
    }
    ResourceConfig resource = getContext().getResources();
    Map<String, String> userEnv = resource != null ? resource.getEnv() : null;
    if (userEnv != null) {
        for (Map.Entry<String, String> entry : userEnv.entrySet()) {
            EnvVar existingVariable = KubernetesResourceUtil.setEnvVarNoOverride(env, entry.getKey(), entry.getValue());
            if (existingVariable != null) {
                String actualValue = existingVariable.getValue();
                if (actualValue == null) {
                    actualValue = "retrieved using the downward API";
                }
                log.warn("Environment variable %s will not be overridden: trying to set the value %s, but its actual value will be %s", entry.getKey(), entry.getValue(), actualValue);
            }
        }
    }
}
Also used : EnvVar(io.fabric8.kubernetes.api.model.EnvVar) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) Map(java.util.Map)

Example 12 with EnvVar

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

the class MergeResourceTest method envVarToMap.

private Map<String, String> envVarToMap(List<EnvVar> envVars) {
    Map<String, String> answer = new HashMap<>();
    if (envVars != null) {
        for (EnvVar envVar : envVars) {
            String value = envVar.getValue();
            if (value != null && !value.isEmpty()) {
                String name = envVar.getName();
                answer.put(name, value);
            }
        }
    }
    return answer;
}
Also used : HashMap(java.util.HashMap) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 13 with EnvVar

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

the class DebugEnricher method enableDebugging.

private boolean enableDebugging(HasMetadata entity, PodTemplateSpec template) {
    if (template != null) {
        PodSpec podSpec = template.getSpec();
        if (podSpec != null) {
            List<Container> containers = podSpec.getContainers();
            if (containers.size() > 0) {
                Container container = containers.get(0);
                List<EnvVar> env = container.getEnv();
                if (env == null) {
                    env = new ArrayList<>();
                }
                String remoteDebugPort = KubernetesResourceUtil.getEnvVar(env, ENV_VAR_JAVA_DEBUG_PORT, ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
                boolean enabled = false;
                if (KubernetesResourceUtil.setEnvVar(env, ENV_VAR_JAVA_DEBUG, "true")) {
                    container.setEnv(env);
                    enabled = true;
                }
                List<ContainerPort> ports = container.getPorts();
                if (ports == null) {
                    ports = new ArrayList<>();
                }
                if (KubernetesResourceUtil.addPort(ports, remoteDebugPort, "debug", log)) {
                    container.setPorts(ports);
                    enabled = true;
                }
                if (enabled) {
                    log.info("Enabling debug on " + getKind(entity) + " " + KubernetesHelper.getName(entity) + " due to the property: " + ENABLE_DEBUG_MAVEN_PROPERTY);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 14 with EnvVar

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

the class KubernetesResourceUtil method removeEnvVar.

public static boolean removeEnvVar(List<EnvVar> envVarList, String name) {
    boolean removed = false;
    for (Iterator<EnvVar> it = envVarList.iterator(); it.hasNext(); ) {
        EnvVar envVar = it.next();
        String envVarName = envVar.getName();
        if (io.fabric8.utils.Objects.equal(name, envVarName)) {
            it.remove();
            removed = true;
        }
    }
    return removed;
}
Also used : EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 15 with EnvVar

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

the class KubernetesResourceUtil method setEnvVar.

public static boolean setEnvVar(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)) {
                return false;
            } else {
                envVar.setValue(value);
                return true;
            }
        }
    }
    EnvVar env = new EnvVarBuilder().withName(name).withValue(value).build();
    envVarList.add(env);
    return true;
}
Also used : EnvVar(io.fabric8.kubernetes.api.model.EnvVar) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder)

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