From 24c16fbf2554a472c7ec088f58de42aaa78f7767 Mon Sep 17 00:00:00 2001 From: Aidan Date: Fri, 3 Feb 2023 11:54:19 -0800 Subject: [PATCH] Update max container name length (#1597) * Update max container name length Signed-off-by: Aidan Jensen * Use hashed name instead to prevent conflicts Signed-off-by: Aidan Jensen --------- Signed-off-by: Aidan Jensen Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- pkg/runner/run_context.go | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index db28328..501eb71 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -5,6 +5,7 @@ import ( "bufio" "context" "crypto/rand" + "crypto/sha256" "encoding/hex" "encoding/json" "errors" @@ -505,26 +506,16 @@ func mergeMaps(maps ...map[string]string) map[string]string { } func createContainerName(parts ...string) string { - name := make([]string, 0) + name := strings.Join(parts, "-") pattern := regexp.MustCompile("[^a-zA-Z0-9]") - partLen := (30 / len(parts)) - 1 - for i, part := range parts { - if i == len(parts)-1 { - name = append(name, pattern.ReplaceAllString(part, "-")) - } else { - // If any part has a '-' on the end it is likely part of a matrix job. - // Let's preserve the number to prevent clashes in container names. - re := regexp.MustCompile("-[0-9]+$") - num := re.FindStringSubmatch(part) - if len(num) > 0 { - name = append(name, trimToLen(pattern.ReplaceAllString(part, "-"), partLen-len(num[0]))) - name = append(name, num[0]) - } else { - name = append(name, trimToLen(pattern.ReplaceAllString(part, "-"), partLen)) - } - } - } - return strings.ReplaceAll(strings.Trim(strings.Join(name, "-"), "-"), "--", "-") + name = pattern.ReplaceAllString(name, "-") + name = strings.ReplaceAll(name, "--", "-") + hash := sha256.Sum256([]byte(name)) + + // SHA256 is 64 hex characters. So trim name to 63 characters to make room for the hash and separator + trimmedName := strings.Trim(trimToLen(name, 63), "-") + + return fmt.Sprintf("%s-%x", trimmedName, hash) } func trimToLen(s string, l int) string {