Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions pkg/model/file/funcmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package file

import (
"strings"
"text/template"
)

// DefaultFuncMap returns the default template.FuncMap for rendering the template.
func DefaultFuncMap() template.FuncMap {
return template.FuncMap{
"title": strings.Title,
"lower": strings.ToLower,
}
}
8 changes: 8 additions & 0 deletions pkg/model/file/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package file

import (
"text/template"

"sigs.k8s.io/kubebuilder/pkg/model/resource"
)

Expand Down Expand Up @@ -82,3 +84,9 @@ type HasResource interface {
// InjectResource sets the template resource
InjectResource(*resource.Resource)
}

// UseCustomFuncMap allows a template to use a custom template.FuncMap instead of the default FuncMap.
type UseCustomFuncMap interface {
// GetFuncMap returns a custom FuncMap.
GetFuncMap() template.FuncMap
}
10 changes: 6 additions & 4 deletions pkg/scaffold/internal/machinery/scaffold.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,12 @@ func doTemplate(t file.Template) ([]byte, error) {

// newTemplate a new template with common functions
func newTemplate(t file.Template) *template.Template {
return template.New(fmt.Sprintf("%T", t)).Funcs(template.FuncMap{
"title": strings.Title,
"lower": strings.ToLower,
})
fm := file.DefaultFuncMap()
useFM, ok := t.(file.UseCustomFuncMap)
if ok {
fm = useFM.GetFuncMap()
}
return template.New(fmt.Sprintf("%T", t)).Funcs(fm)
}

// updateFileModel updates a single file
Expand Down
21 changes: 20 additions & 1 deletion pkg/scaffold/internal/templates/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@ package v2

import (
"fmt"
"hash/fnv"
"path/filepath"
"text/template"

"sigs.k8s.io/kubebuilder/pkg/model/file"
)

const defaultMainPath = "main.go"

var _ file.Template = &Main{}
var _ file.UseCustomFuncMap = &Main{}

// Main scaffolds the controller manager entry point
type Main struct {
file.TemplateMixin
file.BoilerplateMixin
file.DomainMixin
file.RepositoryMixin
}

// SetTemplateDefaults implements file.Template
Expand All @@ -48,6 +53,19 @@ func (f *Main) SetTemplateDefaults() error {
return nil
}

func hash(s string) (string, error) {
hasher := fnv.New32a()
hasher.Write([]byte(s)) // nolint:errcheck
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}

// GetFuncMap implements file.UseCustomFuncMap
func (f *Main) GetFuncMap() template.FuncMap {
fm := file.DefaultFuncMap()
fm["hash"] = hash
return fm
}

var _ file.Inserter = &MainUpdater{}

// MainUpdater updates main.go to run Controllers
Expand Down Expand Up @@ -220,8 +238,9 @@ func main() {
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "{{ hash .Repo }}.{{ .Domain }}",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down
3 changes: 2 additions & 1 deletion testdata/project-v2-multigroup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ func main() {
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "e9b53b87.testproject.org",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down
3 changes: 2 additions & 1 deletion testdata/project-v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ func main() {
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "dc1d9fac.testproject.org",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down