Skip to content

Commit

Permalink
Merge pull request #1603 from estroz/feature/project-name-config
Browse files Browse the repository at this point in the history
config: add `project-name` config key and optional `init --project-name`
  • Loading branch information
k8s-ci-robot committed Jul 23, 2020
2 parents 76edcfb + 3766b48 commit 851e4f5
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 19 deletions.
5 changes: 5 additions & 0 deletions docs/book/src/cronjob-tutorial/cronjob-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,10 @@ project:
kubebuilder init --domain tutorial.kubebuilder.io
```

<aside class="note">
Your project's name defaults to that of your current working directory.
You can pass `--project-name=<dns1123-label-string>` to set a different project name.
</aside>

Now that we've got a project in place, let's take a look at what
Kubebuilder has scaffolded for us so far...
3 changes: 3 additions & 0 deletions pkg/model/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
22 changes: 15 additions & 7 deletions pkg/plugin/v3/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v3

import (
"fmt"
"os"
"path"
"strings"

Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/v3/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
16 changes: 5 additions & 11 deletions pkg/plugin/v3/scaffolds/internal/templates/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package templates

import (
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions testdata/project-v3-addon/PROJECT
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions testdata/project-v3-multigroup/PROJECT
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions testdata/project-v3/PROJECT
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 851e4f5

Please sign in to comment.