use of org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig in project che-server by eclipse-che.
the class JwtProxyProvisionerTest method shouldProvisionJwtProxyRelatedObjectsIntoKubernetesEnvironment.
@Test
public void shouldProvisionJwtProxyRelatedObjectsIntoKubernetesEnvironment() throws Exception {
// given
ServerConfigImpl secureServer = new ServerConfigImpl("4401/tcp", "ws", "/", emptyMap());
ServicePort port = new ServicePort();
port.setTargetPort(new IntOrString(4401));
// when
jwtProxyProvisioner.expose(k8sEnv, podWithName(), "machine", "terminal", port, "TCP", false, ImmutableMap.of("server", secureServer));
// then
InternalMachineConfig jwtProxyMachine = k8sEnv.getMachines().get(JwtProxyProvisioner.JWT_PROXY_MACHINE_NAME);
assertNotNull(jwtProxyMachine);
ConfigMap configMap = k8sEnv.getConfigMaps().get(jwtProxyProvisioner.getConfigMapName());
assertNotNull(configMap);
assertEquals(configMap.getData().get(JWT_PROXY_PUBLIC_KEY_FILE), PUBLIC_KEY_HEADER + Base64.getEncoder().encodeToString("publickey".getBytes()) + PUBLIC_KEY_FOOTER);
assertNotNull(configMap.getData().get(JWT_PROXY_CONFIG_FILE));
Pod jwtProxyPod = k8sEnv.getInjectablePodsCopy().getOrDefault("machine", emptyMap()).get("che-jwtproxy");
assertNotNull(jwtProxyPod);
assertEquals(1, jwtProxyPod.getSpec().getContainers().size());
Container jwtProxyContainer = jwtProxyPod.getSpec().getContainers().get(0);
assertEquals(jwtProxyContainer.getArgs().size(), 2);
assertEquals(jwtProxyContainer.getArgs().get(0), "-config");
assertEquals(jwtProxyContainer.getArgs().get(1), JWT_PROXY_CONFIG_FOLDER + "/" + JWT_PROXY_CONFIG_FILE);
assertEquals(jwtProxyContainer.getEnv().size(), 1);
EnvVar xdgHome = jwtProxyContainer.getEnv().get(0);
assertEquals(xdgHome.getName(), "XDG_CONFIG_HOME");
assertEquals(xdgHome.getValue(), JWT_PROXY_CONFIG_FOLDER);
Service jwtProxyService = k8sEnv.getServices().get(jwtProxyProvisioner.getServiceName());
assertNotNull(jwtProxyService);
}
use of org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig in project che-server by eclipse-che.
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 che-server by eclipse-che.
the class KubernetesInternalRuntime method storeStartingMachine.
/**
* Puts createdPod in the {@code machines} map and sends the starting event for this machine
*/
private void storeStartingMachine(Pod createdPod, ObjectMeta toCreateMeta, Map<String, InternalMachineConfig> machineConfigs, ServerResolver serverResolver) throws InfrastructureException {
final String workspaceId = getContext().getIdentity().getWorkspaceId();
for (Container container : createdPod.getSpec().getContainers()) {
String machineName = Names.machineName(toCreateMeta, container);
LOG.debug("Creating machine '{}' in workspace '{}'", machineName, workspaceId);
// Sometimes we facing NPE trying to retrieve machine config. Possible names mismatch. Need to
// get more info on that cases.
InternalMachineConfig machineConfig = Optional.ofNullable(machineConfigs.get(machineName)).orElseThrow(() -> {
LOG.error("Workspace '{}' start failed. Machine with name '{}' requested but not found in configs map. Present machines are: {}.", workspaceId, machineName, String.join(",", machineConfigs.keySet()));
return new InfrastructureException(format("Unable to start the workspace '%s' due to an internal inconsistency while composing the workspace runtime." + "Please report a bug. If possible, include the details from Che devfile and server log in bug report (your admin can help with that)", workspaceId));
});
machines.put(getContext().getIdentity(), new KubernetesMachineImpl(workspaceId, machineName, createdPod.getMetadata().getName(), container.getName(), MachineStatus.STARTING, machineConfig.getAttributes(), serverResolver.resolve(machineName)));
eventPublisher.sendStartingEvent(machineName, getContext().getIdentity());
}
}
use of org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig in project devspaces-images by redhat-developer.
the class ContainerResourceProvisioner method provision.
@Override
@Traced
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);
final Map<String, InternalMachineConfig> machines = k8sEnv.getMachines();
for (PodData pod : k8sEnv.getPodsData().values()) {
for (Container container : pod.getSpec().getContainers()) {
// make sure that machine configs have settings for RAM limit and request
InternalMachineConfig machineConfig = machines.get(machineName(pod, container));
ResourceLimitAttributesProvisioner.provisionMemory(machineConfig, Containers.getRamLimit(container), Containers.getRamRequest(container), defaultMachineMaxMemorySizeAttribute, defaultMachineRequestMemorySizeAttribute);
// make sure that machine configs have settings for CPU limit and request
ResourceLimitAttributesProvisioner.provisionCPU(machineConfig, Containers.getCpuLimit(container), Containers.getCpuRequest(container), defaultMachineCpuLimitAttribute, defaultMachineCpuRequestAttribute);
// reapply memory and CPU settings to k8s container to make sure that provisioned
// values above are set. Non-positive value means that limit is disabled, so just
// ignoring them.
final Map<String, String> attributes = machineConfig.getAttributes();
long memLimit = Long.parseLong(attributes.get(MEMORY_LIMIT_ATTRIBUTE));
if (memLimit > 0) {
Containers.addRamLimit(container, memLimit);
}
long memRequest = Long.parseLong(attributes.get(MEMORY_REQUEST_ATTRIBUTE));
if (memRequest > 0) {
Containers.addRamRequest(container, memRequest);
}
float cpuLimit = Float.parseFloat(attributes.get(CPU_LIMIT_ATTRIBUTE));
if (cpuLimit > 0) {
Containers.addCpuLimit(container, cpuLimit);
}
float cpuRequest = Float.parseFloat(attributes.get(CPU_REQUEST_ATTRIBUTE));
if (cpuRequest > 0)
Containers.addCpuRequest(container, cpuRequest);
}
}
}
use of org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig in project devspaces-images by redhat-developer.
the class ServersConverter method provision.
@Override
@Traced
public void provision(T k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);
SecureServerExposer<T> secureServerExposer = secureServerExposerFactoryProvider.get(k8sEnv).create(identity);
for (PodData podConfig : k8sEnv.getPodsData().values()) {
final PodSpec podSpec = podConfig.getSpec();
for (Container containerConfig : podSpec.getContainers()) {
String machineName = Names.machineName(podConfig, containerConfig);
InternalMachineConfig machineConfig = k8sEnv.getMachines().get(machineName);
if (!machineConfig.getServers().isEmpty()) {
KubernetesServerExposer kubernetesServerExposer = new KubernetesServerExposer<>(externalServerExposer, secureServerExposer, machineName, podConfig, containerConfig, k8sEnv);
kubernetesServerExposer.expose(machineConfig.getServers());
}
}
}
}
Aggregations