|
23 | 23 | import java.util.List;
|
24 | 24 | import java.util.Map;
|
25 | 25 | import java.util.concurrent.TimeUnit;
|
| 26 | +import org.apache.commons.lang3.RandomStringUtils; |
26 | 27 | import org.slf4j.Logger;
|
27 | 28 | import org.slf4j.LoggerFactory;
|
28 | 29 |
|
29 | 30 | public class DockerProcessFactory implements ProcessFactory {
|
30 | 31 |
|
31 | 32 | private static final Logger LOGGER = LoggerFactory.getLogger(DockerProcessFactory.class);
|
| 33 | + private static final String VERSION_DELIMITER = ":"; |
| 34 | + private static final String DOCKER_DELIMITER = "/"; |
32 | 35 |
|
33 | 36 | private static final Path DATA_MOUNT_DESTINATION = Path.of("/data");
|
34 | 37 | private static final Path LOCAL_MOUNT_DESTINATION = Path.of("/local");
|
@@ -114,6 +117,9 @@ public Process create(final String jobId,
|
114 | 117 | rebasePath(jobRoot).toString(), // rebases the job root on the job data mount
|
115 | 118 | "--log-driver",
|
116 | 119 | "none");
|
| 120 | + final String containerName = createContainerName(imageName, jobId, attempt); |
| 121 | + cmd.add("--name"); |
| 122 | + cmd.add(containerName); |
117 | 123 |
|
118 | 124 | if (networkName != null) {
|
119 | 125 | cmd.add("--network");
|
@@ -163,6 +169,26 @@ public Process create(final String jobId,
|
163 | 169 | }
|
164 | 170 | }
|
165 | 171 |
|
| 172 | + private static String createContainerName(final String fullImagePath, final String jobId, final int attempt) { |
| 173 | + final var noVersion = fullImagePath.split(VERSION_DELIMITER)[0]; |
| 174 | + |
| 175 | + final var nameParts = noVersion.split(DOCKER_DELIMITER); |
| 176 | + var imageName = nameParts[nameParts.length - 1]; |
| 177 | + |
| 178 | + final var randSuffix = RandomStringUtils.randomAlphabetic(5).toLowerCase(); |
| 179 | + final String suffix = "sync" + "-" + jobId + "-" + attempt + "-" + randSuffix; |
| 180 | + |
| 181 | + var podName = imageName + "-" + suffix; |
| 182 | + final var podNameLenLimit = 128; |
| 183 | + if (podName.length() > podNameLenLimit) { |
| 184 | + final var extra = podName.length() - podNameLenLimit; |
| 185 | + imageName = imageName.substring(extra); |
| 186 | + podName = imageName + "-" + suffix; |
| 187 | + } |
| 188 | + |
| 189 | + return podName; |
| 190 | + } |
| 191 | + |
166 | 192 | private Path rebasePath(final Path jobRoot) {
|
167 | 193 | final Path relativePath = workspaceRoot.relativize(jobRoot);
|
168 | 194 | return DATA_MOUNT_DESTINATION.resolve(relativePath);
|
|
0 commit comments