Skip to content

Commit

Permalink
Cache docker containers and images across job runs
Browse files Browse the repository at this point in the history
Every time a job runs, it runs completely independently of other runs.
While this can be good, it also has significant impacts. The duration of
the run and the disk space taken up will be much longer because there
will be no docker cache.

This change mounts a volume on /var/lib/docker on the job container so
that there will be a cache between runs. For isolation purposes, each
job will have it's own cache volume. This is to prevent possible
unintended behavior and also for security purposes so different jobs
don't have access to each others cache which could have sensitive
information on them such as secrets.

The result of this change should be jobs running quicker and taking up
less space.
  • Loading branch information
ScottG489 committed Jan 2, 2022
1 parent 8678327 commit 8323344
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/main/java/conjob/core/job/DockerAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public List<String> listAllVolumeNames() throws DockerException, InterruptedExce
}

public String createJobRun(JobRunConfig jobRunConfig) throws CreateJobRunException {
HostConfig hostConfig = getHostConfig(jobRunConfig.getSecretsVolumeName());
HostConfig hostConfig = getHostConfig(jobRunConfig.getDockerCacheVolumeName(), jobRunConfig.getSecretsVolumeName());

ContainerConfig containerConfig = getContainerConfig(
jobRunConfig.getJobName(),
Expand Down Expand Up @@ -106,8 +106,11 @@ private ContainerConfig getContainerConfig(String jobName, String input, HostCon
return containerConfigBuilder.build();
}

private HostConfig getHostConfig(String secretsVolumeName) {
private HostConfig getHostConfig(String dockerCacheVolumeName, String secretsVolumeName) {
HostConfig.Builder hostConfigBuilder = getHostConfigBuilderFor(containerRuntime);

hostConfigBuilder.appendBinds(dockerCacheVolumeName + ":" + "/var/lib/docker");

if (secretsVolumeName != null) {
hostConfigBuilder.appendBinds(
secretsVolumeName
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/conjob/core/job/JobRunConfigCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import conjob.core.job.model.JobRunConfig;

public class JobRunConfigCreator {
public JobRunConfig getContainerConfig(String imageName, String input, String secretsVolumeName) {
public JobRunConfig getContainerConfig(String imageName, String input, String dockerCacheVolumeName, String secretsVolumeName) {
JobRunConfig jobRunConfig;
if (input != null && !input.isEmpty()) {
jobRunConfig = new JobRunConfig(imageName, input, secretsVolumeName);
jobRunConfig = new JobRunConfig(imageName, input, dockerCacheVolumeName, secretsVolumeName);
} else {
jobRunConfig = new JobRunConfig(imageName, null, secretsVolumeName);
jobRunConfig = new JobRunConfig(imageName, null, dockerCacheVolumeName, secretsVolumeName);
}

return jobRunConfig;
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/conjob/core/job/config/ConfigUtil.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package conjob.core.job.config;

public class ConfigUtil {
public String translateToVolumeName(String imageName) {
public String translateToSecretsVolumeName(String imageName) {
int usernameSeparatorIndex = imageName.indexOf('/');
int tagSeparatorIndex = imageName.lastIndexOf(':');
StringBuilder sb = new StringBuilder(imageName);
Expand All @@ -12,4 +12,16 @@ public String translateToVolumeName(String imageName) {

return sb.toString();
}

public String translateToDockerCacheVolumeName(String imageName) {
int usernameSeparatorIndex = imageName.indexOf('/');
int tagSeparatorIndex = imageName.lastIndexOf(':');
StringBuilder sb = new StringBuilder(imageName);
sb.setCharAt(usernameSeparatorIndex, '-');
if (tagSeparatorIndex != -1) {
sb.setCharAt(tagSeparatorIndex, '-');
}

return "conjob-docker-cache-" + sb;
}
}
1 change: 1 addition & 0 deletions src/main/java/conjob/core/job/model/JobRunConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
public class JobRunConfig {
String jobName;
String input;
String dockerCacheVolumeName;
String secretsVolumeName;
}
5 changes: 3 additions & 2 deletions src/main/java/conjob/service/job/JobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ private JobRun runJob(String imageName, String input, PullStrategy pullStrategy)
}

private JobRunConfig getJobRunConfig(String imageName, String input) throws SecretsStoreException {
String correspondingSecretsVolumeName = configUtil.translateToVolumeName(imageName);
String correspondingSecretsVolumeName = configUtil.translateToSecretsVolumeName(imageName);
String dockerCacheVolumeName = configUtil.translateToDockerCacheVolumeName(imageName);
String secretsVolumeName = secretsStore
.findSecrets(correspondingSecretsVolumeName)
.orElse(null);

return jobRunConfigCreator.getContainerConfig(imageName, input, secretsVolumeName);
return jobRunConfigCreator.getContainerConfig(imageName, input, dockerCacheVolumeName, secretsVolumeName);
}
}
2 changes: 1 addition & 1 deletion src/main/java/conjob/service/secrets/SecretsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public SecretsService(

public void createsSecret(String imageName, String secrets)
throws IOException {
String secretsVolumeName = configUtil.translateToVolumeName(imageName);
String secretsVolumeName = configUtil.translateToSecretsVolumeName(imageName);
// TODO: Could there be a race condition if two of these containers are running at the same time?
String intermediaryContainerName =
uniqueContainerNameGenerator.generate(CONTAINER_NAME_PREFIX);
Expand Down

0 comments on commit 8323344

Please sign in to comment.