From 3766b48ac47f2e688b938861a41253386e56c77e Mon Sep 17 00:00:00 2001 From: Eric Stroczynski Date: Tue, 21 Jul 2020 16:56:06 -0700 Subject: [PATCH] pkg/model/config: add 'project-name' which configures a project's name pkg/plugin/v3/init.go: add --project-name which sets a config's 'project-name' key --- .../src/cronjob-tutorial/cronjob-tutorial.md | 5 +++++ pkg/model/config/config.go | 3 +++ pkg/plugin/v3/init.go | 22 +++++++++++++------ pkg/plugin/v3/scaffolds/init.go | 2 +- .../scaffolds/internal/templates/kustomize.go | 16 +++++--------- testdata/project-v3-addon/PROJECT | 1 + testdata/project-v3-multigroup/PROJECT | 1 + testdata/project-v3/PROJECT | 1 + 8 files changed, 32 insertions(+), 19 deletions(-) diff --git a/docs/book/src/cronjob-tutorial/cronjob-tutorial.md b/docs/book/src/cronjob-tutorial/cronjob-tutorial.md index 8fd3bf5caad..ac57999bbfd 100644 --- a/docs/book/src/cronjob-tutorial/cronjob-tutorial.md +++ b/docs/book/src/cronjob-tutorial/cronjob-tutorial.md @@ -49,5 +49,10 @@ project: kubebuilder init --domain tutorial.kubebuilder.io ``` + + Now that we've got a project in place, let's take a look at what Kubebuilder has scaffolded for us so far... diff --git a/pkg/model/config/config.go b/pkg/model/config/config.go index f13a2ceaeb8..b9d21ca8fe6 100644 --- a/pkg/model/config/config.go +++ b/pkg/model/config/config.go @@ -41,6 +41,9 @@ type Config struct { // Repo is the go package name of the project root Repo string `json:"repo,omitempty"` + // ProjectName is the name of this controller project set on initialization. + ProjectName string `json:"project-name,omitempty"` + // Resources tracks scaffolded resources in the project // This info is tracked only in project with version 2 Resources []GVK `json:"resources,omitempty"` diff --git a/pkg/plugin/v3/init.go b/pkg/plugin/v3/init.go index 69fd1ccd852..d08c2344676 100644 --- a/pkg/plugin/v3/init.go +++ b/pkg/plugin/v3/init.go @@ -18,6 +18,7 @@ package v3 import ( "fmt" + "os" "path" "strings" @@ -90,6 +91,7 @@ func (p *initPlugin) BindFlags(fs *pflag.FlagSet) { fs.StringVar(&p.config.Repo, "repo", "", "name to use for go module (e.g., github.com/user/repo), "+ "defaults to the go package of the current working directory.") fs.StringVar(&p.config.Domain, "domain", "my.domain", "domain for groups") + fs.StringVar(&p.config.ProjectName, "project-name", "", "name of this project") } func (p *initPlugin) InjectConfig(c *config.Config) { @@ -108,6 +110,19 @@ func (p *initPlugin) Validate() error { return err } } + + // Check if the project name is a valid k8s namespace (DNS 1123 label). + if p.config.ProjectName == "" { + dir, err := os.Getwd() + if err != nil { + return fmt.Errorf("error getting current directory: %v", err) + } + p.config.ProjectName = path.Base(dir) + } + if err := validation.IsDNS1123Label(strings.ToLower(p.config.ProjectName)); err != nil { + return fmt.Errorf("project name (%s) is invalid: %v", p.config.ProjectName, err) + } + // Try to guess repository if flag is not set. if p.config.Repo == "" { repoPath, err := util.FindCurrentRepo() @@ -117,13 +132,6 @@ func (p *initPlugin) Validate() error { p.config.Repo = repoPath } - // Use base repository path as project name, since a project should be able to exist in an - // arbitrarily-named directory. - projectName := path.Base(p.config.Repo) - if err := validation.IsDNS1123Label(strings.ToLower(projectName)); err != nil { - return fmt.Errorf("project name (%s) is invalid: %v", projectName, err) - } - return nil } diff --git a/pkg/plugin/v3/scaffolds/init.go b/pkg/plugin/v3/scaffolds/init.go index 7ea614e98c8..d0125300983 100644 --- a/pkg/plugin/v3/scaffolds/init.go +++ b/pkg/plugin/v3/scaffolds/init.go @@ -113,7 +113,7 @@ func (s *initScaffolder) scaffold() error { }, &templates.Dockerfile{}, &templates.DockerignoreFile{}, - &templates.Kustomize{}, + &templates.Kustomize{Prefix: s.config.ProjectName}, &templates.ManagerWebhookPatch{}, &templates.ManagerRoleBinding{}, &templates.LeaderElectionRole{}, diff --git a/pkg/plugin/v3/scaffolds/internal/templates/kustomize.go b/pkg/plugin/v3/scaffolds/internal/templates/kustomize.go index 347d492a8fc..bbe4d68a05d 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/kustomize.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/kustomize.go @@ -18,7 +18,6 @@ package templates import ( "os" - "path" "path/filepath" "strings" @@ -30,7 +29,6 @@ var _ file.Template = &Kustomize{} // Kustomize scaffolds the Kustomization file for the default overlay type Kustomize struct { file.TemplateMixin - file.RepositoryMixin // Prefix to use for name prefix customization Prefix string @@ -47,16 +45,12 @@ func (f *Kustomize) SetTemplateDefaults() error { f.IfExistsAction = file.Error if f.Prefix == "" { - if f.Repo != "" { - f.Prefix = strings.ToLower(path.Base(f.Repo)) - } else { - // Use directory name as prefix if no repo is present. - dir, err := os.Getwd() - if err != nil { - return err - } - f.Prefix = strings.ToLower(filepath.Base(dir)) + // Use directory name as prefix. + wd, err := os.Getwd() + if err != nil { + return err } + f.Prefix = strings.ToLower(filepath.Base(wd)) } return nil diff --git a/testdata/project-v3-addon/PROJECT b/testdata/project-v3-addon/PROJECT index e76966fd5c8..b0e28924a74 100644 --- a/testdata/project-v3-addon/PROJECT +++ b/testdata/project-v3-addon/PROJECT @@ -1,5 +1,6 @@ domain: testproject.org layout: go.kubebuilder.io/v3-alpha +project-name: project-v3-addon repo: sigs.k8s.io/kubebuilder/testdata/project-v3-addon resources: - group: crew diff --git a/testdata/project-v3-multigroup/PROJECT b/testdata/project-v3-multigroup/PROJECT index a5a4e01ce04..71f2bf6ca75 100644 --- a/testdata/project-v3-multigroup/PROJECT +++ b/testdata/project-v3-multigroup/PROJECT @@ -1,6 +1,7 @@ domain: testproject.org layout: go.kubebuilder.io/v3-alpha multigroup: true +project-name: project-v3-multigroup repo: sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup resources: - group: crew diff --git a/testdata/project-v3/PROJECT b/testdata/project-v3/PROJECT index 3c9fd6b0113..525156da701 100644 --- a/testdata/project-v3/PROJECT +++ b/testdata/project-v3/PROJECT @@ -1,5 +1,6 @@ domain: testproject.org layout: go.kubebuilder.io/v3-alpha +project-name: project-v3 repo: sigs.k8s.io/kubebuilder/testdata/project-v3 resources: - group: crew