Skip to content

Commit

Permalink
馃悰 Kube Pod Name Edge Case (#5352)
Browse files Browse the repository at this point in the history

Co-authored-by: Davin Chia <davinchia@gmail.com>
  • Loading branch information
jrhizor and davinchia committed Aug 13, 2021
1 parent 1ccd218 commit 92c6092
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Expand Up @@ -31,6 +31,8 @@
import io.kubernetes.client.openapi.ApiClient;
import java.nio.file.Path;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,6 +41,8 @@ public class KubeProcessFactory implements ProcessFactory {

private static final Logger LOGGER = LoggerFactory.getLogger(KubeProcessFactory.class);

private static final Pattern ALPHABETIC = Pattern.compile("[a-zA-Z]+");;

private final String namespace;
private final ApiClient officialClient;
private final KubernetesClient fabricClient;
Expand Down Expand Up @@ -108,7 +112,8 @@ public Process create(String jobId,
* This is followed by a colon and a version number. e.g. airbyte/scheduler:v1 or
* gcr.io/my-project/image-name:v2.
*
* Kubernetes has a maximum pod name length of 63 characters.
* Kubernetes has a maximum pod name length of 63 characters, and names must start with an
* alphabetic character.
*
* With these two facts, attempt to construct a unique Pod name with the image name present for
* easier operations.
Expand All @@ -134,7 +139,12 @@ protected static String createPodName(String fullImagePath, String jobId, int at
podName = imageName + "-" + suffix;
}

return podName;
final Matcher m = ALPHABETIC.matcher(podName);
// Since we add worker-UUID as a suffix a couple of lines up, there will always be a substring
// starting with an alphabetic character.
// If the image name is a no-op, this function should always return `worker-UUID` at the minimum.
m.find();
return podName.substring(m.start());
}

}
Expand Up @@ -43,4 +43,31 @@ void getPodNameTruncated() {
Assertions.assertEquals("very-very-very-long-name-longer-than-63-chars-worker-1-10-", withoutRandSuffix);
}

@Test
void testHandlingDashAsFirstCharacter() {
var uuid = "7339ba3b-cb53-4210-9591-c70d4a372330";
var name = KubeProcessFactory.createPodName("airbyte/source-google-adwordsv2:latest", uuid, 10);

var withoutRandSuffix = name.substring(0, name.length() - 5);
Assertions.assertEquals("adwordsv2-worker-7339ba3b-cb53-4210-9591-c70d4a372330-10-", withoutRandSuffix);
}

@Test
void testOnlyDashes() {
var uuid = "7339ba3b-cb53-4210-9591-c70d4a372330";
var name = KubeProcessFactory.createPodName("--------", uuid, 10);

var withoutRandSuffix = name.substring(0, name.length() - 5);
Assertions.assertEquals("worker-7339ba3b-cb53-4210-9591-c70d4a372330-10-", withoutRandSuffix);
}

@Test
void testOnlyNumeric() {
var uuid = "7339ba3b-cb53-4210-9591-c70d4a372330";
var name = KubeProcessFactory.createPodName("0000000000", uuid, 10);

var withoutRandSuffix = name.substring(0, name.length() - 5);
Assertions.assertEquals("worker-7339ba3b-cb53-4210-9591-c70d4a372330-10-", withoutRandSuffix);
}

}

0 comments on commit 92c6092

Please sign in to comment.