File tree 10 files changed +31
-23
lines changed
10 files changed +31
-23
lines changed Original file line number Diff line number Diff line change @@ -42,7 +42,7 @@ type Config struct {
42
42
Repo string `json:"repo,omitempty"`
43
43
44
44
// ProjectName is the name of this controller project set on initialization.
45
- ProjectName string `json:"project-name ,omitempty"`
45
+ ProjectName string `json:"projectName ,omitempty"`
46
46
47
47
// Resources tracks scaffolded resources in the project
48
48
// This info is tracked only in project with version 2
Original file line number Diff line number Diff line change @@ -85,6 +85,12 @@ type HasResource interface {
85
85
InjectResource (* resource.Resource )
86
86
}
87
87
88
+ // HasProjectName allows a project name to be used on a template.
89
+ type HasProjectName interface {
90
+ // InjectProjectName sets the template project name.
91
+ InjectProjectName (string )
92
+ }
93
+
88
94
// UseCustomFuncMap allows a template to use a custom template.FuncMap instead of the default FuncMap.
89
95
type UseCustomFuncMap interface {
90
96
// GetFuncMap returns a custom FuncMap.
Original file line number Diff line number Diff line change @@ -128,3 +128,15 @@ func (m *ResourceMixin) InjectResource(res *resource.Resource) {
128
128
m .Resource = res
129
129
}
130
130
}
131
+
132
+ // ProjectNameMixin provides templates with an injectable project name field.
133
+ type ProjectNameMixin struct {
134
+ ProjectName string
135
+ }
136
+
137
+ // InjectProjectName implements HasProjectName.
138
+ func (m * ProjectNameMixin ) InjectProjectName (projectName string ) {
139
+ if m .ProjectName == "" {
140
+ m .ProjectName = projectName
141
+ }
142
+ }
Original file line number Diff line number Diff line change @@ -91,6 +91,9 @@ func (u Universe) InjectInto(builder file.Builder) {
91
91
if builderWithMultiGroup , hasMultiGroup := builder .(file.HasMultiGroup ); hasMultiGroup {
92
92
builderWithMultiGroup .InjectMultiGroup (u .Config .MultiGroup )
93
93
}
94
+ if builderWithProjectName , hasProjectName := builder .(file.HasProjectName ); hasProjectName {
95
+ builderWithProjectName .InjectProjectName (u .Config .ProjectName )
96
+ }
94
97
}
95
98
// Inject boilerplate
96
99
if builderWithBoilerplate , hasBoilerplate := builder .(file.HasBoilerplate ); hasBoilerplate {
Original file line number Diff line number Diff line change @@ -117,9 +117,9 @@ func (p *initPlugin) Validate() error {
117
117
if err != nil {
118
118
return fmt .Errorf ("error getting current directory: %v" , err )
119
119
}
120
- p .config .ProjectName = path .Base (dir )
120
+ p .config .ProjectName = strings . ToLower ( path .Base (dir ) )
121
121
}
122
- if err := validation .IsDNS1123Label (strings . ToLower ( p .config .ProjectName ) ); err != nil {
122
+ if err := validation .IsDNS1123Label (p .config .ProjectName ); err != nil {
123
123
return fmt .Errorf ("project name (%s) is invalid: %v" , p .config .ProjectName , err )
124
124
}
125
125
Original file line number Diff line number Diff line change @@ -113,7 +113,7 @@ func (s *initScaffolder) scaffold() error {
113
113
},
114
114
& templates.Dockerfile {},
115
115
& templates.DockerignoreFile {},
116
- & templates.Kustomize {Prefix : s . config . ProjectName },
116
+ & templates.Kustomize {},
117
117
& templates.ManagerWebhookPatch {},
118
118
& templates.ManagerRoleBinding {},
119
119
& templates.LeaderElectionRole {},
Original file line number Diff line number Diff line change @@ -17,9 +17,7 @@ limitations under the License.
17
17
package templates
18
18
19
19
import (
20
- "os"
21
20
"path/filepath"
22
- "strings"
23
21
24
22
"sigs.k8s.io/kubebuilder/pkg/model/file"
25
23
)
@@ -29,9 +27,7 @@ var _ file.Template = &Kustomize{}
29
27
// Kustomize scaffolds the Kustomization file for the default overlay
30
28
type Kustomize struct {
31
29
file.TemplateMixin
32
-
33
- // Prefix to use for name prefix customization
34
- Prefix string
30
+ file.ProjectNameMixin
35
31
}
36
32
37
33
// SetTemplateDefaults implements input.Template
@@ -44,27 +40,18 @@ func (f *Kustomize) SetTemplateDefaults() error {
44
40
45
41
f .IfExistsAction = file .Error
46
42
47
- if f .Prefix == "" {
48
- // Use directory name as prefix.
49
- wd , err := os .Getwd ()
50
- if err != nil {
51
- return err
52
- }
53
- f .Prefix = strings .ToLower (filepath .Base (wd ))
54
- }
55
-
56
43
return nil
57
44
}
58
45
59
46
const kustomizeTemplate = `# Adds namespace to all resources.
60
- namespace: {{ .Prefix }}-system
47
+ namespace: {{ .ProjectName }}-system
61
48
62
49
# Value of this field is prepended to the
63
50
# names of all resources, e.g. a deployment named
64
51
# "wordpress" becomes "alices-wordpress".
65
52
# Note that it should also match with the prefix (text before '-') of the namespace
66
53
# field above.
67
- namePrefix: {{ .Prefix }}-
54
+ namePrefix: {{ .ProjectName }}-
68
55
69
56
# Labels to add to all resources and selectors.
70
57
#commonLabels:
Original file line number Diff line number Diff line change 1
1
domain: testproject.org
2
2
layout: go.kubebuilder.io/v3-alpha
3
- project-name : project-v3-addon
3
+ projectName : project-v3-addon
4
4
repo: sigs.k8s.io/kubebuilder/testdata/project-v3-addon
5
5
resources:
6
6
- group: crew
Original file line number Diff line number Diff line change 1
1
domain: testproject.org
2
2
layout: go.kubebuilder.io/v3-alpha
3
3
multigroup: true
4
- project-name : project-v3-multigroup
4
+ projectName : project-v3-multigroup
5
5
repo: sigs.k8s.io/kubebuilder/testdata/project-v3-multigroup
6
6
resources:
7
7
- group: crew
Original file line number Diff line number Diff line change 1
1
domain: testproject.org
2
2
layout: go.kubebuilder.io/v3-alpha
3
- project-name : project-v3
3
+ projectName : project-v3
4
4
repo: sigs.k8s.io/kubebuilder/testdata/project-v3
5
5
resources:
6
6
- group: crew
You can’t perform that action at this time.
0 commit comments