Search in sources :

Example 1 with EnvVars

use of org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars in project che-server by eclipse-che.

the class EnvVarsConverter method provision.

@Override
@Traced
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
    TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);
    for (PodData pod : k8sEnv.getPodsData().values()) {
        for (Container container : pod.getSpec().getContainers()) {
            String machineName = Names.machineName(pod, container);
            InternalMachineConfig machineConf = k8sEnv.getMachines().get(machineName);
            // we need to combine the env vars from the machine config with the variables already
            // present in the container. Let's key the variables by name and use the map for merging
            Map<String, EnvVar> envVars = machineConf.getEnv().entrySet().stream().map(e -> new EnvVar(e.getKey(), e.getValue(), null)).collect(toMap(EnvVar::getName, identity()));
            // the env vars defined in our machine config take precedence over the ones already defined
            // in the container, if any
            container.getEnv().forEach(v -> envVars.putIfAbsent(v.getName(), v));
            // The environment variable expansion only works if a variable that is referenced
            // is already defined earlier in the list of environment variables.
            // We need to produce a list where variables that reference others always appear later
            // in the list.
            List<EnvVar> sorted = topoSort.sort(envVars.values());
            container.setEnv(sorted);
        }
    }
}
Also used : EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) ConfigurationProvisioner(org.eclipse.che.workspace.infrastructure.kubernetes.provision.ConfigurationProvisioner) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) EnvVars(org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars) Singleton(javax.inject.Singleton) Traced(org.eclipse.che.commons.annotation.Traced) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) TopologicalSort(org.eclipse.che.commons.lang.TopologicalSort) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) Collectors.toMap(java.util.stream.Collectors.toMap) TracingTags(org.eclipse.che.commons.tracing.TracingTags) Map(java.util.Map) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Function.identity(java.util.function.Function.identity) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) Container(io.fabric8.kubernetes.api.model.Container) Function.identity(java.util.function.Function.identity) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Traced(org.eclipse.che.commons.annotation.Traced)

Example 2 with EnvVars

use of org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars in project devspaces-images by redhat-developer.

the class EnvVarsConverter method provision.

@Override
@Traced
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
    TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);
    for (PodData pod : k8sEnv.getPodsData().values()) {
        for (Container container : pod.getSpec().getContainers()) {
            String machineName = Names.machineName(pod, container);
            InternalMachineConfig machineConf = k8sEnv.getMachines().get(machineName);
            // we need to combine the env vars from the machine config with the variables already
            // present in the container. Let's key the variables by name and use the map for merging
            Map<String, EnvVar> envVars = machineConf.getEnv().entrySet().stream().map(e -> new EnvVar(e.getKey(), e.getValue(), null)).collect(toMap(EnvVar::getName, identity()));
            // the env vars defined in our machine config take precedence over the ones already defined
            // in the container, if any
            container.getEnv().forEach(v -> envVars.putIfAbsent(v.getName(), v));
            // The environment variable expansion only works if a variable that is referenced
            // is already defined earlier in the list of environment variables.
            // We need to produce a list where variables that reference others always appear later
            // in the list.
            List<EnvVar> sorted = topoSort.sort(envVars.values());
            container.setEnv(sorted);
        }
    }
}
Also used : EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Container(io.fabric8.kubernetes.api.model.Container) MachineConfig(org.eclipse.che.api.core.model.workspace.config.MachineConfig) ConfigurationProvisioner(org.eclipse.che.workspace.infrastructure.kubernetes.provision.ConfigurationProvisioner) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) EnvVars(org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars) Singleton(javax.inject.Singleton) Traced(org.eclipse.che.commons.annotation.Traced) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) TopologicalSort(org.eclipse.che.commons.lang.TopologicalSort) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) Collectors.toMap(java.util.stream.Collectors.toMap) TracingTags(org.eclipse.che.commons.tracing.TracingTags) Map(java.util.Map) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) Function.identity(java.util.function.Function.identity) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) Container(io.fabric8.kubernetes.api.model.Container) Function.identity(java.util.function.Function.identity) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) Traced(org.eclipse.che.commons.annotation.Traced)

Aggregations

Container (io.fabric8.kubernetes.api.model.Container)2 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)2 List (java.util.List)2 Map (java.util.Map)2 Function.identity (java.util.function.Function.identity)2 Collectors.toMap (java.util.stream.Collectors.toMap)2 Singleton (javax.inject.Singleton)2 MachineConfig (org.eclipse.che.api.core.model.workspace.config.MachineConfig)2 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)2 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)2 InternalMachineConfig (org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig)2 Traced (org.eclipse.che.commons.annotation.Traced)2 TopologicalSort (org.eclipse.che.commons.lang.TopologicalSort)2 TracingTags (org.eclipse.che.commons.tracing.TracingTags)2 Names (org.eclipse.che.workspace.infrastructure.kubernetes.Names)2 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)2 PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)2 ConfigurationProvisioner (org.eclipse.che.workspace.infrastructure.kubernetes.provision.ConfigurationProvisioner)2 EnvVars (org.eclipse.che.workspace.infrastructure.kubernetes.util.EnvVars)2