Skip to content

Commit

Permalink
Add support for resource limits when running on Openshift
Browse files Browse the repository at this point in the history
Add resource limits to workspace Pods when running on OpenShift.
Workspace memory limit is obtained from the che REST API, while
cpu limit, is determined by the new che properties

- "che.openshift.workspace.cpu.limit"

Signed-off-by: Angel Misevski <amisevsk@redhat.com>
  • Loading branch information
amisevsk committed Mar 28, 2017
1 parent f538239 commit 881d242
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ che.openshift.liveness.probe.delay=300
che.openshift.liveness.probe.timeout=1
che.openshift.workspaces.pvc.name=claim-che-workspace
che.openshift.workspaces.pvc.quantity=10Gi
che.openshift.workspace.cpu.limit=1
# Override memory limit used for openshift workspaces. String, e.g. 1300Mi
che.openshift.workspace.memory.limit=

# Which implementation of DockerConnector to use in managing containers. In general,
# the base implementation of DockerConnector is appropriate, but OpenShiftConnector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public class OpenShiftConnector extends DockerConnector {
private final String cheWorkspaceStorage;
private final String cheWorkspaceProjectsStorage;
private final String cheServerExternalAddress;
private final String cheWorkspaceCpuLimit;

@Inject
public OpenShiftConnector(DockerConnectorConfiguration connectorConfiguration,
Expand All @@ -183,7 +184,9 @@ public OpenShiftConnector(DockerConnectorConfiguration connectorConfiguration,
@Named("che.openshift.workspaces.pvc.name") String workspacesPersistentVolumeClaim,
@Named("che.openshift.workspaces.pvc.quantity") String workspacesPvcQuantity,
@Named("che.workspace.storage") String cheWorkspaceStorage,
@Named("che.workspace.projects.storage") String cheWorkspaceProjectsStorage) {
@Named("che.workspace.projects.storage") String cheWorkspaceProjectsStorage,
@Named("che.openshift.workspace.cpu.limit") String cheWorkspaceCpuLimit) {


super(connectorConfiguration, connectionFactory, authResolver, dockerApiVersionPathPrefixProvider);
this.cheServerExternalAddress = cheServerExternalAddress;
Expand All @@ -194,6 +197,7 @@ public OpenShiftConnector(DockerConnectorConfiguration connectorConfiguration,
this.workspacesPvcQuantity = workspacesPvcQuantity;
this.cheWorkspaceStorage = cheWorkspaceStorage;
this.cheWorkspaceProjectsStorage = cheWorkspaceProjectsStorage;
this.cheWorkspaceCpuLimit = cheWorkspaceCpuLimit;

this.openShiftClient = new DefaultOpenShiftClient();
}
Expand Down Expand Up @@ -252,6 +256,13 @@ public ContainerCreated createContainer(CreateContainerParams createContainerPar
String[] volumes = createContainerParams.getContainerConfig().getHostConfig().getBinds();

Map<String, String> additionalLabels = createContainerParams.getContainerConfig().getLabels();

long memoryLimitBytes = createContainerParams.getContainerConfig().getHostConfig().getMemory();
String memoryLimit = Long.toString(memoryLimitBytes / 1048576) + "Mi";
Map<String, Quantity> resourceLimits = new HashMap<>();
resourceLimits.put("memory", new Quantity(memoryLimit));
resourceLimits.put("cpu", new Quantity(cheWorkspaceCpuLimit));

String containerID;
try {
createOpenShiftService(workspaceID, exposedPorts, additionalLabels);
Expand All @@ -260,7 +271,8 @@ public ContainerCreated createContainer(CreateContainerParams createContainerPar
containerName,
exposedPorts,
envVariables,
volumes);
volumes,
resourceLimits);

containerID = waitAndRetrieveContainerID(deploymentName);
if (containerID == null) {
Expand Down Expand Up @@ -990,7 +1002,8 @@ private String createOpenShiftDeployment(String workspaceID,
String sanitizedContainerName,
Set<String> exposedPorts,
String[] envVariables,
String[] volumes) {
String[] volumes,
Map<String, Quantity> resourceLimits) {

String deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID;
LOG.info("Creating OpenShift deployment {}", deploymentName);
Expand Down Expand Up @@ -1018,7 +1031,8 @@ private String createOpenShiftDeployment(String workspaceID,
deploymentName,
selector,
command,
false);
false,
resourceLimits);

try {
waitAndRetrieveContainerID(deploymentName);
Expand All @@ -1043,7 +1057,8 @@ private String createOpenShiftDeployment(String workspaceID,
deploymentName,
selector,
null,
true);
true,
resourceLimits);
LOG.info("OpenShift deployment {} created", deploymentName);
return deployment.getMetadata().getName();
}
Expand Down Expand Up @@ -1079,7 +1094,8 @@ private Deployment createOpenShiftDeploymentInternal(String workspaceID,
Map<String,
String> selector,
String[] command,
boolean withSubpath) {
boolean withSubpath,
Map<String, Quantity> resourceLimits) {

Container container = new ContainerBuilder()
.withName(sanitizedContainerName)
Expand All @@ -1093,6 +1109,9 @@ private Deployment createOpenShiftDeploymentInternal(String workspaceID,
.withLivenessProbe(getLivenessProbeFrom(exposedPorts))
.withCommand(command)
.withVolumeMounts(getVolumeMountsFrom(volumes, workspaceID, withSubpath))
.withNewResources()
.withLimits(resourceLimits)
.endResources()
.build();

PodSpec podSpec = new PodSpecBuilder()
Expand Down Expand Up @@ -1196,7 +1215,6 @@ private ContainerInfo createContainerInfo(Service svc,
// HostConfig
HostConfig hostConfig = new HostConfig();
hostConfig.setBinds(new String[0]);
hostConfig.setMemory(imageInfo.getConfig().getMemory());

// Env vars
List<String> imageEnv = Arrays.asList(imageContainerConfig.getEnv());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class OpenShiftConnectorTest {
private static final String OPENSHIFT_DEFAULT_WORKSPACE_STORAGE = "/data/workspaces";
private static final String OPENSHIFT_DEFAULT_WORKSPACE_PROJECTS_STORAGE = "/projects";
private static final String CHE_DEFAULT_SERVER_EXTERNAL_ADDRESS = "che.openshift.mini";

private static final String CHE_WORKSPACE_CPU_LIMIT = "1";

@Mock
private DockerConnectorConfiguration dockerConnectorConfiguration;
@Mock
Expand Down Expand Up @@ -73,7 +74,8 @@ public void shouldGetWorkspaceIDWhenAValidOneIsProvidedInCreateContainerParams()
OPENSHIFT_DEFAULT_WORKSPACE_PERSISTENT_VOLUME_CLAIM,
OPENSHIFT_DEFAULT_WORKSPACE_QUANTITY,
OPENSHIFT_DEFAULT_WORKSPACE_STORAGE,
OPENSHIFT_DEFAULT_WORKSPACE_PROJECTS_STORAGE);
OPENSHIFT_DEFAULT_WORKSPACE_PROJECTS_STORAGE,
CHE_WORKSPACE_CPU_LIMIT);
String workspaceID = openShiftConnector.getCheWorkspaceId(createContainerParams);

//Then
Expand Down

0 comments on commit 881d242

Please sign in to comment.