Skip to content

Commit 72a0ef1

Browse files
authored
Merge pull request #1173 from camilamacedo86/add-init-name-check
Add init name check
2 parents 814dadb + 563c037 commit 72a0ef1

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

cmd/init_project.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"log"
2222
"os"
2323
"os/exec"
24+
"path/filepath"
2425
"regexp"
2526
"strconv"
2627
"strings"
@@ -137,6 +138,20 @@ func (o *projectOptions) validate() error {
137138
return err
138139
}
139140
}
141+
142+
// use directory name as prefix
143+
dir, err := os.Getwd()
144+
if err != nil {
145+
return fmt.Errorf("error to get the current path: %v", err)
146+
}
147+
148+
// check if the name of th project pass is a valid name for k8s objects
149+
// it will be used to create the namespace
150+
projectName := filepath.Base(dir)
151+
if err := util.IsValidName(strings.ToLower(projectName)); err != nil {
152+
return fmt.Errorf("project name (%v) is invalid: (%v)", projectName, err)
153+
}
154+
140155
if o.project.Repo == "" {
141156
repoPath, err := findCurrentRepo()
142157
if err != nil {

cmd/util/validations.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package util
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
)
7+
8+
// The following code came from "k8s.io/apimachinery/pkg/util/validation/validation.go"
9+
// If be required the usage of more funcs from this then please replace it for the import
10+
// ---------------------------------------
11+
12+
const (
13+
qnameCharFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
14+
// The value is 56 because it will be contact with "-system" = 63
15+
qualifiedNameMaxLength int = 56
16+
)
17+
var qualifiedNameRegexp = regexp.MustCompile("^" + qnameCharFmt + "$")
18+
19+
//IsValidName used to check the name of the project
20+
func IsValidName(value string) []string {
21+
var errs []string
22+
if len(value) > qualifiedNameMaxLength {
23+
errs = append(errs, MaxLenError(qualifiedNameMaxLength))
24+
}
25+
if !qualifiedNameRegexp.MatchString(value) {
26+
errs = append(errs, RegexError("invalid value for project name", qnameCharFmt))
27+
}
28+
return errs
29+
}
30+
31+
// RegexError returns a string explanation of a regex validation failure.
32+
func RegexError(msg string, fmt string, examples ...string) string {
33+
if len(examples) == 0 {
34+
return msg + " (regex used for validation is '" + fmt + "')"
35+
}
36+
msg += " (e.g. "
37+
for i := range examples {
38+
if i > 0 {
39+
msg += " or "
40+
}
41+
msg += "'" + examples[i] + "', "
42+
}
43+
msg += "regex used for validation is '" + fmt + "')"
44+
return msg
45+
}
46+
47+
// MaxLenError returns a string explanation of a "string too long" validation
48+
// failure.
49+
func MaxLenError(length int) string {
50+
return fmt.Sprintf("must be no more than %d characters", length)
51+
}

pkg/scaffold/project/kustomize.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package project
1919
import (
2020
"os"
2121
"path/filepath"
22+
"strings"
2223

2324
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
2425
)
@@ -44,7 +45,7 @@ func (c *Kustomize) GetInput() (input.Input, error) {
4445
if err != nil {
4546
return input.Input{}, err
4647
}
47-
c.Prefix = filepath.Base(dir)
48+
c.Prefix = strings.ToLower(filepath.Base(dir))
4849
}
4950
c.TemplateBody = kustomizeTemplate
5051
c.Input.IfExistsAction = input.Error

pkg/scaffold/v2/kustomize.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package v2
1919
import (
2020
"os"
2121
"path/filepath"
22+
"strings"
2223

2324
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
2425
)
@@ -44,7 +45,7 @@ func (c *Kustomize) GetInput() (input.Input, error) {
4445
if err != nil {
4546
return input.Input{}, err
4647
}
47-
c.Prefix = filepath.Base(dir)
48+
c.Prefix = strings.ToLower(filepath.Base(dir))
4849
}
4950
c.TemplateBody = kustomizeTemplate
5051
c.Input.IfExistsAction = input.Error

0 commit comments

Comments
 (0)