Skip to content

Commit

Permalink
fix(hatcheries): fix generated name (#5024)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault committed Feb 28, 2020
1 parent bb9f425 commit 2b4848d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
23 changes: 11 additions & 12 deletions sdk/hatchery/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"io"
"os"
"path/filepath"
"strings"
"sync/atomic"
"time"

"github.com/ovh/cds/engine/api/observability"
"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 {
Expand Down Expand Up @@ -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)
}
11 changes: 8 additions & 3 deletions sdk/hatchery/starter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2b4848d

Please sign in to comment.