diff --git a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/Constants.java b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/Constants.java index f7f16ec4e53..e3c485e3243 100644 --- a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/Constants.java +++ b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/Constants.java @@ -37,13 +37,6 @@ public final class Constants { /** The label that contains a value with volume name. */ public static final String CHE_VOLUME_NAME_LABEL = "che.workspace.volume_name"; - /** - * The annotation with the wildcard reserved for container name. Formatted annotation with the - * real container name is used to get machine name. - */ - public static final String MACHINE_NAME_ANNOTATION_FMT = - "org.eclipse.che.container.%s.machine_name"; - /** Kubernetes Pod status phase values */ public static final String POD_STATUS_PHASE_RUNNING = "Running"; diff --git a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/Names.java b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/Names.java index 6e25a0b8c63..43bccca512f 100644 --- a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/Names.java +++ b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/Names.java @@ -13,15 +13,14 @@ import static java.lang.String.format; import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.CHE_ORIGINAL_NAME_LABEL; -import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.MACHINE_NAME_ANNOTATION_FMT; +import com.google.common.collect.Maps; import io.fabric8.kubernetes.api.model.Container; import io.fabric8.kubernetes.api.model.ObjectMeta; -import io.fabric8.kubernetes.api.model.Pod; +import java.util.HashMap; import java.util.Map; import org.eclipse.che.commons.lang.NameGenerator; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData; -import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil; /** * Helps to work with Kubernetes objects names. @@ -30,18 +29,14 @@ */ public class Names { - static final char WORKSPACE_ID_PREFIX_SEPARATOR = '.'; + public static final int MAX_CONTAINER_NAME_LENGTH = 63; // K8S container name limit - static final int GENERATED_PART_SIZE = 8; + private static final char WORKSPACE_ID_PREFIX_SEPARATOR = '.'; - /** - * Returns machine name for the specified container in the specified pod. - * - *

This is a convenience method for {@link #machineName(ObjectMeta, Container)} - */ - public static String machineName(Pod pod, Container container) { - return machineName(pod.getMetadata(), container); - } + private static final int GENERATED_PART_SIZE = 8; + + private static final String CONTAINER_DISPLAY_NAMES_FMT = + "org.eclipse.che.container.display-name/%s"; /** * Returns machine name for the specified container in the specified pod. @@ -52,10 +47,60 @@ public static String machineName(PodData podData, Container container) { return machineName(podData.getMetadata(), container); } + /** + * Records the machine name used for given container in the annotations of the object metadata. + * + * @param objectMeta the object metadata + * @param containerName the name of the container + * @param machineName the name of the machine of the container + */ public static void putMachineName( ObjectMeta objectMeta, String containerName, String machineName) { - KubernetesObjectUtil.putAnnotation( - objectMeta, format(MACHINE_NAME_ANNOTATION_FMT, containerName), machineName); + + Map annotations = objectMeta.getAnnotations(); + if (annotations == null) { + objectMeta.setAnnotations(annotations = new HashMap<>()); + } + + putMachineName(annotations, containerName, machineName); + } + + /** + * Transforms the provided mapping of container names to machine names into a map of annotations + * to be put in some Kubernetes object's metadata. + * + * @param containerToMachineNames the mapping of container names to machine names + * @return a map of annotations + */ + public static Map createMachineNameAnnotations( + Map containerToMachineNames) { + + Map ret = new HashMap<>(); + + containerToMachineNames.forEach((c, m) -> putMachineName(ret, c, m)); + + return ret; + } + + /** + * Similar to {@link #createMachineNameAnnotations(Map)} but only creates annotations for a single + * mapping. + * + * @param containerName the name of the container + * @param machineName the name of the machine + * @return a map of annotations for the container-machine mapping + * @see #createMachineNameAnnotations(Map) + */ + public static Map createMachineNameAnnotations( + String containerName, String machineName) { + Map ret = Maps.newHashMapWithExpectedSize(2); + putMachineName(ret, containerName, machineName); + return ret; + } + + private static void putMachineName( + Map annotations, String containerName, String machineName) { + annotations.put(format(CONTAINER_DISPLAY_NAMES_FMT, containerName), machineName); } /** @@ -75,8 +120,16 @@ public static String machineName(ObjectMeta podMeta, Container container) { final Map annotations = podMeta.getAnnotations(); final String machineName; final String containerName = container.getName(); + + if (containerName != null && containerName.length() > MAX_CONTAINER_NAME_LENGTH) { + throw new IllegalArgumentException( + format( + "The container name exceeds the allowed limit of %s characters.", + MAX_CONTAINER_NAME_LENGTH)); + } + if (annotations != null - && (machineName = annotations.get(format(MACHINE_NAME_ANNOTATION_FMT, containerName))) + && (machineName = annotations.get(format(CONTAINER_DISPLAY_NAMES_FMT, containerName))) != null) { return machineName; } diff --git a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/devfile/DockerimageComponentToWorkspaceApplier.java b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/devfile/DockerimageComponentToWorkspaceApplier.java index a924e17f586..ff11e1c8a58 100644 --- a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/devfile/DockerimageComponentToWorkspaceApplier.java +++ b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/devfile/DockerimageComponentToWorkspaceApplier.java @@ -20,7 +20,6 @@ import static org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE; import static org.eclipse.che.api.workspace.server.devfile.Constants.PUBLIC_ENDPOINT_ATTRIBUTE; import static org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME; -import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.MACHINE_NAME_ANNOTATION_FMT; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableMap; @@ -51,6 +50,7 @@ import org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl; import org.eclipse.che.api.workspace.server.model.impl.VolumeImpl; import org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl; +import org.eclipse.che.workspace.infrastructure.kubernetes.Names; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment; import org.eclipse.che.workspace.infrastructure.kubernetes.util.Containers; @@ -202,7 +202,7 @@ private Deployment buildDeployment( .withNewMetadata() .withName(name) .addToLabels(CHE_COMPONENT_NAME_LABEL, name) - .addToAnnotations(format(MACHINE_NAME_ANNOTATION_FMT, name), name) + .addToAnnotations(Names.createMachineNameAnnotations(name, name)) .endMetadata() .withNewSpec() .withContainers(container) @@ -257,6 +257,14 @@ static String toMachineName(String imageName) throws DevfileException { return imageName; } + if (imageName.length() > Names.MAX_CONTAINER_NAME_LENGTH) { + throw new DevfileException( + format( + "The image name '%s' is longer than 63 characters and as such cannot be used as a container" + + " name. Please provide an alias for the component with that image.", + imageName)); + } + // the name needs to be both a valid k8s label and a valid machine name. String clean = imageName.replaceAll("[^-a-zA-Z0-9_]", "-"); diff --git a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/PodMerger.java b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/PodMerger.java index aa4bff3c296..5c75611da0b 100644 --- a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/PodMerger.java +++ b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/PodMerger.java @@ -12,7 +12,6 @@ package org.eclipse.che.workspace.infrastructure.kubernetes.environment; import static java.lang.String.format; -import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.MACHINE_NAME_ANNOTATION_FMT; import io.fabric8.kubernetes.api.model.Container; import io.fabric8.kubernetes.api.model.LocalObjectReference; @@ -85,11 +84,7 @@ public Deployment merge(List podsData) throws ValidationException { } // store original recipe machine name - basePodMeta - .getAnnotations() - .put( - format(MACHINE_NAME_ANNOTATION_FMT, containerName), - Names.machineName(podMeta, container)); + Names.putMachineName(basePodMeta, containerName, Names.machineName(podMeta, container)); baseSpec.getContainers().add(container); } diff --git a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/convert/DockerImageEnvironmentConverter.java b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/convert/DockerImageEnvironmentConverter.java index f1da0d34cfd..e4e4924e4bf 100644 --- a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/convert/DockerImageEnvironmentConverter.java +++ b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/convert/DockerImageEnvironmentConverter.java @@ -11,20 +11,17 @@ */ package org.eclipse.che.workspace.infrastructure.kubernetes.environment.convert; -import static java.lang.String.format; -import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.MACHINE_NAME_ANNOTATION_FMT; - import com.google.common.collect.ImmutableMap; import io.fabric8.kubernetes.api.model.ContainerBuilder; import io.fabric8.kubernetes.api.model.Pod; import io.fabric8.kubernetes.api.model.PodBuilder; -import java.util.HashMap; import java.util.Map; import javax.inject.Inject; import javax.inject.Singleton; import org.eclipse.che.api.workspace.server.spi.InfrastructureException; import org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig; import org.eclipse.che.workspace.infrastructure.docker.environment.dockerimage.DockerImageEnvironment; +import org.eclipse.che.workspace.infrastructure.kubernetes.Names; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.util.EntryPoint; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.util.EntryPointParser; @@ -67,14 +64,11 @@ public KubernetesEnvironment convert(DockerImageEnvironment environment) applyEntryPoint(machine, container); - final Map annotations = new HashMap<>(); - annotations.put(format(MACHINE_NAME_ANNOTATION_FMT, CONTAINER_NAME), machineName); - final Pod pod = new PodBuilder() .withNewMetadata() .withName(POD_NAME) - .withAnnotations(annotations) + .withAnnotations(Names.createMachineNameAnnotations(CONTAINER_NAME, machineName)) .endMetadata() .withNewSpec() .withContainers(container.build()) diff --git a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/server/secure/jwtproxy/JwtProxyProvisioner.java b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/server/secure/jwtproxy/JwtProxyProvisioner.java index 50fa0dfa192..d70cb4a1c07 100644 --- a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/server/secure/jwtproxy/JwtProxyProvisioner.java +++ b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/server/secure/jwtproxy/JwtProxyProvisioner.java @@ -55,6 +55,7 @@ import org.eclipse.che.commons.lang.Size; import org.eclipse.che.multiuser.machine.authentication.server.signature.SignatureKeyManager; import org.eclipse.che.multiuser.machine.authentication.server.signature.SignatureKeyManagerException; +import org.eclipse.che.workspace.infrastructure.kubernetes.Names; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment; import org.eclipse.che.workspace.infrastructure.kubernetes.server.ServerServiceBuilder; import org.eclipse.che.workspace.infrastructure.kubernetes.server.secure.jwtproxy.factory.JwtProxyConfigBuilderFactory; @@ -264,9 +265,7 @@ private Pod createJwtProxyPod() { return new PodBuilder() .withNewMetadata() .withName(JWT_PROXY_POD_NAME) - .withAnnotations( - ImmutableMap.of( - "org.eclipse.che.container.verifier.machine_name", JWT_PROXY_MACHINE_NAME)) + .withAnnotations(Names.createMachineNameAnnotations("verifier", JWT_PROXY_MACHINE_NAME)) .endMetadata() .withNewSpec() .withContainers( diff --git a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/K8sContainerResolver.java b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/K8sContainerResolver.java index 8a9c7f0415f..e9d422f8dff 100644 --- a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/K8sContainerResolver.java +++ b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/K8sContainerResolver.java @@ -27,6 +27,7 @@ import org.eclipse.che.api.workspace.server.wsplugins.model.ChePluginEndpoint; import org.eclipse.che.api.workspace.server.wsplugins.model.EnvVar; import org.eclipse.che.commons.lang.NameGenerator; +import org.eclipse.che.workspace.infrastructure.kubernetes.Names; import org.eclipse.che.workspace.infrastructure.kubernetes.util.Containers; import org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSize; @@ -38,8 +39,6 @@ */ public class K8sContainerResolver { - static final int MAX_CONTAINER_NAME_LENGTH = 63; // K8S container name limit - private final String imagePullPolicy; private final CheContainer cheContainer; private final List containerEndpoints; @@ -111,6 +110,6 @@ private List toK8sEnv(List env) private String buildContainerName(String cheContainerName) { String uniqueName = NameGenerator.generate(cheContainerName, 3).toLowerCase(); - return uniqueName.substring(0, min(uniqueName.length(), MAX_CONTAINER_NAME_LENGTH)); + return uniqueName.substring(0, min(uniqueName.length(), Names.MAX_CONTAINER_NAME_LENGTH)); } } diff --git a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/brokerphases/BrokerEnvironmentFactory.java b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/brokerphases/BrokerEnvironmentFactory.java index c64882ec131..d4c3a5fdc8c 100644 --- a/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/brokerphases/BrokerEnvironmentFactory.java +++ b/infrastructures/kubernetes/src/main/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/brokerphases/BrokerEnvironmentFactory.java @@ -233,7 +233,7 @@ private BrokerConfig createBrokerConfig( configMapVolume = brokerConfig.configMapVolume; } brokerConfig.container = newContainer(runtimeId, envVars, image, configMapVolume); - brokerConfig.machineName = Names.machineName(pod, brokerConfig.container); + brokerConfig.machineName = Names.machineName(pod.getMetadata(), brokerConfig.container); brokerConfig.machineConfig = new InternalMachineConfig(); brokerConfig .machineConfig diff --git a/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/KubernetesBrokerInitContainerApplierTest.java b/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/KubernetesBrokerInitContainerApplierTest.java index 79451082fa3..df7301b9727 100644 --- a/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/KubernetesBrokerInitContainerApplierTest.java +++ b/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/KubernetesBrokerInitContainerApplierTest.java @@ -11,7 +11,7 @@ */ package org.eclipse.che.workspace.infrastructure.kubernetes; -import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.MACHINE_NAME_ANNOTATION_FMT; +import static org.eclipse.che.workspace.infrastructure.kubernetes.Names.createMachineNameAnnotations; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.testng.Assert.assertEquals; @@ -59,17 +59,15 @@ public class KubernetesBrokerInitContainerApplierTest { private static final String WORKSPACE_MACHINE_NAME = "workspaceMachine"; private static final String WORKSPACE_CONTAINER_NAME = "workspaceContainer"; private static final Map workspacePodAnnotations = - ImmutableMap.of( - String.format(MACHINE_NAME_ANNOTATION_FMT, WORKSPACE_CONTAINER_NAME), - WORKSPACE_MACHINE_NAME); + createMachineNameAnnotations(WORKSPACE_CONTAINER_NAME, WORKSPACE_MACHINE_NAME); private static final String BROKER_POD_NAME = "brokerPod"; private static final String BROKER_MACHINE_NAME = "brokerMachine"; private static final String BROKER_CONTAINER_NAME = "brokerContainer"; private static final String BROKER_CONFIGMAP_NAME = "brokerConfigMap"; private static final Map brokerPodAnnotations = - ImmutableMap.of( - String.format(MACHINE_NAME_ANNOTATION_FMT, BROKER_CONTAINER_NAME), BROKER_MACHINE_NAME); + createMachineNameAnnotations(BROKER_CONTAINER_NAME, BROKER_MACHINE_NAME); + private static final Map brokerConfigMapData = ImmutableMap.of("brokerConfigKey", "brokerConfigValue"); diff --git a/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/devfile/DockerimageComponentToWorkspaceApplierTest.java b/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/devfile/DockerimageComponentToWorkspaceApplierTest.java index 2c2faa6acb5..da55da11c3c 100644 --- a/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/devfile/DockerimageComponentToWorkspaceApplierTest.java +++ b/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/devfile/DockerimageComponentToWorkspaceApplierTest.java @@ -17,7 +17,6 @@ import static org.eclipse.che.api.workspace.server.devfile.Constants.DOCKERIMAGE_COMPONENT_TYPE; import static org.eclipse.che.api.workspace.server.devfile.Constants.PUBLIC_ENDPOINT_ATTRIBUTE; import static org.eclipse.che.api.workspace.shared.Constants.PROJECTS_VOLUME_NAME; -import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.MACHINE_NAME_ANNOTATION_FMT; import static org.eclipse.che.workspace.infrastructure.kubernetes.devfile.DockerimageComponentToWorkspaceApplier.CHE_COMPONENT_NAME_LABEL; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -54,6 +53,7 @@ import org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl; import org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig; import org.eclipse.che.api.workspace.server.spi.environment.MachineConfigsValidator; +import org.eclipse.che.workspace.infrastructure.kubernetes.Names; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment; import org.mockito.ArgumentCaptor; import org.mockito.Captor; @@ -122,12 +122,11 @@ public void setUp() throws Exception { assertFalse(deploymentSelector.isEmpty()); assertTrue(podMeta.getLabels().entrySet().containsAll(deploymentSelector.entrySet())); - Map annotations = podMeta.getAnnotations(); - assertEquals(annotations.get(String.format(MACHINE_NAME_ANNOTATION_FMT, "jdk")), "jdk"); - Container container = podTemplate.getSpec().getContainers().get(0); assertEquals(container.getName(), "jdk"); assertEquals(container.getImage(), "eclipse/ubuntu_jdk8:latest"); + + assertEquals(Names.machineName(podMeta, container), "jdk"); } @Test @@ -163,14 +162,12 @@ public void shouldProvisionK8sEnvironmentWithMachineConfigFromSpecifiedDockerima assertFalse(deploymentSelector.isEmpty()); assertTrue(podMeta.getLabels().entrySet().containsAll(deploymentSelector.entrySet())); - Map annotations = podMeta.getAnnotations(); - assertEquals( - annotations.get(String.format(MACHINE_NAME_ANNOTATION_FMT, "eclipse-ubuntu_jdk8-latest")), - "eclipse-ubuntu_jdk8-latest"); - Container container = podTemplate.getSpec().getContainers().get(0); assertEquals(container.getName(), "eclipse-ubuntu_jdk8-latest"); assertEquals(container.getImage(), "eclipse/ubuntu_jdk8:latest"); + + assertEquals( + Names.machineName(podTemplate.getMetadata(), container), "eclipse-ubuntu_jdk8-latest"); } @Test diff --git a/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/KubernetesEnvironmentFactoryTest.java b/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/KubernetesEnvironmentFactoryTest.java index b3dcaf04c86..ec12b880680 100644 --- a/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/KubernetesEnvironmentFactoryTest.java +++ b/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/KubernetesEnvironmentFactoryTest.java @@ -11,12 +11,10 @@ */ package org.eclipse.che.workspace.infrastructure.kubernetes.environment; -import static java.lang.String.format; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; import static java.util.Collections.singletonList; -import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.MACHINE_NAME_ANNOTATION_FMT; import static org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.INGRESSES_IGNORED_WARNING_CODE; import static org.eclipse.che.workspace.infrastructure.kubernetes.Warnings.INGRESSES_IGNORED_WARNING_MESSAGE; import static org.eclipse.che.workspace.infrastructure.kubernetes.environment.PodMerger.DEPLOYMENT_NAME_LABEL; @@ -64,6 +62,7 @@ import org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig; import org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe; import org.eclipse.che.api.workspace.server.spi.environment.MemoryAttributeProvisioner; +import org.eclipse.che.workspace.infrastructure.kubernetes.Names; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData; import org.mockito.Mock; import org.mockito.testng.MockitoTestNGListener; @@ -476,8 +475,7 @@ private static PodData createPodData(String machineName, long ramLimit, long ram when(containerMock.getName()).thenReturn(containerName); when(containerMock.getResources()).thenReturn(resourcesMock); when(metadataMock.getAnnotations()) - .thenReturn( - ImmutableMap.of(format(MACHINE_NAME_ANNOTATION_FMT, containerName), machineName)); + .thenReturn(Names.createMachineNameAnnotations(containerName, machineName)); when(specMock.getContainers()).thenReturn(ImmutableList.of(containerMock)); return new PodData(specMock, metadataMock); } diff --git a/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/convert/DockerImageEnvironmentConverterTest.java b/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/convert/DockerImageEnvironmentConverterTest.java index 170737d0450..9cfc93c09f7 100644 --- a/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/convert/DockerImageEnvironmentConverterTest.java +++ b/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/environment/convert/DockerImageEnvironmentConverterTest.java @@ -11,11 +11,9 @@ */ package org.eclipse.che.workspace.infrastructure.kubernetes.environment.convert; -import static java.lang.String.format; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; -import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.MACHINE_NAME_ANNOTATION_FMT; import static org.eclipse.che.workspace.infrastructure.kubernetes.environment.convert.DockerImageEnvironmentConverter.CONTAINER_NAME; import static org.eclipse.che.workspace.infrastructure.kubernetes.environment.convert.DockerImageEnvironmentConverter.POD_NAME; import static org.mockito.ArgumentMatchers.any; @@ -35,6 +33,7 @@ import org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig; import org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe; import org.eclipse.che.workspace.infrastructure.docker.environment.dockerimage.DockerImageEnvironment; +import org.eclipse.che.workspace.infrastructure.kubernetes.Names; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.util.EntryPoint; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.util.EntryPointParser; @@ -71,8 +70,9 @@ public void setup() throws Exception { lenient().when(recipe.getContent()).thenReturn(RECIPE_CONTENT); lenient().when(recipe.getType()).thenReturn(RECIPE_TYPE); machines = ImmutableMap.of(MACHINE_NAME, mock(InternalMachineConfig.class)); - final Map annotations = new HashMap<>(); - annotations.put(format(MACHINE_NAME_ANNOTATION_FMT, CONTAINER_NAME), MACHINE_NAME); + final Map annotations = + Names.createMachineNameAnnotations(CONTAINER_NAME, MACHINE_NAME); + pod = new PodBuilder() .withNewMetadata() diff --git a/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/K8sContainerResolverTest.java b/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/K8sContainerResolverTest.java index 091a0a50b68..78227d04b32 100644 --- a/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/K8sContainerResolverTest.java +++ b/infrastructures/kubernetes/src/test/java/org/eclipse/che/workspace/infrastructure/kubernetes/wsplugins/K8sContainerResolverTest.java @@ -11,7 +11,7 @@ */ package org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins; -import static org.eclipse.che.workspace.infrastructure.kubernetes.wsplugins.K8sContainerResolver.MAX_CONTAINER_NAME_LENGTH; +import static org.eclipse.che.workspace.infrastructure.kubernetes.Names.MAX_CONTAINER_NAME_LENGTH; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEqualsNoOrder; import static org.testng.Assert.assertTrue; diff --git a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentFactoryTest.java b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentFactoryTest.java index eee95c431c7..552f5f502b3 100644 --- a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentFactoryTest.java +++ b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/environment/OpenShiftEnvironmentFactoryTest.java @@ -11,12 +11,10 @@ */ package org.eclipse.che.workspace.infrastructure.openshift.environment; -import static java.lang.String.format; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.emptyMap; import static java.util.Collections.singletonList; -import static org.eclipse.che.workspace.infrastructure.kubernetes.Constants.MACHINE_NAME_ANNOTATION_FMT; import static org.eclipse.che.workspace.infrastructure.kubernetes.environment.PodMerger.DEPLOYMENT_NAME_LABEL; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -60,6 +58,7 @@ import org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig; import org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe; import org.eclipse.che.api.workspace.server.spi.environment.MemoryAttributeProvisioner; +import org.eclipse.che.workspace.infrastructure.kubernetes.Names; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData; import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesRecipeParser; @@ -480,8 +479,7 @@ private static PodData createPodData(String machineName, Long ramLimit, Long ram } when(containerMock.getName()).thenReturn(containerName); when(metadataMock.getAnnotations()) - .thenReturn( - ImmutableMap.of(format(MACHINE_NAME_ANNOTATION_FMT, containerName), machineName)); + .thenReturn(Names.createMachineNameAnnotations(containerName, machineName)); when(specMock.getContainers()).thenReturn(ImmutableList.of(containerMock)); return new PodData(specMock, metadataMock); }