8000 fix(hatcheries): fix generated name (#5024) · ovh/cds@2b4848d · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 2b4848d

Browse files
authored
fix(hatcheries): fix generated name (#5024)
1 parent bb9f425 commit 2b4848d

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

sdk/hatchery/starter.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import (
66
"io"
77
"os"
88
"path/filepath"
9-
"strings"
109
"sync/atomic"
1110
"time"
1211

1312
"github.com/ovh/cds/engine/api/observability"
1413
"github.com/ovh/cds/sdk"
1514
"github.com/ovh/cds/sdk/log"
1615
"github.com/ovh/cds/sdk/namesgenerator"
16+
"github.com/ovh/cds/sdk/slug"
1717
)
1818

1919
type workerStarterRequest struct {
@@ -226,39 +226,38 @@ func spawnWorkerForJob(ctx context.Context, h Interface, j workerStarterRequest)
226226
return true // ok for this job
227227
}
228228

229-
// a worker name must be 60 char max, without '.' and '_' -> replaced by '-'
230-
func generateWorkerName(hatcheryName string, isRegister bool, model string) string {
229+
// a worker name must be 60 char max, without '.' and '_', "/" -> replaced by '-'
230+
func generateWorkerName(hatcheryName string, isRegister bool, modelName string) string {
231231
prefix := ""
232232
if isRegister {
233233
prefix = "register-"
234234
}
235235

236236
maxLength := 63
237-
hName := strings.Replace(strings.ToLower(hatcheryName), "/", "-", -1) + "-"
238-
modelName := strings.Replace(strings.ToLower(model), "/", "-", -1)
239-
random := strings.Replace(namesgenerator.GetRandomNameCDS(0), "_", "-", -1)
240-
workerName := strings.Replace(fmt.Sprintf("%s%s-%s-%s", prefix, hatcheryName, modelName, random), ".", "-", -1)
237+
hName := hatcheryName + "-"
238+
random := namesgenerator.GetRandomNameCDS(0)
239+
workerName := fmt.Sprintf("%s%s-%s-%s", prefix, hatcheryName, modelName, random)
241240

242241
if len(workerName) <= maxLength {
243-
return workerName
242+
return slug.Convert(workerName)
244243
}
245244
if len(hName) > 10 {
246245
hName = ""
247246
}
248247
workerName = fmt.Sprintf("%s%s%s-%s", prefix, hName, modelName, random)
249248
if len(workerName) <= maxLength {
250-
return workerName
249+
return slug.Convert(workerName)
251250
}
252251
if len(modelName) > 15 {
253252
modelName = modelName[:15]
254253
}
255254
workerName = fmt.Sprintf("%s%s%s-%s", prefix, hName, modelName, random)
256255
if len(workerName) <= maxLength {
257-
return workerName
256+
return slug.Convert(workerName)
258257
}
259258

260259
if len(workerName) > maxLength {
261-
return workerName[:maxLength]
260+
return slug.Convert(workerName[:maxLength])
262261
}
263-
return workerName
262+
return slug.Convert(workerName)
264263
}

sdk/hatchery/starter_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@ func Test_generateWorkerName(t *testing.T) {
1919
{
2020
name: "simple",
2121
args: args{hatcheryName: "p999-prod", isRegister: true, model: "shared.infra-rust-official-1.41"},
22-
want: "register-p999-prod-shared.infra-ru",
22+
want: "register-p999-prod-shared-infra-ru",
23+
},
24+
{
25+
name: "simple special char",
26+
args: args{hatcheryName: "p999/prod", isRegister: true, model: "shared.infra-rust-official-1.41"},
27+
want: "register-p999-prod-shared-infra-ru",
2328
},
2429
{
2530
name: "long hatchery name",
2631
args: args{hatcheryName: "p999-prod-xxxx-xxxx-xxxx-xxxx-xxxx", isRegister: true, model: "shared.infra-rust-official-1.41"},
27-
want: "register-shared.infra-ru",
32+
want: "register-shared-infra-ru",
2833
},
2934
{
3035
name: "long model name",
3136
args: args{hatcheryName: "hname", isRegister: true, model: "shared.infra-rust-official-1.41-xxx-xxx-xxx-xxx"},
32-
want: "register-hname-shared.infra-ru",
37+
want: "register-hname-shared-infra-ru",
3338
},
3439
}
3540
for _, tt := range tests {

0 commit comments

Comments
 (0)
0