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 987b88b commit d3cfa39
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 44 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 @@ -305,9 +306,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 @@ -352,7 +350,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 @@ -370,18 +371,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 @@ -392,7 +408,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;
openShiftDeploymentCleaner.cleanDeploymentResources(deploymentName, openShiftCheProjectName);
openShiftClient.resource(imageStreamTag).delete();
throw e;
Expand Down Expand Up @@ -1052,13 +1067,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 @@ -1079,19 +1093,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 @@ -1100,7 +1120,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 @@ -1122,7 +1141,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 @@ -1131,7 +1150,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 @@ -1162,7 +1181,6 @@ private String createOpenShiftDeployment(String workspaceID,
}

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

private EnvVar[] getAdditionalEnvVars() {
Expand Down Expand Up @@ -1352,7 +1370,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 @@ -1369,7 +1387,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 @@ -28,26 +28,27 @@ public class OpenShiftRouteCreator {
private static final String REDIRECT_INSECURE_EDGE_TERMINATION_POLICY = "Redirect";

public void createRoute (final String namespace,
final String workspaceName,
final String openShiftNamespaceExternalAddress,
final String serverRef,
final String serviceName,
final boolean enableTls) {
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
.routes()
.inNamespace(namespace)
.createNew()
.withNewMetadata()
.withName(routeName)
.addToLabels(OpenShiftConnector.OPENSHIFT_DEPLOYMENT_LABEL, serviceName)
.addToLabels(OpenShiftConnector.OPENSHIFT_DEPLOYMENT_LABEL, deploymentName)
.endMetadata()
.withNewSpec()
.withHost(serviceHost)
Expand All @@ -74,8 +75,8 @@ public void createRoute (final String namespace,
}
}

private String generateRouteName(final String workspaceName, final String serverRef) {
return serverRef + "-" + workspaceName;
private String generateRouteName(final String serviceName, final String serverRef) {
return serverRef + "-" + serviceName;
}

private String generateRouteHost(final String routeName, final String openShiftNamespaceExternalAddress) {
Expand Down

0 comments on commit d3cfa39

Please sign in to comment.