Skip to content

Commit 1ff9245

Browse files
author
Matthew Fisher
committed
fix(builder): truncate slugbuilder and dockerbuilder pod name length
kubernetes has a name length limit of 63 characters. We must truncate the name such that it stays below that limit with the new app name length bump.
1 parent 089eb67 commit 1ff9245

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

pkg/gitreceive/k8s_util.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,21 @@ const (
3333

3434
func dockerBuilderPodName(appName, shortSha string) string {
3535
uid := uuid.New()[:8]
36+
// NOTE(bacongobbler): pod names cannot exceed 63 characters in length, so we truncate
37+
// the application name to stay under that limit when adding all the extra metadata to the name
38+
if len(appName) > 33 {
39+
appName = appName[:33]
40+
}
3641
return fmt.Sprintf("dockerbuild-%s-%s-%s", appName, shortSha, uid)
3742
}
3843

3944
func slugBuilderPodName(appName, shortSha string) string {
4045
uid := uuid.New()[:8]
46+
// NOTE(bacongobbler): pod names cannot exceed 63 characters in length, so we truncate
47+
// the application name to stay under that limit when adding all the extra metadata to the name
48+
if len(appName) > 35 {
49+
appName = appName[:35]
50+
}
4151
return fmt.Sprintf("slugbuild-%s-%s-%s", appName, shortSha, uid)
4252
}
4353

pkg/gitreceive/k8s_util_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,30 @@ import (
1515
func TestDockerBuilderPodName(t *testing.T) {
1616
name := dockerBuilderPodName("demo", "12345678")
1717
if !strings.HasPrefix(name, "dockerbuild-demo-12345678-") {
18-
t.Fatalf("expected pod name dockerbuild-demo-12345678-*, got %s", name)
18+
t.Errorf("expected pod name dockerbuild-demo-12345678-*, got %s", name)
19+
}
20+
21+
name = dockerBuilderPodName("this-name-has-more-than-24-characters-in-length", "12345678")
22+
if !strings.HasPrefix(name, "dockerbuild-this-name-has-more-than-24-charac-12345678-") {
23+
t.Errorf("expected pod name dockerbuild-this-name-has-more-than-24-charac-12345678-*, got %s", name)
24+
}
25+
if len(name) > 63 {
26+
t.Errorf("expected dockerbuilder pod name length to be <= 63 characters in length, got %d", len(name))
1927
}
2028
}
2129

2230
func TestSlugBuilderPodName(t *testing.T) {
2331
name := slugBuilderPodName("demo", "12345678")
2432
if !strings.HasPrefix(name, "slugbuild-demo-12345678-") {
25-
t.Fatalf("expected pod name slugbuild-demo-12345678-*, got %s", name)
33+
t.Errorf("expected pod name slugbuild-demo-12345678-*, got %s", name)
34+
}
35+
36+
name = slugBuilderPodName("this-name-has-more-than-24-characters-in-length", "12345678")
37+
if !strings.HasPrefix(name, "slugbuild-this-name-has-more-than-24-characte-12345678-") {
38+
t.Errorf("expected pod name slugbuild-this-name-has-more-than-24-characte-12345678-*, got %s", name)
39+
}
40+
if len(name) > 63 {
41+
t.Errorf("expected slugbuilder pod name length to be <= 63 characters in length, got %d", len(name))
2642
}
2743
}
2844

0 commit comments

Comments
 (0)