Skip to content

Commit 400d31d

Browse files
author
OpenShift Bot
committed
Merge pull request #5331 from php-coder/gh5182_new_app_invalid_name
Merged by openshift-bot
2 parents a60a542 + ba0c0a1 commit 400d31d

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

pkg/generate/app/cmd/newapp.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"fmt"
55
"io"
6+
"regexp"
67
"strconv"
78
"strings"
89

@@ -44,6 +45,9 @@ const (
4445
// and no Dockerfile is detected in the repository.
4546
var ErrNoDockerfileDetected = fmt.Errorf("No Dockerfile was found in the repository and the requested build strategy is 'docker'")
4647

48+
// the opposite of kvalidation.DNS1123LabelFmt
49+
var invalidNameCharactersRegexp = regexp.MustCompile("[^-a-z0-9]")
50+
4751
// AppConfig contains all the necessary configuration for an application
4852
type AppConfig struct {
4953
SourceRepositories util.StringList
@@ -587,14 +591,21 @@ func ensureValidUniqueName(names map[string]int, name string) (string, error) {
587591
if len(name) < 2 {
588592
return "", fmt.Errorf("invalid name: %s", name)
589593
}
594+
595+
// Make all names lowercase
596+
name = strings.ToLower(name)
597+
598+
// Remove everything except [-0-9a-z]
599+
name = invalidNameCharactersRegexp.ReplaceAllString(name, "")
600+
601+
// Remove leading hyphen(s) that may be introduced by the previous step
602+
name = strings.TrimLeft(name, "-")
603+
590604
if len(name) > kvalidation.DNS1123SubdomainMaxLength {
591605
glog.V(4).Infof("Trimming %s to maximum allowable length (%d)\n", name, kvalidation.DNS1123SubdomainMaxLength)
592606
name = name[:kvalidation.DNS1123SubdomainMaxLength]
593607
}
594608

595-
// Make all names lowercase
596-
name = strings.ToLower(name)
597-
598609
count, existing := names[name]
599610
if !existing {
600611
names[name] = 0

pkg/generate/app/cmd/newapp_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,11 @@ func TestEnsureValidUniqueName(t *testing.T) {
12641264
input: []string{"One", "ONE", "tWo"},
12651265
expected: []string{"one", "one-1", "two"},
12661266
},
1267+
{
1268+
name: "non-standard characters",
1269+
input: []string{"Emby.One", "test-_test", "_-_", "@-MyRepo"},
1270+
expected: []string{"embyone", "test-test", "", "myrepo"},
1271+
},
12671272
{
12681273
name: "short name",
12691274
input: []string{"t"},

0 commit comments

Comments
 (0)