use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.
the class OpenshiftTrustedCAProvisionerTest method shouldProvisionTrustStoreMapAndMountIt.
@Test
public void shouldProvisionTrustStoreMapAndMountIt() throws Exception {
Pod pod = newPod();
PodData podData = new PodData(pod.getSpec(), pod.getMetadata());
doReturn(of(POD_NAME, podData)).when(k8sEnv).getPodsData();
lenient().when(cheServerConfigMapResource.get()).thenReturn(cheServerConfigMap);
trustedCAProvisioner.provision(k8sEnv, runtimeID);
assertEquals(envConfigMaps.size(), 1);
assertTrue(envConfigMaps.get(CONFIGMAP_NAME).getMetadata().getLabels().containsKey("foo"));
assertEquals(envConfigMaps.get(CONFIGMAP_NAME).getMetadata().getLabels().get("foo"), "bar");
assertEquals(envConfigMaps.get(CONFIGMAP_NAME).getMetadata().getAnnotations(), cheServerConfigMapAnnotations);
assertEquals(envConfigMaps.get(CONFIGMAP_NAME).getData(), cheServerConfigMapData);
PodSpec podSpec = pod.getSpec();
assertEquals(podSpec.getVolumes().size(), 1);
assertEquals(podSpec.getVolumes().get(0).getConfigMap().getName(), CONFIGMAP_NAME);
assertTrue(podSpec.getContainers().stream().allMatch(c -> c.getVolumeMounts().get(0).getMountPath().equals(CERTIFICATE_MOUNT_PATH)));
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData 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.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.
the class EphemeralWorkspaceAdapter method replacePVCsWithEmptyDir.
private void replacePVCsWithEmptyDir(KubernetesEnvironment k8sEnv) {
for (PodData pod : k8sEnv.getPodsData().values()) {
PodSpec podSpec = pod.getSpec();
podSpec.getVolumes().stream().filter(v -> v.getPersistentVolumeClaim() != null).forEach(v -> {
v.setPersistentVolumeClaim(null);
v.setEmptyDir(new EmptyDirVolumeSource());
});
}
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.
the class PodsVolumes method replacePVCVolumesWithCommon.
/**
* Replaces all pods PVC sourced volumes with the specified one.
*
* @param pods pods to change
* @param commonPVCName PVC name that should be referenced in all existing PVC sources volumes
*/
public void replacePVCVolumesWithCommon(Map<String, PodData> pods, String commonPVCName) {
for (PodData pod : pods.values()) {
Set<String> pvcSourcedVolumes = reducePVCSourcedVolumes(pod.getSpec().getVolumes());
if (pvcSourcedVolumes.isEmpty()) {
continue;
}
// add common PVC sourced volume instead of removed
pod.getSpec().getVolumes().add(new VolumeBuilder().withName(commonPVCName).withNewPersistentVolumeClaim().withClaimName(commonPVCName).endPersistentVolumeClaim().build());
Stream.concat(pod.getSpec().getContainers().stream(), pod.getSpec().getInitContainers().stream()).flatMap(c -> c.getVolumeMounts().stream()).filter(vm -> pvcSourcedVolumes.contains(vm.getName())).forEach(vm -> vm.setName(commonPVCName));
}
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project devspaces-images by redhat-developer.
the class SubPathPrefixes method prefixVolumeMountsSubpaths.
/**
* Prefixes volumes mounts of containers inside of the specified kubernetes environment.
*
* <p>Subpaths have the following format: '{workspaceId}/{Che Volume name|PVC name}'.<br>
* Where Che Volume is used if it is present in PVC labels, otherwise PVC name will be used.<br>
* Note that logs volume has the special format: '{workspaceId}/{volumeName}/{machineName}'. It is
* done in this way to avoid conflicts e.g. two identical agents inside different machines produce
* the same log file.
*
* @param k8sEnv environment to process
* @param workspaceId workspace id that should be used as prefix
*/
public void prefixVolumeMountsSubpaths(KubernetesEnvironment k8sEnv, String workspaceId) {
for (PodData pod : k8sEnv.getPodsData().values()) {
Map<String, String> volumeToCheVolumeName = new HashMap<>();
for (io.fabric8.kubernetes.api.model.Volume volume : pod.getSpec().getVolumes()) {
if (volume.getPersistentVolumeClaim() == null) {
continue;
}
PersistentVolumeClaim pvc = k8sEnv.getPersistentVolumeClaims().get(volume.getPersistentVolumeClaim().getClaimName());
String cheVolumeName = pvc.getMetadata().getLabels().get(CHE_VOLUME_NAME_LABEL);
if (cheVolumeName == null) {
cheVolumeName = pvc.getMetadata().getName();
pvc.getMetadata().getLabels().put(CHE_VOLUME_NAME_LABEL, cheVolumeName);
}
volumeToCheVolumeName.put(volume.getName(), cheVolumeName);
}
if (volumeToCheVolumeName.isEmpty()) {
// Pod does not have any volume that references PVC
continue;
}
Stream.concat(pod.getSpec().getContainers().stream(), pod.getSpec().getInitContainers().stream()).forEach(c -> {
for (VolumeMount volumeMount : c.getVolumeMounts()) {
String pvcName = volumeToCheVolumeName.get(volumeMount.getName());
if (pvcName == null) {
// validation
continue;
}
String volumeSubPath = getVolumeMountSubpath(volumeMount, pvcName, workspaceId, Names.machineName(pod, c));
volumeMount.setSubPath(volumeSubPath);
}
});
}
}
Aggregations