VcsSslCertificateProvisioner implements ConfigurationProvisioner<KubernetesEnvironment> { @Override public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException { if (!isConfigured()) { return; } String selfSignedCertConfigMapName = identity.getWorkspaceId() + CHE_GIT_SELF_SIGNED_CERT_CONFIG_MAP_SUFFIX; k8sEnv .getConfigMaps() .put( selfSignedCertConfigMapName, new ConfigMapBuilder() .withNewMetadata() .withName(selfSignedCertConfigMapName) .endMetadata() .withData(singletonMap(CA_CERT_FILE, certificate)) .build()); for (PodData pod : k8sEnv.getPodsData().values()) { if (pod.getRole() != PodRole.INJECTABLE) { if (pod.getSpec() .getVolumes() .stream() .noneMatch(v -> v.getName().equals(CHE_GIT_SELF_SIGNED_VOLUME))) { pod.getSpec().getVolumes().add(buildCertVolume(selfSignedCertConfigMapName)); } } for (Container container : pod.getSpec().getInitContainers()) { provisionCertVolumeMountIfNeeded(container); } for (Container container : pod.getSpec().getContainers()) { provisionCertVolumeMountIfNeeded(container); } } } VcsSslCertificateProvisioner(); @VisibleForTesting  VcsSslCertificateProvisioner(String certificate, String host); boolean isConfigured(); String getCertPath(); String getGitServerHost(); @Override void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity);  }
@Test public void shouldAddConfigMapWithCertificateIntoEnvironment() throws Exception { provisioner.provision(k8sEnv, runtimeId); Map<String, ConfigMap> configMaps = k8sEnv.getConfigMaps(); assertEquals(configMaps.size(), 1); ConfigMap configMap = configMaps.get(EXPECTED_CERT_NAME); assertNotNull(configMap); assertEquals(configMap.getMetadata().getName(), EXPECTED_CERT_NAME); assertEquals(configMap.getData().get(CA_CERT_FILE), CERT_CONTENT); }
@Test public void shouldAddVolumeAndVolumeMountsToPodsAndContainersInEnvironment() throws Exception { k8sEnv.addPod(createPod("pod")); k8sEnv.addPod(createPod("pod2")); provisioner.provision(k8sEnv, runtimeId); for (Pod pod : k8sEnv.getPodsCopy().values()) { verifyVolumeIsPresent(pod); for (Container container : pod.getSpec().getInitContainers()) { verifyVolumeMountIsPresent(container); } for (Container container : pod.getSpec().getContainers()) { verifyVolumeMountIsPresent(container); } } }
@Test public void shouldNotAddVolumeAndVolumeMountsToPodsAndContainersInEnvironmentIfCertIsNotConfigured() throws Exception { provisioner = new VcsSslCertificateProvisioner("", ""); k8sEnv.addPod(createPod("pod")); k8sEnv.addPod(createPod("pod2")); provisioner.provision(k8sEnv, runtimeId); for (Pod pod : k8sEnv.getPodsCopy().values()) { assertTrue(pod.getSpec().getVolumes().isEmpty()); for (Container container : pod.getSpec().getContainers()) { assertTrue(container.getVolumeMounts().isEmpty()); } } }
@Test public void shouldNotAddVolumeButAddVolumeMountsToInjectablePods() throws Exception { k8sEnv.addPod(createPod("pod")); k8sEnv.addInjectablePod("r", "i", createPod("pod2")); provisioner.provision(k8sEnv, runtimeId); for (Pod pod : k8sEnv.getPodsCopy().values()) { verifyVolumeIsPresent(pod); for (Container container : pod.getSpec().getInitContainers()) { verifyVolumeMountIsPresent(container); } for (Container container : pod.getSpec().getContainers()) { verifyVolumeMountIsPresent(container); } } for (Pod pod : k8sEnv .getInjectablePodsCopy() .values() .stream() .flatMap(v -> v.values().stream()) .toArray(Pod[]::new)) { assertTrue(pod.getSpec().getVolumes().isEmpty()); for (Container container : pod.getSpec().getInitContainers()) { verifyVolumeMountIsPresent(container); } for (Container container : pod.getSpec().getContainers()) { verifyVolumeMountIsPresent(container); } } }