Skip to content

Commit

Permalink
Name openshift resources based on the machine name for non-dev machines
Browse files Browse the repository at this point in the history
This fixes https://issues.jboss.org/browse/CHE-259
and https://issues.jboss.org/browse/CHE-258

Signed-off-by: David Festal <dfestal@redhat.com>
  • Loading branch information
davidfestal committed May 29, 2017
1 parent 6401f22 commit 6ccb963
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,20 @@ protected boolean useHttpsForExternalUrls() {
protected Map<String, String> 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<String, String> labels= containerInfo.getConfig().getLabels();

Map<String, List<PortBinding>> portBindings = containerInfo.getNetworkSettings().getPorts();
Map<String, String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -301,9 +302,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();
Expand Down Expand Up @@ -348,7 +346,10 @@ public ContainerCreated createContainer(CreateContainerParams createContainerPar
String[] volumes = createContainerParams.getContainerConfig().getHostConfig().getBinds();

Map<String, String> 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<String, Quantity> resourceLimits = new HashMap<>();
if (!isNullOrEmpty(cheWorkspaceMemoryLimit)) {
LOG.info("Che property 'che.openshift.workspace.memory.override' "
Expand All @@ -366,18 +367,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) {
Expand All @@ -388,7 +404,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;
Expand Down Expand Up @@ -1094,13 +1109,12 @@ protected String getCheWorkspaceId(CreateContainerParams createContainerParams)
return workspaceID.replaceFirst("workspace","");
}

private void createOpenShiftService(String workspaceID,
private void createOpenShiftService(String deploymentName,
String serviceName,
Set<String> exposedPorts,
Map<String, String> additionalLabels) {

String serviceName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID;

Map<String, String> selector = Collections.singletonMap(OPENSHIFT_DEPLOYMENT_LABEL, serviceName);
Map<String, String> additionalLabels,
String[] endpointAliases) {
Map<String, String> selector = Collections.singletonMap(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName);
List<ServicePort> ports = KubernetesService.getServicePortsFrom(exposedPorts);

try (OpenShiftClient openShiftClient = new DefaultOpenShiftClient()) {
Expand All @@ -1121,19 +1135,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<String> exposedPorts,
Expand All @@ -1142,7 +1162,6 @@ private String createOpenShiftDeployment(String workspaceID,
Map<String, Quantity> resourceLimits,
Map<String, Quantity> resourceRequests) throws OpenShiftException {

String deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID;
LOG.info("Creating OpenShift deployment {}", deploymentName);

Map<String, String> selector = Collections.singletonMap(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName);
Expand All @@ -1164,7 +1183,7 @@ private String createOpenShiftDeployment(String workspaceID,
.withPrivileged(false)
.endSecurityContext()
.withLivenessProbe(getLivenessProbeFrom(exposedPorts))
.withVolumeMounts(getVolumeMountsFrom(volumes, workspaceID))
.withVolumeMounts(getVolumeMountsFrom(volumes))
.withNewResources()
.withLimits(resourceLimits)
.withRequests(resourceRequests)
Expand All @@ -1173,7 +1192,7 @@ private String createOpenShiftDeployment(String workspaceID,

PodSpec podSpec = new PodSpecBuilder()
.withContainers(container)
.withVolumes(getVolumesFrom(volumes, workspaceID))
.withVolumes(getVolumesFrom(volumes))
.withTerminationGracePeriodSeconds(OPENSHIFT_POD_TERMINATION_GRACE_PERIOD)
.build();

Expand Down Expand Up @@ -1204,7 +1223,6 @@ private String createOpenShiftDeployment(String workspaceID,
}

LOG.info("OpenShift deployment {} created", deploymentName);
return deployment.getMetadata().getName();
}

private EnvVar[] getAdditionalEnvVars() {
Expand Down Expand Up @@ -1445,7 +1463,7 @@ private String getWorkspaceSubpath(String[] volumes) {
return workspaceSubpath;
}

private List<VolumeMount> getVolumeMountsFrom(String[] volumes, String workspaceID) {
private List<VolumeMount> getVolumeMountsFrom(String[] volumes) {
List<VolumeMount> vms = new ArrayList<>();
PersistentVolumeClaim pvc = getClaimCheWorkspace();
if (pvc != null) {
Expand All @@ -1462,7 +1480,7 @@ private List<VolumeMount> getVolumeMountsFrom(String[] volumes, String workspace
return vms;
}

private List<Volume> getVolumesFrom(String[] volumes, String workspaceID) {
private List<Volume> getVolumesFrom(String[] volumes) {
List<Volume> vs = new ArrayList<>();
PersistentVolumeClaim pvc = getClaimCheWorkspace();
if (pvc != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ 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) {
throw new IllegalArgumentException("Property che.docker.ip.external must be set when using openshift.");
}

try (OpenShiftClient openShiftClient = new DefaultOpenShiftClient()) {
String routeName = generateRouteName(workspaceName, serverRef);
String routeName = generateRouteName(routeId, serverRef);
String serviceHost = generateRouteHost(routeName, openShiftNamespaceExternalAddress);

SpecNested<DoneableRoute> routeSpec = openShiftClient
Expand All @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit 6ccb963

Please sign in to comment.