SubPathPrefixes { 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()) { 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) { continue; } String volumeSubPath = getVolumeMountSubpath( volumeMount, pvcName, workspaceId, Names.machineName(pod, c)); volumeMount.setSubPath(volumeSubPath); } }); } }  void prefixVolumeMountsSubpaths(KubernetesEnvironment k8sEnv, String workspaceId); String getWorkspaceSubPath(String workspaceId);  }
@Test public void shouldPrefixVolumeMountsSubpathsAndUsePvcNameAsVolumeName() { subpathPrefixes.prefixVolumeMountsSubpaths(k8sEnv, WORKSPACE_ID); PodSpec podSpec = k8sEnv.getPodsData().get(POD_1_NAME).getSpec(); io.fabric8.kubernetes.api.model.Volume userPodVolume = podSpec.getVolumes().get(0); assertEquals(userPodVolume.getPersistentVolumeClaim().getClaimName(), USER_DATA_PVC_NAME); assertEquals( podSpec.getVolumes().get(0).getPersistentVolumeClaim().getClaimName(), USER_DATA_PVC_NAME); Container initContainer = podSpec.getInitContainers().get(0); VolumeMount initVolumeMount = initContainer.getVolumeMounts().get(0); assertEquals( initVolumeMount.getSubPath(), WORKSPACE_ID + "/" + USER_DATA_PVC_NAME + "/tmp/init/userData"); assertEquals(initVolumeMount.getName(), userPodVolume.getName()); Container container = podSpec.getContainers().get(0); VolumeMount volumeMount = container.getVolumeMounts().get(0); assertEquals( volumeMount.getSubPath(), WORKSPACE_ID + "/" + USER_DATA_PVC_NAME + "/home/user/data"); assertEquals(volumeMount.getName(), userPodVolume.getName()); }
@Test public void shouldNotPrefixNotPVCSourcesVolumes() { Volume podVolume = pod.getSpec().getVolumes().get(0); podVolume.setPersistentVolumeClaim(null); podVolume.setConfigMap(new ConfigMapVolumeSourceBuilder().withName("configMap").build()); subpathPrefixes.prefixVolumeMountsSubpaths(k8sEnv, WORKSPACE_ID); PodSpec podSpec = k8sEnv.getPodsData().get(POD_1_NAME).getSpec(); io.fabric8.kubernetes.api.model.Volume podDataVolume = podSpec.getVolumes().get(0); Container initContainer = podSpec.getInitContainers().get(0); VolumeMount initVolumeMount = initContainer.getVolumeMounts().get(0); assertEquals(initVolumeMount.getSubPath(), "/tmp/init/userData"); assertEquals(initVolumeMount.getName(), podDataVolume.getName()); Container container = podSpec.getContainers().get(0); VolumeMount volumeMount = container.getVolumeMounts().get(0); assertEquals(volumeMount.getSubPath(), "/home/user/data"); assertEquals(volumeMount.getName(), podDataVolume.getName()); }
@Test public void shouldPrefixVolumeMountsSubpathsAndUseVolumeNameStoredInLabels() { String volumeName = "userDataVolume"; pvc.getMetadata().getLabels().put(CHE_VOLUME_NAME_LABEL, volumeName); subpathPrefixes.prefixVolumeMountsSubpaths(k8sEnv, WORKSPACE_ID); PodSpec podSpec = k8sEnv.getPodsData().get(POD_1_NAME).getSpec(); io.fabric8.kubernetes.api.model.Volume userPodVolume = podSpec.getVolumes().get(0); assertEquals(userPodVolume.getPersistentVolumeClaim().getClaimName(), USER_DATA_PVC_NAME); assertEquals( podSpec.getVolumes().get(0).getPersistentVolumeClaim().getClaimName(), USER_DATA_PVC_NAME); Container initContainer = podSpec.getInitContainers().get(0); VolumeMount initVolumeMount = initContainer.getVolumeMounts().get(0); assertEquals( initVolumeMount.getSubPath(), WORKSPACE_ID + "/" + volumeName + "/tmp/init/userData"); assertEquals(initVolumeMount.getName(), userPodVolume.getName()); Container container = podSpec.getContainers().get(0); VolumeMount volumeMount = container.getVolumeMounts().get(0); assertEquals(volumeMount.getSubPath(), WORKSPACE_ID + "/" + volumeName + "/home/user/data"); assertEquals(volumeMount.getName(), userPodVolume.getName()); }