diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategy.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategy.java index b32b7144d37a..0ae5a0863e15 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategy.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/LocalDockerSinglePortServerEvaluationStrategy.java @@ -64,14 +64,20 @@ protected boolean useHttpsForExternalUrls() { protected Map getExternalAddressesAndPorts(ContainerInfo containerInfo, String internalHost) { String cheExternalAddress = getCheExternalAddress(containerInfo, internalHost); - String workspaceID = getWorkspaceID(containerInfo.getConfig().getEnv()); + String serviceName = containerInfo.getConfig().getHostname(); + String workspaceId = getWorkspaceID(containerInfo.getConfig().getEnv()); + if (! "unknown-ws".equals(workspaceId)) { + // It is the Dev machine + serviceName = workspaceId; + } + Map labels= containerInfo.getConfig().getLabels(); Map> portBindings = containerInfo.getNetworkSettings().getPorts(); Map addressesAndPorts = new HashMap<>(); for (String serverKey : portBindings.keySet()) { String serverName = getWorkspaceServerName(labels, serverKey); - String serverURL = serverName + "-" + workspaceID + "-" + cheExternalAddress; + String serverURL = serverName + "-" + serviceName + "-" + cheExternalAddress; addressesAndPorts.put(serverKey, serverURL); } diff --git a/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/OpenShiftConnector.java b/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/OpenShiftConnector.java index 05ecf8b60489..c2d49b5f3b8c 100644 --- a/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/OpenShiftConnector.java +++ b/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/OpenShiftConnector.java @@ -67,6 +67,7 @@ import org.eclipse.che.plugin.docker.client.json.NetworkSettings; import org.eclipse.che.plugin.docker.client.json.PortBinding; import org.eclipse.che.plugin.docker.client.json.network.ContainerInNetwork; +import org.eclipse.che.plugin.docker.client.json.network.EndpointConfig; import org.eclipse.che.plugin.docker.client.json.network.Ipam; import org.eclipse.che.plugin.docker.client.json.network.IpamConfig; import org.eclipse.che.plugin.docker.client.json.network.Network; @@ -297,9 +298,6 @@ public ContainerCreated createContainer(CreateContainerParams createContainerPar String containerName = KubernetesStringUtils.convertToContainerName(createContainerParams.getContainerName()); String workspaceID = getCheWorkspaceId(createContainerParams); - // Generate workspaceID if CHE_WORKSPACE_ID env var does not exist - workspaceID = workspaceID.isEmpty() ? KubernetesStringUtils.generateWorkspaceID() : workspaceID; - // imageForDocker is the docker version of the image repository. It's needed for other // OpenShiftConnector API methods, but is not acceptable as an OpenShift name String imageForDocker = createContainerParams.getContainerConfig().getImage(); @@ -344,7 +342,10 @@ public ContainerCreated createContainer(CreateContainerParams createContainerPar String[] volumes = createContainerParams.getContainerConfig().getHostConfig().getBinds(); Map additionalLabels = createContainerParams.getContainerConfig().getLabels(); - + String networkName = createContainerParams.getContainerConfig().getHostConfig().getNetworkMode(); + EndpointConfig endpointConfig = createContainerParams.getContainerConfig().getNetworkingConfig().getEndpointsConfig().get(networkName); + String[] endpointAliases = endpointConfig != null ? endpointConfig.getAliases() : new String[0]; + Map resourceLimits = new HashMap<>(); if (!isNullOrEmpty(cheWorkspaceMemoryLimit)) { LOG.info("Che property 'che.openshift.workspace.memory.override' " @@ -362,18 +363,33 @@ public ContainerCreated createContainer(CreateContainerParams createContainerPar resourceRequests.put("memory", new Quantity(cheWorkspaceMemoryRequest)); } + String deploymentName; + String serviceName; + if (workspaceID.isEmpty()) { + // It's a not the Dev machine + if (endpointAliases.length > 0) { + serviceName = endpointAliases[0]; + deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + serviceName; + } else { + // Should never happen + serviceName = deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + KubernetesStringUtils.generateWorkspaceID(); + } + } else { + serviceName = deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID; + } + String containerID; OpenShiftClient openShiftClient = new DefaultOpenShiftClient(); - try { - createOpenShiftService(workspaceID, exposedPorts, additionalLabels); - String deploymentName = createOpenShiftDeployment(workspaceID, - dockerPullSpec, - containerName, - exposedPorts, - envVariables, - volumes, - resourceLimits, - resourceRequests); + try { + createOpenShiftService(deploymentName, serviceName, exposedPorts, additionalLabels, endpointAliases); + createOpenShiftDeployment(deploymentName, + dockerPullSpec, + containerName, + exposedPorts, + envVariables, + volumes, + resourceLimits, + resourceRequests); containerID = waitAndRetrieveContainerID(deploymentName); if (containerID == null) { @@ -384,7 +400,6 @@ public ContainerCreated createContainer(CreateContainerParams createContainerPar // in an inconsistent state. LOG.info("Error while creating Pod, removing deployment"); LOG.info(e.getMessage()); - String deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID; cleanUpWorkspaceResources(deploymentName); openShiftClient.resource(imageStreamTag).delete(); throw e; @@ -1090,13 +1105,12 @@ protected String getCheWorkspaceId(CreateContainerParams createContainerParams) return workspaceID.replaceFirst("workspace",""); } - private void createOpenShiftService(String workspaceID, + private void createOpenShiftService(String deploymentName, + String serviceName, Set exposedPorts, - Map additionalLabels) { - - String serviceName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID; - - Map selector = Collections.singletonMap(OPENSHIFT_DEPLOYMENT_LABEL, serviceName); + Map additionalLabels, + String[] endpointAliases) { + Map selector = Collections.singletonMap(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName); List ports = KubernetesService.getServicePortsFrom(exposedPorts); try (OpenShiftClient openShiftClient = new DefaultOpenShiftClient()) { @@ -1117,19 +1131,25 @@ private void createOpenShiftService(String workspaceID, LOG.info("OpenShift service {} created", service.getMetadata().getName()); for (ServicePort port : ports) { - createOpenShiftRoute(serviceName, port.getName(), workspaceID); + createOpenShiftRoute(serviceName, deploymentName, port.getName()); } } } private void createOpenShiftRoute(String serviceName, - String serverRef, - String workspaceName) { - - OpenShiftRouteCreator.createRoute(openShiftCheProjectName, workspaceName, cheServerExternalAddress, serverRef, serviceName, secureRoutes); + String deploymentName, + String serverRef) { + String routeId = serviceName.replaceFirst(CHE_OPENSHIFT_RESOURCES_PREFIX, ""); + OpenShiftRouteCreator.createRoute(openShiftCheProjectName, + cheServerExternalAddress, + serverRef, + serviceName, + deploymentName, + routeId, + secureRoutes); } - private String createOpenShiftDeployment(String workspaceID, + private void createOpenShiftDeployment(String deploymentName, String imageName, String sanitizedContainerName, Set exposedPorts, @@ -1138,7 +1158,6 @@ private String createOpenShiftDeployment(String workspaceID, Map resourceLimits, Map resourceRequests) throws OpenShiftException { - String deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID; LOG.info("Creating OpenShift deployment {}", deploymentName); Map selector = Collections.singletonMap(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName); @@ -1159,7 +1178,7 @@ private String createOpenShiftDeployment(String workspaceID, .withPrivileged(false) .endSecurityContext() .withLivenessProbe(getLivenessProbeFrom(exposedPorts)) - .withVolumeMounts(getVolumeMountsFrom(volumes, workspaceID)) + .withVolumeMounts(getVolumeMountsFrom(volumes)) .withNewResources() .withLimits(resourceLimits) .withRequests(resourceRequests) @@ -1168,7 +1187,7 @@ private String createOpenShiftDeployment(String workspaceID, PodSpec podSpec = new PodSpecBuilder() .withContainers(container) - .withVolumes(getVolumesFrom(volumes, workspaceID)) + .withVolumes(getVolumesFrom(volumes)) .build(); Deployment deployment = new DeploymentBuilder() @@ -1198,7 +1217,6 @@ private String createOpenShiftDeployment(String workspaceID, } LOG.info("OpenShift deployment {} created", deploymentName); - return deployment.getMetadata().getName(); } /** @@ -1418,7 +1436,7 @@ private String getWorkspaceSubpath(String[] volumes) { return workspaceSubpath; } - private List getVolumeMountsFrom(String[] volumes, String workspaceID) { + private List getVolumeMountsFrom(String[] volumes) { List vms = new ArrayList<>(); PersistentVolumeClaim pvc = getClaimCheWorkspace(); if (pvc != null) { @@ -1435,7 +1453,7 @@ private List getVolumeMountsFrom(String[] volumes, String workspace return vms; } - private List getVolumesFrom(String[] volumes, String workspaceID) { + private List getVolumesFrom(String[] volumes) { List vs = new ArrayList<>(); PersistentVolumeClaim pvc = getClaimCheWorkspace(); if (pvc != null) { diff --git a/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/OpenShiftRouteCreator.java b/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/OpenShiftRouteCreator.java index 2cdc8a14ba76..05a7034ef257 100644 --- a/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/OpenShiftRouteCreator.java +++ b/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/OpenShiftRouteCreator.java @@ -25,10 +25,11 @@ public class OpenShiftRouteCreator { private static final String REDIRECT_INSECURE_EDGE_TERMINATION_POLICY = "Redirect"; public static void createRoute (final String namespace, - final String workspaceName, final String openShiftNamespaceExternalAddress, final String serverRef, final String serviceName, + final String deploymentName, + final String routeId, final boolean enableTls) { if (openShiftNamespaceExternalAddress == null) { @@ -36,7 +37,7 @@ public static void createRoute (final String namespace, } try (OpenShiftClient openShiftClient = new DefaultOpenShiftClient()) { - String routeName = generateRouteName(workspaceName, serverRef); + String routeName = generateRouteName(routeId, serverRef); String serviceHost = generateRouteHost(routeName, openShiftNamespaceExternalAddress); SpecNested routeSpec = openShiftClient @@ -45,7 +46,7 @@ public static void createRoute (final String namespace, .createNew() .withNewMetadata() .withName(routeName) - .addToLabels(OpenShiftConnector.OPENSHIFT_DEPLOYMENT_LABEL, serviceName) + .addToLabels(OpenShiftConnector.OPENSHIFT_DEPLOYMENT_LABEL, deploymentName) .endMetadata() .withNewSpec() .withHost(serviceHost) @@ -72,8 +73,8 @@ public static void createRoute (final String namespace, } } - private static String generateRouteName(final String workspaceName, final String serverRef) { - return serverRef + "-" + workspaceName; + private static String generateRouteName(final String serviceName, final String serverRef) { + return serverRef + "-" + serviceName; } private static String generateRouteHost(final String routeName, final String openShiftNamespaceExternalAddress) {