use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.
the class PodMergerTest method shouldFailIfSecurityContextDiffersInPods.
@Test(expectedExceptions = ValidationException.class)
public void shouldFailIfSecurityContextDiffersInPods() throws Exception {
// given
PodSpec podSpec1 = new PodSpecBuilder().withSecurityContext(new PodSecurityContextBuilder().withRunAsUser(42L).build()).build();
podSpec1.setAdditionalProperty("add1", 1L);
PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());
PodSpec podSpec2 = new PodSpecBuilder().withSecurityContext(new PodSecurityContextBuilder().withRunAsUser(43L).build()).build();
podSpec2.setAdditionalProperty("add2", 2L);
PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());
// when
Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));
// then
// exception is thrown
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.
the class PodMergerTest method shouldGenerateInitContainerNamesIfCollisionHappened.
@Test
public void shouldGenerateInitContainerNamesIfCollisionHappened() throws Exception {
// given
PodSpec podSpec1 = new PodSpecBuilder().withInitContainers(new ContainerBuilder().withName("initC").build()).build();
PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());
PodSpec podSpec2 = new PodSpecBuilder().withInitContainers(new ContainerBuilder().withName("initC").build()).build();
PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());
// when
Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));
// then
PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
List<Container> initContainers = podTemplate.getSpec().getInitContainers();
assertEquals(initContainers.size(), 2);
Container container1 = initContainers.get(0);
assertEquals(container1.getName(), "initC");
Container container2 = initContainers.get(1);
assertNotEquals(container2.getName(), "initC");
assertTrue(container2.getName().startsWith("initC"));
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.
the class KubernetesArtifactsBrokerApplier method apply.
/**
* Apply plugin broker as init container to workspace environment. Workspace environment will have
* broker's configmap, machines, and volumes added in addition to the init container
*/
public void apply(E workspaceEnvironment, RuntimeIdentity runtimeID, Collection<PluginFQN> pluginFQNs, boolean mergePlugins) throws InfrastructureException {
E brokerEnvironment = brokerEnvironmentFactory.createForArtifactsBroker(pluginFQNs, runtimeID, mergePlugins);
Map<String, PodData> workspacePods = workspaceEnvironment.getPodsData();
if (workspacePods.size() != 1) {
throw new InfrastructureException("Che plugins tooling configuration can be applied to a workspace with one pod only.");
}
PodData workspacePod = workspacePods.values().iterator().next();
Map<String, PodData> brokerPods = brokerEnvironment.getPodsData();
if (brokerPods.size() != 1) {
throw new InfrastructureException("Broker environment must have only one Pod.");
}
PodData brokerPod = brokerPods.values().iterator().next();
// Add broker machines to workspace environment so that the init containers can be provisioned.
List<Container> brokerContainers = brokerPod.getSpec().getContainers();
for (Container container : brokerContainers) {
InternalMachineConfig brokerMachine = brokerEnvironment.getMachines().get(Names.machineName(brokerPod, container));
if (brokerMachine == null) {
throw new InfrastructureException(String.format("Could not retrieve the specification of the plugin broker container %s", container.getName()));
}
workspaceEnvironment.getMachines().put(Names.machineName(workspacePod, container), brokerMachine);
}
workspaceEnvironment.getConfigMaps().putAll(brokerEnvironment.getConfigMaps());
workspacePod.getSpec().getInitContainers().addAll(brokerPod.getSpec().getContainers());
workspacePod.getSpec().getVolumes().addAll(brokerPod.getSpec().getVolumes());
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.
the class KubernetesPluginsToolingApplier method addSidecar.
/**
* Adds k8s and Che specific configuration of a sidecar into the environment. For example:
* <li>k8s container configuration {@link Container}
* <li>k8s service configuration {@link Service}
* <li>Che machine config {@link InternalMachineConfig}
* <li>Fill in machine name attribute in related commands
*
* @throws InfrastructureException when any error occurs
*/
private void addSidecar(PodData pod, CheContainer container, ChePlugin chePlugin, KubernetesEnvironment k8sEnv, Collection<CommandImpl> sidecarRelatedCommands, Component pluginRelatedComponent, RuntimeIdentity runtimeIdentity) throws InfrastructureException {
K8sContainerResolver k8sContainerResolver = toK8sContainerResolver(container, chePlugin.getEndpoints());
List<ChePluginEndpoint> containerEndpoints = k8sContainerResolver.getEndpoints();
Container k8sContainer = k8sContainerResolver.resolve();
envVars.apply(k8sContainer, pluginRelatedComponent.getEnv());
chePluginsVolumeApplier.applyVolumes(pod, k8sContainer, container.getVolumes(), k8sEnv);
String machineName = k8sContainer.getName();
Names.putMachineName(pod.getMetadata(), k8sContainer.getName(), machineName);
pod.getSpec().getContainers().add(k8sContainer);
MachineResolver machineResolver = new MachineResolverBuilder().setCheContainer(container).setContainer(k8sContainer).setContainerEndpoints(containerEndpoints).setDefaultSidecarMemoryLimitAttribute(defaultSidecarMemoryLimitBytes).setDefaultSidecarMemoryRequestAttribute(defaultSidecarMemoryRequestBytes).setDefaultSidecarCpuLimitAttribute(defaultSidecarCpuLimitCores).setDefaultSidecarCpuRequestAttribute(defaultSidecarCpuRequestCores).setProjectsRootPathEnvVar(projectsRootEnvVariableProvider.get(runtimeIdentity)).setComponent(pluginRelatedComponent).build();
InternalMachineConfig machineConfig = machineResolver.resolve();
machineConfig.getAttributes().put(CONTAINER_SOURCE_ATTRIBUTE, TOOL_CONTAINER_SOURCE);
machineConfig.getAttributes().put(PLUGIN_MACHINE_ATTRIBUTE, chePlugin.getId());
k8sEnv.getMachines().put(machineName, machineConfig);
sidecarRelatedCommands.forEach(c -> c.getAttributes().put(org.eclipse.che.api.core.model.workspace.config.Command.MACHINE_NAME_ATTRIBUTE, machineName));
container.getCommands().stream().map(c -> asCommand(machineName, c)).forEach(c -> k8sEnv.getCommands().add(c));
SidecarServicesProvisioner sidecarServicesProvisioner = new SidecarServicesProvisioner(containerEndpoints, pod.getMetadata().getName());
sidecarServicesProvisioner.provision(k8sEnv);
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.
the class KubernetesPluginsToolingApplier method apply.
@Override
public void apply(RuntimeIdentity runtimeIdentity, InternalEnvironment internalEnvironment, Collection<ChePlugin> chePlugins) throws InfrastructureException {
if (chePlugins.isEmpty()) {
return;
}
KubernetesEnvironment k8sEnv = (KubernetesEnvironment) internalEnvironment;
Map<String, PodData> pods = k8sEnv.getPodsData();
switch(pods.size()) {
case 0:
addToolingPod(k8sEnv);
pods = k8sEnv.getPodsData();
break;
case 1:
break;
default:
throw new InfrastructureException("Che plugins tooling configuration can be applied to a workspace with one pod only");
}
PodData pod = pods.values().iterator().next();
CommandsResolver commandsResolver = new CommandsResolver(k8sEnv);
for (ChePlugin chePlugin : chePlugins) {
Map<String, ComponentImpl> devfilePlugins = k8sEnv.getDevfile().getComponents().stream().filter(c -> c.getType().equals("cheEditor") || c.getType().equals("chePlugin")).collect(Collectors.toMap(ComponentImpl::getId, Function.identity()));
if (!devfilePlugins.containsKey(chePlugin.getId())) {
throw new InfrastructureException(String.format("The downloaded plugin '%s' configuration does not have the " + "corresponding component in devfile. Devfile contains the following cheEditor/chePlugins: %s", chePlugin.getId(), devfilePlugins.keySet()));
}
ComponentImpl pluginRelatedComponent = devfilePlugins.get(chePlugin.getId());
for (CheContainer container : chePlugin.getInitContainers()) {
Container k8sInitContainer = toK8sContainer(container);
envVars.apply(k8sInitContainer, pluginRelatedComponent.getEnv());
chePluginsVolumeApplier.applyVolumes(pod, k8sInitContainer, container.getVolumes(), k8sEnv);
pod.getSpec().getInitContainers().add(k8sInitContainer);
}
Collection<CommandImpl> pluginRelatedCommands = commandsResolver.resolve(chePlugin);
for (CheContainer container : chePlugin.getContainers()) {
addSidecar(pod, container, chePlugin, k8sEnv, pluginRelatedCommands, pluginRelatedComponent, runtimeIdentity);
}
}
chePlugins.forEach(chePlugin -> populateWorkspaceEnvVars(chePlugin, k8sEnv));
}
Aggregations