From 563c037d674670256f71b8b70eea3da7e536a5a9 Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Sun, 10 Nov 2019 12:51:32 +0000 Subject: [PATCH] add validation for the name of the project in the init --- cmd/init_project.go | 15 +++++++++ cmd/util/validations.go | 51 +++++++++++++++++++++++++++++++ pkg/scaffold/project/kustomize.go | 3 +- pkg/scaffold/v2/kustomize.go | 3 +- 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 cmd/util/validations.go diff --git a/cmd/init_project.go b/cmd/init_project.go index 9eff693b63..c74f36a07b 100644 --- a/cmd/init_project.go +++ b/cmd/init_project.go @@ -21,6 +21,7 @@ import ( "log" "os" "os/exec" + "path/filepath" "regexp" "strconv" "strings" @@ -137,6 +138,20 @@ func (o *projectOptions) validate() error { return err } } + + // use directory name as prefix + dir, err := os.Getwd() + if err != nil { + return fmt.Errorf("error to get the current path: %v", err) + } + + // check if the name of th project pass is a valid name for k8s objects + // it will be used to create the namespace + projectName := filepath.Base(dir) + if err := util.IsValidName(strings.ToLower(projectName)); err != nil { + return fmt.Errorf("project name (%v) is invalid: (%v)", projectName, err) + } + if o.project.Repo == "" { repoPath, err := findCurrentRepo() if err != nil { diff --git a/cmd/util/validations.go b/cmd/util/validations.go new file mode 100644 index 0000000000..1ca2958c43 --- /dev/null +++ b/cmd/util/validations.go @@ -0,0 +1,51 @@ +package util + +import ( + "fmt" + "regexp" +) + +// The following code came from "k8s.io/apimachinery/pkg/util/validation/validation.go" +// If be required the usage of more funcs from this then please replace it for the import +// --------------------------------------- + +const ( + qnameCharFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?" + // The value is 56 because it will be contact with "-system" = 63 + qualifiedNameMaxLength int = 56 +) +var qualifiedNameRegexp = regexp.MustCompile("^" + qnameCharFmt + "$") + +//IsValidName used to check the name of the project +func IsValidName(value string) []string { + var errs []string + if len(value) > qualifiedNameMaxLength { + errs = append(errs, MaxLenError(qualifiedNameMaxLength)) + } + if !qualifiedNameRegexp.MatchString(value) { + errs = append(errs, RegexError("invalid value for project name", qnameCharFmt)) + } + return errs +} + +// RegexError returns a string explanation of a regex validation failure. +func RegexError(msg string, fmt string, examples ...string) string { + if len(examples) == 0 { + return msg + " (regex used for validation is '" + fmt + "')" + } + msg += " (e.g. " + for i := range examples { + if i > 0 { + msg += " or " + } + msg += "'" + examples[i] + "', " + } + msg += "regex used for validation is '" + fmt + "')" + return msg +} + +// MaxLenError returns a string explanation of a "string too long" validation +// failure. +func MaxLenError(length int) string { + return fmt.Sprintf("must be no more than %d characters", length) +} diff --git a/pkg/scaffold/project/kustomize.go b/pkg/scaffold/project/kustomize.go index 7a4b285ea1..32e7f5e405 100644 --- a/pkg/scaffold/project/kustomize.go +++ b/pkg/scaffold/project/kustomize.go @@ -19,6 +19,7 @@ package project import ( "os" "path/filepath" + "strings" "sigs.k8s.io/kubebuilder/pkg/scaffold/input" ) @@ -44,7 +45,7 @@ func (c *Kustomize) GetInput() (input.Input, error) { if err != nil { return input.Input{}, err } - c.Prefix = filepath.Base(dir) + c.Prefix = strings.ToLower(filepath.Base(dir)) } c.TemplateBody = kustomizeTemplate c.Input.IfExistsAction = input.Error diff --git a/pkg/scaffold/v2/kustomize.go b/pkg/scaffold/v2/kustomize.go index 075e545489..ae1681ef5c 100644 --- a/pkg/scaffold/v2/kustomize.go +++ b/pkg/scaffold/v2/kustomize.go @@ -19,6 +19,7 @@ package v2 import ( "os" "path/filepath" + "strings" "sigs.k8s.io/kubebuilder/pkg/scaffold/input" ) @@ -44,7 +45,7 @@ func (c *Kustomize) GetInput() (input.Input, error) { if err != nil { return input.Input{}, err } - c.Prefix = filepath.Base(dir) + c.Prefix = strings.ToLower(filepath.Base(dir)) } c.TemplateBody = kustomizeTemplate c.Input.IfExistsAction = input.Error