PodsVolumes { 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; } 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)); } }  void changePVCReferences(
      Collection<PodData> pods, String currentPVCName, String newPVCName); void replacePVCVolumesWithCommon(Map<String, PodData> pods, String commonPVCName);  }
@Test public void shouldReplaceVolumesWithCommon() { podData .getSpec() .getInitContainers() .add( new ContainerBuilder() .withName("userInitContainer") .withVolumeMounts( new VolumeMountBuilder() .withName("initData") .withSubPath("/tmp/init/userData") .build()) .build()); podData .getSpec() .getContainers() .get(0) .getVolumeMounts() .add(new VolumeMountBuilder().withName("userData").withSubPath("/home/user/data").build()); podData .getSpec() .getVolumes() .add( new VolumeBuilder() .withName("userData") .withPersistentVolumeClaim( new PersistentVolumeClaimVolumeSourceBuilder() .withClaimName("userDataPVC") .build()) .build()); podData .getSpec() .getVolumes() .add( new VolumeBuilder() .withName("initData") .withPersistentVolumeClaim( new PersistentVolumeClaimVolumeSourceBuilder() .withClaimName("initDataPVC") .build()) .build()); podsVolumes.replacePVCVolumesWithCommon(ImmutableMap.of("pod", podData), "commonPVC"); assertEquals(podData.getSpec().getVolumes().size(), 1); assertEquals( podData.getSpec().getVolumes().get(0).getPersistentVolumeClaim().getClaimName(), "commonPVC"); assertEquals( podData.getSpec().getInitContainers().get(0).getVolumeMounts().get(0).getName(), "commonPVC"); assertEquals( podData.getSpec().getContainers().get(0).getVolumeMounts().get(0).getName(), "commonPVC"); }
@Test public void shouldNotReplaceNonPVCVolumes() { podData .getSpec() .getInitContainers() .add( new ContainerBuilder() .withName("userInitContainer") .withVolumeMounts(new VolumeMountBuilder().withName("configMap").build()) .build()); podData .getSpec() .getContainers() .get(0) .getVolumeMounts() .add(new VolumeMountBuilder().withName("secret").withSubPath("/home/user/data").build()); podData .getSpec() .getVolumes() .add( new VolumeBuilder() .withName("configMap") .withConfigMap(new ConfigMapVolumeSourceBuilder().withName("configMap").build()) .build()); podData .getSpec() .getVolumes() .add( new VolumeBuilder() .withName("secret") .withSecret(new SecretVolumeSourceBuilder().withSecretName("secret").build()) .build()); podsVolumes.replacePVCVolumesWithCommon(ImmutableMap.of("pod", podData), "commonPVC"); assertEquals(podData.getSpec().getVolumes().size(), 2); assertNotNull(podData.getSpec().getVolumes().get(0).getConfigMap()); assertNull(podData.getSpec().getVolumes().get(0).getPersistentVolumeClaim()); assertNotNull(podData.getSpec().getVolumes().get(1).getSecret()); assertNull(podData.getSpec().getVolumes().get(1).getPersistentVolumeClaim()); assertEquals( podData.getSpec().getInitContainers().get(0).getVolumeMounts().get(0).getName(), "configMap"); assertEquals( podData.getSpec().getContainers().get(0).getVolumeMounts().get(0).getName(), "secret"); }