use of org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig in project devspaces-images by redhat-developer.
the class KubernetesInternalRuntime method doStartMachine.
/**
* Creates Kubernetes pods and resolves servers using the specified serverResolver.
*
* @param serverResolver server resolver that provide servers by container
* @throws InfrastructureException when any error occurs while creating Kubernetes pods
*/
@Traced
protected void doStartMachine(ServerResolver serverResolver) throws InfrastructureException {
final KubernetesEnvironment environment = getContext().getEnvironment();
final Map<String, InternalMachineConfig> machineConfigs = environment.getMachines();
final String workspaceId = getContext().getIdentity().getWorkspaceId();
LOG.debug("Begin pods creation for workspace '{}'", workspaceId);
PodMerger podMerger = new PodMerger();
Map<String, Map<String, Pod>> injectablePods = environment.getInjectablePodsCopy();
for (Pod toCreate : environment.getPodsCopy().values()) {
ObjectMeta toCreateMeta = toCreate.getMetadata();
List<PodData> injectables = getAllInjectablePods(toCreate, injectablePods);
Pod createdPod;
if (injectables.isEmpty()) {
createdPod = namespace.deployments().deploy(toCreate);
} else {
try {
injectables.add(new PodData(toCreate));
Deployment merged = podMerger.merge(injectables);
merged.getMetadata().setName(toCreate.getMetadata().getName());
createdPod = namespace.deployments().deploy(merged);
} catch (ValidationException e) {
throw new InfrastructureException(e);
}
}
LOG.debug("Creating pod '{}' in workspace '{}'", toCreateMeta.getName(), workspaceId);
storeStartingMachine(createdPod, createdPod.getMetadata(), machineConfigs, serverResolver);
}
for (Deployment toCreate : environment.getDeploymentsCopy().values()) {
PodTemplateSpec template = toCreate.getSpec().getTemplate();
List<PodData> injectables = getAllInjectablePods(template.getMetadata(), template.getSpec().getContainers(), injectablePods);
Pod createdPod;
if (injectables.isEmpty()) {
createdPod = namespace.deployments().deploy(toCreate);
} else {
try {
injectables.add(new PodData(toCreate));
Deployment deployment = podMerger.merge(injectables);
deployment.getMetadata().setName(toCreate.getMetadata().getName());
putAnnotations(deployment.getMetadata(), toCreate.getMetadata().getAnnotations());
putLabels(deployment.getMetadata(), toCreate.getMetadata().getLabels());
createdPod = namespace.deployments().deploy(deployment);
} catch (ValidationException e) {
throw new InfrastructureException(e);
}
}
LOG.debug("Creating deployment '{}' in workspace '{}'", createdPod.getMetadata().getName(), workspaceId);
storeStartingMachine(createdPod, createdPod.getMetadata(), machineConfigs, serverResolver);
}
LOG.debug("Pods creation finished in workspace '{}'", workspaceId);
}
use of org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig in project devspaces-images by redhat-developer.
the class DockerimageComponentToWorkspaceApplierTest method testGeneratesValidMachineNameFromImageName.
@Test(dataProvider = "imageNames")
public void testGeneratesValidMachineNameFromImageName(String imageName) throws ValidationException, DevfileException {
// given
String machineName = DockerimageComponentToWorkspaceApplier.toMachineName(imageName);
MachineConfigsValidator validator = new MachineConfigsValidator();
Map<String, InternalMachineConfig> configs = new HashMap<>();
configs.put(machineName, new InternalMachineConfig());
// when
validator.validate(configs);
// then no exception is thrown
}
use of org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig 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.api.workspace.server.spi.environment.InternalMachineConfig 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.api.workspace.server.spi.environment.InternalMachineConfig in project devspaces-images by redhat-developer.
the class MachineResolver method resolve.
public InternalMachineConfig resolve() throws InfrastructureException {
InternalMachineConfig machineConfig = new InternalMachineConfig(toServers(containerEndpoints), emptyMap(), resolveMachineAttributes(), toWorkspaceVolumes(cheContainer));
applyDevfileVolumes(machineConfig);
applyDevfileEndpoints(machineConfig);
normalizeMemory(container, machineConfig);
normalizeCpu(container, machineConfig);
return machineConfig;
}
Aggregations