UniqueNamesProvisioner implements ConfigurationProvisioner<T> { @Override @Traced public void provision(T k8sEnv, RuntimeIdentity identity) throws InfrastructureException { final String workspaceId = identity.getWorkspaceId(); TracingTags.WORKSPACE_ID.set(workspaceId); final Map<String, ConfigMap> configMaps = k8sEnv.getConfigMaps(); Map<String, String> configMapNameTranslation = new HashMap<>(); for (ConfigMap configMap : configMaps.values()) { final String originalName = configMap.getMetadata().getName(); putLabel(configMap.getMetadata(), Constants.CHE_ORIGINAL_NAME_LABEL, originalName); final String uniqueName = Names.uniqueResourceName(originalName, workspaceId); configMap.getMetadata().setName(uniqueName); configMapNameTranslation.put(originalName, uniqueName); } final Collection<PodData> podData = k8sEnv.getPodsData().values(); for (PodData pod : podData) { final ObjectMeta podMeta = pod.getMetadata(); putLabel(podMeta, Constants.CHE_ORIGINAL_NAME_LABEL, podMeta.getName()); final String podName = Names.uniqueResourceName(podMeta.getName(), workspaceId); podMeta.setName(podName); if (configMapNameTranslation.size() > 0) { rewriteConfigMapNames(pod, configMapNameTranslation); } } final Collection<Deployment> deployments = k8sEnv.getDeploymentsCopy().values(); for (Deployment deployment : deployments) { final ObjectMeta deploymentMeta = deployment.getMetadata(); final String originalName = deploymentMeta.getName(); putLabel(deploymentMeta, Constants.CHE_ORIGINAL_NAME_LABEL, originalName); final String deploymentName = Names.uniqueResourceName(originalName, workspaceId); deploymentMeta.setName(deploymentName); } final Set<Ingress> ingresses = new HashSet<>(k8sEnv.getIngresses().values()); k8sEnv.getIngresses().clear(); for (Ingress ingress : ingresses) { final ObjectMeta ingressMeta = ingress.getMetadata(); putLabel(ingress, Constants.CHE_ORIGINAL_NAME_LABEL, ingressMeta.getName()); final String ingressName = Names.generateName("ingress"); ingressMeta.setName(ingressName); k8sEnv.getIngresses().put(ingressName, ingress); } }  @Override @Traced void provision(T k8sEnv, RuntimeIdentity identity);  }
@Test public void provideUniquePodsNames() throws Exception { when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID); Pod pod = newPod(); PodData podData = new PodData(pod.getSpec(), pod.getMetadata()); doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData(); uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity); ObjectMeta podMetadata = pod.getMetadata(); assertNotEquals(podMetadata.getName(), POD_NAME); assertEquals(podMetadata.getLabels().get(CHE_ORIGINAL_NAME_LABEL), POD_NAME); }
@Test public void provideUniqueDeploymentsName() throws Exception { when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID); Deployment deployment = newDeployment(); doReturn(ImmutableMap.of(DEPLOYMENT_NAME, deployment)).when(k8sEnv).getDeploymentsCopy(); uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity); ObjectMeta deploymentMetadata = deployment.getMetadata(); assertNotEquals(deploymentMetadata.getName(), DEPLOYMENT_NAME); assertEquals(deploymentMetadata.getLabels().get(CHE_ORIGINAL_NAME_LABEL), DEPLOYMENT_NAME); }
@Test public void provideUniqueConfigMapName() throws Exception { when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID); ConfigMap configMap = newConfigMap(); doReturn(ImmutableMap.of(CONFIGMAP_NAME, configMap)).when(k8sEnv).getConfigMaps(); uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity); ObjectMeta configMapMetadata = configMap.getMetadata(); assertNotEquals(configMapMetadata.getName(), CONFIGMAP_NAME); assertEquals(configMapMetadata.getLabels().get(CHE_ORIGINAL_NAME_LABEL), CONFIGMAP_NAME); }
@Test public void rewritePodConfigMapEnv() throws Exception { when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID); ConfigMap configMap = newConfigMap(); doReturn(ImmutableMap.of(CONFIGMAP_NAME, configMap)).when(k8sEnv).getConfigMaps(); EnvVar envVar = new EnvVarBuilder() .withNewValueFrom() .withNewConfigMapKeyRef() .withName(CONFIGMAP_NAME) .withKey(CONFIGMAP_KEY) .endConfigMapKeyRef() .endValueFrom() .build(); Container container = new ContainerBuilder().withEnv(envVar).build(); Pod pod = newPod(); pod.getSpec().setContainers(ImmutableList.of(container)); PodData podData = new PodData(pod.getSpec(), pod.getMetadata()); doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData(); uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity); String newConfigMapName = configMap.getMetadata().getName(); EnvVar newEnvVar = container.getEnv().iterator().next(); assertEquals(newEnvVar.getValueFrom().getConfigMapKeyRef().getName(), newConfigMapName); }
@Test public void doesNotRewritePodConfigMapEnvWhenNoConfigMap() throws Exception { when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID); EnvVar envVar = new EnvVarBuilder() .withNewValueFrom() .withNewConfigMapKeyRef() .withName(CONFIGMAP_NAME) .withKey(CONFIGMAP_KEY) .endConfigMapKeyRef() .endValueFrom() .build(); Container container = new ContainerBuilder().withEnv(envVar).build(); Pod pod = newPod(); pod.getSpec().setContainers(ImmutableList.of(container)); PodData podData = new PodData(pod.getSpec(), pod.getMetadata()); doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData(); uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity); EnvVar newEnvVar = container.getEnv().iterator().next(); assertEquals(newEnvVar.getValueFrom().getConfigMapKeyRef().getName(), CONFIGMAP_NAME); }
@Test public void rewritePodConfigMapEnvFrom() throws Exception { when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID); ConfigMap configMap = newConfigMap(); doReturn(ImmutableMap.of(CONFIGMAP_NAME, configMap)).when(k8sEnv).getConfigMaps(); EnvFromSource envFrom = new EnvFromSourceBuilder() .withNewConfigMapRef() .withName(CONFIGMAP_NAME) .endConfigMapRef() .build(); Container container = new ContainerBuilder().withEnvFrom(envFrom).build(); Pod pod = newPod(); pod.getSpec().setContainers(ImmutableList.of(container)); PodData podData = new PodData(pod.getSpec(), pod.getMetadata()); doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData(); uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity); String newConfigMapName = configMap.getMetadata().getName(); EnvFromSource newEnvFromSource = container.getEnvFrom().iterator().next(); assertEquals(newEnvFromSource.getConfigMapRef().getName(), newConfigMapName); }
@Test public void doesNotRewritePodConfigMapEnvFromWhenNoConfigMap() throws Exception { when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID); EnvFromSource envFrom = new EnvFromSourceBuilder() .withNewConfigMapRef() .withName(CONFIGMAP_NAME) .endConfigMapRef() .build(); Container container = new ContainerBuilder().withEnvFrom(envFrom).build(); Pod pod = newPod(); pod.getSpec().setContainers(ImmutableList.of(container)); PodData podData = new PodData(pod.getSpec(), pod.getMetadata()); doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData(); uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity); EnvFromSource newEnvFromSource = container.getEnvFrom().iterator().next(); assertEquals(newEnvFromSource.getConfigMapRef().getName(), CONFIGMAP_NAME); }
@Test public void rewritePodConfigMapVolumes() throws Exception { when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID); ConfigMap configMap = newConfigMap(); doReturn(ImmutableMap.of(CONFIGMAP_NAME, configMap)).when(k8sEnv).getConfigMaps(); Volume volume = new VolumeBuilder().withNewConfigMap().withName(CONFIGMAP_NAME).endConfigMap().build(); Pod pod = newPod(); pod.getSpec().setVolumes(ImmutableList.of(volume)); PodData podData = new PodData(pod.getSpec(), pod.getMetadata()); doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData(); uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity); String newConfigMapName = configMap.getMetadata().getName(); Volume newVolume = pod.getSpec().getVolumes().iterator().next(); assertEquals(newVolume.getConfigMap().getName(), newConfigMapName); }
@Test public void doesNotRewritePodConfigMapVolumesWhenNoConfigMap() throws Exception { when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID); Volume volume = new VolumeBuilder().withNewConfigMap().withName(CONFIGMAP_NAME).endConfigMap().build(); Pod pod = newPod(); pod.getSpec().setVolumes(ImmutableList.of(volume)); PodData podData = new PodData(pod.getSpec(), pod.getMetadata()); doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData(); uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity); Volume newVolume = pod.getSpec().getVolumes().iterator().next(); assertEquals(newVolume.getConfigMap().getName(), CONFIGMAP_NAME); }
@Test public void provideUniqueIngressesNames() throws Exception { final HashMap<String, Ingress> ingresses = new HashMap<>(); Ingress ingress = newIngress(); ingresses.put(POD_NAME, ingress); doReturn(ingresses).when(k8sEnv).getIngresses(); uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity); final ObjectMeta ingressMetadata = ingress.getMetadata(); assertNotEquals(ingressMetadata.getName(), INGRESS_NAME); assertEquals(ingressMetadata.getLabels().get(CHE_ORIGINAL_NAME_LABEL), INGRESS_NAME); }