From c570225acec6eac062ada910a0f1d93b28ac6901 Mon Sep 17 00:00:00 2001 From: Davin Chia Date: Tue, 3 May 2022 17:21:37 +0800 Subject: [PATCH] Make job docker container name meaningful. (#12503) Closes #9584. This now follows the Kubernetes name convention of --. This should make things easier for oss users doing oncall for docker deployments. Mirror the Kubernetes name convention: https://github.com/airbytehq/airbyte/blob/master/airbyte-workers/src/main/java/io/airbyte/workers/process/KubeProcessFactory.java#L180 We can probably reuse the naming functions across both processes. We are going to do more changes in this area around making things more ergonomic (e.g. instead of always using sync, we want to replace this with the actual operation - discover, check connection etc.) so we'll do refactoring in that PR. I want to unblock this in the time being since open source users are having difficulty debugging now. --- .../workers/process/DockerProcessFactory.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/airbyte-workers/src/main/java/io/airbyte/workers/process/DockerProcessFactory.java b/airbyte-workers/src/main/java/io/airbyte/workers/process/DockerProcessFactory.java index e40f884cf93875..61c33bd6db3810 100644 --- a/airbyte-workers/src/main/java/io/airbyte/workers/process/DockerProcessFactory.java +++ b/airbyte-workers/src/main/java/io/airbyte/workers/process/DockerProcessFactory.java @@ -23,12 +23,15 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import org.apache.commons.lang3.RandomStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DockerProcessFactory implements ProcessFactory { private static final Logger LOGGER = LoggerFactory.getLogger(DockerProcessFactory.class); + private static final String VERSION_DELIMITER = ":"; + private static final String DOCKER_DELIMITER = "/"; private static final Path DATA_MOUNT_DESTINATION = Path.of("/data"); private static final Path LOCAL_MOUNT_DESTINATION = Path.of("/local"); @@ -114,6 +117,9 @@ public Process create(final String jobId, rebasePath(jobRoot).toString(), // rebases the job root on the job data mount "--log-driver", "none"); + final String containerName = createContainerName(imageName, jobId, attempt); + cmd.add("--name"); + cmd.add(containerName); if (networkName != null) { cmd.add("--network"); @@ -163,6 +169,26 @@ public Process create(final String jobId, } } + private static String createContainerName(final String fullImagePath, final String jobId, final int attempt) { + final var noVersion = fullImagePath.split(VERSION_DELIMITER)[0]; + + final var nameParts = noVersion.split(DOCKER_DELIMITER); + var imageName = nameParts[nameParts.length - 1]; + + final var randSuffix = RandomStringUtils.randomAlphabetic(5).toLowerCase(); + final String suffix = "sync" + "-" + jobId + "-" + attempt + "-" + randSuffix; + + var podName = imageName + "-" + suffix; + final var podNameLenLimit = 128; + if (podName.length() > podNameLenLimit) { + final var extra = podName.length() - podNameLenLimit; + imageName = imageName.substring(extra); + podName = imageName + "-" + suffix; + } + + return podName; + } + private Path rebasePath(final Path jobRoot) { final Path relativePath = workspaceRoot.relativize(jobRoot); return DATA_MOUNT_DESTINATION.resolve(relativePath);