Skip to content

Commit

Permalink
This commit adds pkg/model/file.ProjectNameMixin, which will inject
Browse files Browse the repository at this point in the history
the Config.ProjectName into any structs that embed this mixin.
  • Loading branch information
estroz committed Jul 23, 2020
1 parent a7be4b7 commit f028d0a
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/model/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Config struct {
Repo string `json:"repo,omitempty"`

// ProjectName is the name of this controller project set on initialization.
ProjectName string `json:"project-name,omitempty"`
ProjectName string `json:"projectName,omitempty"`

// Resources tracks scaffolded resources in the project
// This info is tracked only in project with version 2
Expand Down
6 changes: 6 additions & 0 deletions pkg/model/file/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ type HasResource interface {
InjectResource(*resource.Resource)
}

// HasProjectName allows a project name to be used on a template.
type HasProjectName interface {
// InjectProjectName sets the template project name.
InjectProjectName(string)
}

// UseCustomFuncMap allows a template to use a custom template.FuncMap instead of the default FuncMap.
type UseCustomFuncMap interface {
// GetFuncMap returns a custom FuncMap.
Expand Down
12 changes: 12 additions & 0 deletions pkg/model/file/mixins.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,15 @@ func (m *ResourceMixin) InjectResource(res *resource.Resource) {
m.Resource = res
}
}

// ProjectNameMixin provides templates with an injectable project name field.
type ProjectNameMixin struct {
ProjectName string
}

// InjectProjectName implements HasProjectName.
func (m *ProjectNameMixin) InjectProjectName(projectName string) {
if m.ProjectName == "" {
m.ProjectName = projectName
}
}
3 changes: 3 additions & 0 deletions pkg/model/universe.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ func (u Universe) InjectInto(builder file.Builder) {
if builderWithMultiGroup, hasMultiGroup := builder.(file.HasMultiGroup); hasMultiGroup {
builderWithMultiGroup.InjectMultiGroup(u.Config.MultiGroup)
}
if builderWithProjectName, hasProjectName := builder.(file.HasProjectName); hasProjectName {
builderWithProjectName.InjectProjectName(u.Config.ProjectName)
}
}
// Inject boilerplate
if builderWithBoilerplate, hasBoilerplate := builder.(file.HasBoilerplate); hasBoilerplate {
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugin/v3/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ func (p *initPlugin) Validate() error {
if err != nil {
return fmt.Errorf("error getting current directory: %v", err)
}
p.config.ProjectName = path.Base(dir)
p.config.ProjectName = strings.ToLower(path.Base(dir))
}
if err := validation.IsDNS1123Label(strings.ToLower(p.config.ProjectName)); err != nil {
if err := validation.IsDNS1123Label(p.config.ProjectName); err != nil {
return fmt.Errorf("project name (%s) is invalid: %v", p.config.ProjectName, err)
}

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{Prefix: s.config.ProjectName},
&templates.Kustomize{},
&templates.ManagerWebhookPatch{},
&templates.ManagerRoleBinding{},
&templates.LeaderElectionRole{},
Expand Down
19 changes: 3 additions & 16 deletions pkg/plugin/v3/scaffolds/internal/templates/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ limitations under the License.
package templates

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

"sigs.k8s.io/kubebuilder/pkg/model/file"
)
Expand All @@ -29,9 +27,7 @@ var _ file.Template = &Kustomize{}
// Kustomize scaffolds the Kustomization file for the default overlay
type Kustomize struct {
file.TemplateMixin

// Prefix to use for name prefix customization
Prefix string
file.ProjectNameMixin
}

// SetTemplateDefaults implements input.Template
Expand All @@ -44,27 +40,18 @@ func (f *Kustomize) SetTemplateDefaults() error {

f.IfExistsAction = file.Error

if f.Prefix == "" {
// Use directory name as prefix.
wd, err := os.Getwd()
if err != nil {
return err
}
f.Prefix = strings.ToLower(filepath.Base(wd))
}

return nil
}

const kustomizeTemplate = `# Adds namespace to all resources.
namespace: {{ .Prefix }}-system
namespace: {{ .ProjectName }}-system
# Value of this field is prepended to the
# names of all resources, e.g. a deployment named
# "wordpress" becomes "alices-wordpress".
# Note that it should also match with the prefix (text before '-') of the namespace
# field above.
namePrefix: {{ .Prefix }}-
namePrefix: {{ .ProjectName }}-
# Labels to add to all resources and selectors.
#commonLabels:
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v3-addon/PROJECT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
domain: testproject.org
layout: go.kubebuilder.io/v3-alpha
project-name: project-v3-addon
projectName: project-v3-addon
repo: sigs.k8s.io/kubebuilder/testdata/project-v3-addon
resources:
- group: crew
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v3-multigroup/PROJECT
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
domain: testproject.org
layout: go.kubebuilder.io/v3-alpha
multigroup: true
project-name: project-v3-multigroup
projectName: project-v3-multigroup
repo: sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup
resources:
- group: crew
Expand Down
2 changes: 1 addition & 1 deletion testdata/project-v3/PROJECT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
domain: testproject.org
layout: go.kubebuilder.io/v3-alpha
project-name: project-v3
projectName: project-v3
repo: sigs.k8s.io/kubebuilder/testdata/project-v3
resources:
- group: crew
Expand Down

0 comments on commit f028d0a

Please sign in to comment.