From 2b4848d7cdc1bbd64066100eef300444bd7d200d Mon Sep 17 00:00:00 2001 From: Yvonnick Esnault Date: Fri, 28 Feb 2020 18:09:21 +0100 Subject: [PATCH] fix(hatcheries): fix generated name (#5024) --- sdk/hatchery/starter.go | 23 +++++++++++------------ sdk/hatchery/starter_test.go | 11 ++++++++--- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/sdk/hatchery/starter.go b/sdk/hatchery/starter.go index d85dd7e9b4..cd980f4962 100644 --- a/sdk/hatchery/starter.go +++ b/sdk/hatchery/starter.go @@ -6,7 +6,6 @@ import ( "io" "os" "path/filepath" - "strings" "sync/atomic" "time" @@ -14,6 +13,7 @@ import ( "github.com/ovh/cds/sdk" "github.com/ovh/cds/sdk/log" "github.com/ovh/cds/sdk/namesgenerator" + "github.com/ovh/cds/sdk/slug" ) type workerStarterRequest struct { @@ -226,39 +226,38 @@ func spawnWorkerForJob(ctx context.Context, h Interface, j workerStarterRequest) return true // ok for this job } -// a worker name must be 60 char max, without '.' and '_' -> replaced by '-' -func generateWorkerName(hatcheryName string, isRegister bool, model string) string { +// a worker name must be 60 char max, without '.' and '_', "/" -> replaced by '-' +func generateWorkerName(hatcheryName string, isRegister bool, modelName string) string { prefix := "" if isRegister { prefix = "register-" } maxLength := 63 - hName := strings.Replace(strings.ToLower(hatcheryName), "/", "-", -1) + "-" - modelName := strings.Replace(strings.ToLower(model), "/", "-", -1) - random := strings.Replace(namesgenerator.GetRandomNameCDS(0), "_", "-", -1) - workerName := strings.Replace(fmt.Sprintf("%s%s-%s-%s", prefix, hatcheryName, modelName, random), ".", "-", -1) + hName := hatcheryName + "-" + random := namesgenerator.GetRandomNameCDS(0) + workerName := fmt.Sprintf("%s%s-%s-%s", prefix, hatcheryName, modelName, random) if len(workerName) <= maxLength { - return workerName + return slug.Convert(workerName) } if len(hName) > 10 { hName = "" } workerName = fmt.Sprintf("%s%s%s-%s", prefix, hName, modelName, random) if len(workerName) <= maxLength { - return workerName + return slug.Convert(workerName) } if len(modelName) > 15 { modelName = modelName[:15] } workerName = fmt.Sprintf("%s%s%s-%s", prefix, hName, modelName, random) if len(workerName) <= maxLength { - return workerName + return slug.Convert(workerName) } if len(workerName) > maxLength { - return workerName[:maxLength] + return slug.Convert(workerName[:maxLength]) } - return workerName + return slug.Convert(workerName) } diff --git a/sdk/hatchery/starter_test.go b/sdk/hatchery/starter_test.go index b8409750ee..18267fcd98 100644 --- a/sdk/hatchery/starter_test.go +++ b/sdk/hatchery/starter_test.go @@ -19,17 +19,22 @@ func Test_generateWorkerName(t *testing.T) { { name: "simple", args: args{hatcheryName: "p999-prod", isRegister: true, model: "shared.infra-rust-official-1.41"}, - want: "register-p999-prod-shared.infra-ru", + want: "register-p999-prod-shared-infra-ru", + }, + { + name: "simple special char", + args: args{hatcheryName: "p999/prod", isRegister: true, model: "shared.infra-rust-official-1.41"}, + want: "register-p999-prod-shared-infra-ru", }, { name: "long hatchery name", args: args{hatcheryName: "p999-prod-xxxx-xxxx-xxxx-xxxx-xxxx", isRegister: true, model: "shared.infra-rust-official-1.41"}, - want: "register-shared.infra-ru", + want: "register-shared-infra-ru", }, { name: "long model name", args: args{hatcheryName: "hname", isRegister: true, model: "shared.infra-rust-official-1.41-xxx-xxx-xxx-xxx"}, - want: "register-hname-shared.infra-ru", + want: "register-hname-shared-infra-ru", }, } for _, tt := range tests {