Search in sources :

Example 61 with PodData

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)));
}
Also used : MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) OpenShiftProjectFactory(org.eclipse.che.workspace.infrastructure.openshift.project.OpenShiftProjectFactory) Listeners(org.testng.annotations.Listeners) Mock(org.mockito.Mock) OpenShiftProject(org.eclipse.che.workspace.infrastructure.openshift.project.OpenShiftProject) Assert.assertEquals(org.testng.Assert.assertEquals) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) Mockito.lenient(org.mockito.Mockito.lenient) Resource(io.fabric8.kubernetes.client.dsl.Resource) CheServerKubernetesClientFactory(org.eclipse.che.workspace.infrastructure.kubernetes.CheServerKubernetesClientFactory) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Map(java.util.Map) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Mockito.doReturn(org.mockito.Mockito.doReturn) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) KubernetesConfigsMaps(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesConfigsMaps) MockitoTestNGListener(org.mockito.testng.MockitoTestNGListener) ImmutableMap(com.google.common.collect.ImmutableMap) CheInstallationLocation(org.eclipse.che.workspace.infrastructure.kubernetes.environment.CheInstallationLocation) BeforeMethod(org.testng.annotations.BeforeMethod) ConfigMapList(io.fabric8.kubernetes.api.model.ConfigMapList) Pod(io.fabric8.kubernetes.api.model.Pod) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) ImmutableMap.of(com.google.common.collect.ImmutableMap.of) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Assert.assertTrue(org.testng.Assert.assertTrue) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Pod(io.fabric8.kubernetes.api.model.Pod) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Test(org.testng.annotations.Test)

Example 62 with PodData

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);
}
Also used : PodMerger(org.eclipse.che.workspace.infrastructure.kubernetes.environment.PodMerger) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ValidationException(org.eclipse.che.api.core.ValidationException) Pod(io.fabric8.kubernetes.api.model.Pod) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap) Collections.emptyMap(java.util.Collections.emptyMap) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) InternalInfrastructureException(org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException) Traced(org.eclipse.che.commons.annotation.Traced)

Example 63 with PodData

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());
        });
    }
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) EmptyDirVolumeSource(io.fabric8.kubernetes.api.model.EmptyDirVolumeSource) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Map(java.util.Map) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) EmptyDirVolumeSource(io.fabric8.kubernetes.api.model.EmptyDirVolumeSource) PodSpec(io.fabric8.kubernetes.api.model.PodSpec)

Example 64 with PodData

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));
    }
}
Also used : HashSet(java.util.HashSet) List(java.util.List) Stream(java.util.stream.Stream) Iterator(java.util.Iterator) Collection(java.util.Collection) Map(java.util.Map) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Set(java.util.Set) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder)

Example 65 with PodData

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);
            }
        });
    }
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) HashMap(java.util.HashMap) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim)

Aggregations

PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)156 Test (org.testng.annotations.Test)86 Container (io.fabric8.kubernetes.api.model.Container)62 Pod (io.fabric8.kubernetes.api.model.Pod)56 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)52 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)52 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)40 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)36 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)34 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)30 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)28 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)26 Map (java.util.Map)22 InternalMachineConfig (org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig)22 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)20 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)20 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)20 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)16 Volume (io.fabric8.kubernetes.api.model.Volume)16 HashMap (java.util.HashMap)16