Skip to content

Commit e3e6626

Browse files
author
Mengqi Yu
committed
✨ default leader election ID using hash of repo + domain name
1 parent f0c25d0 commit e3e6626

File tree

6 files changed

+68
-7
lines changed

6 files changed

+68
-7
lines changed

pkg/model/file/funcmap.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package file
18+
19+
import (
20+
"strings"
21+
"text/template"
22+
)
23+
24+
// DefaultFuncMap returns the default template.FuncMap for rendering the template.
25+
func DefaultFuncMap() template.FuncMap {
26+
return template.FuncMap{
27+
"title": strings.Title,
28+
"lower": strings.ToLower,
29+
}
30+
}

pkg/model/file/interfaces.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package file
1818

1919
import (
20+
"text/template"
21+
2022
"sigs.k8s.io/kubebuilder/pkg/model/resource"
2123
)
2224

@@ -82,3 +84,9 @@ type HasResource interface {
8284
// InjectResource sets the template resource
8385
InjectResource(*resource.Resource)
8486
}
87+
88+
// UseCustomFuncMap allows a template to use a custom template.FuncMap instead of the default FuncMap.
89+
type UseCustomFuncMap interface {
90+
// GetFuncMap returns a custom FuncMap.
91+
GetFuncMap() template.FuncMap
92+
}

pkg/scaffold/internal/machinery/scaffold.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,12 @@ func doTemplate(t file.Template) ([]byte, error) {
179179

180180
// newTemplate a new template with common functions
181181
func newTemplate(t file.Template) *template.Template {
182-
return template.New(fmt.Sprintf("%T", t)).Funcs(template.FuncMap{
183-
"title": strings.Title,
184-
"lower": strings.ToLower,
185-
})
182+
fm := file.DefaultFuncMap()
183+
useFM, ok := t.(file.UseCustomFuncMap)
184+
if ok {
185+
fm = useFM.GetFuncMap()
186+
}
187+
return template.New(fmt.Sprintf("%T", t)).Funcs(fm)
186188
}
187189

188190
// updateFileModel updates a single file

pkg/scaffold/internal/templates/v2/main.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,24 @@ package v2
1818

1919
import (
2020
"fmt"
21+
"hash/fnv"
2122
"path/filepath"
23+
"text/template"
2224

2325
"sigs.k8s.io/kubebuilder/pkg/model/file"
2426
)
2527

2628
const defaultMainPath = "main.go"
2729

2830
var _ file.Template = &Main{}
31+
var _ file.UseCustomFuncMap = &Main{}
2932

3033
// Main scaffolds the controller manager entry point
3134
type Main struct {
3235
file.TemplateMixin
3336
file.BoilerplateMixin
37+
file.DomainMixin
38+
file.RepositoryMixin
3439
}
3540

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

56+
func hash(s string) (string, error) {
57+
hasher := fnv.New32a()
58+
hasher.Write([]byte(s)) // nolint:errcheck
59+
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
60+
}
61+
62+
// GetFuncMap implements file.UseCustomFuncMap
63+
func (f *Main) GetFuncMap() template.FuncMap {
64+
fm := file.DefaultFuncMap()
65+
fm["hash"] = hash
66+
return fm
67+
}
68+
5169
var _ file.Inserter = &MainUpdater{}
5270

5371
// MainUpdater updates main.go to run Controllers
@@ -220,8 +238,9 @@ func main() {
220238
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
221239
Scheme: scheme,
222240
MetricsBindAddress: metricsAddr,
223-
LeaderElection: enableLeaderElection,
224241
Port: 9443,
242+
LeaderElection: enableLeaderElection,
243+
LeaderElectionID: "{{ hash .Repo }}.{{ .Domain }}",
225244
})
226245
if err != nil {
227246
setupLog.Error(err, "unable to start manager")

testdata/project-v2-multigroup/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ func main() {
7272
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
7373
Scheme: scheme,
7474
MetricsBindAddress: metricsAddr,
75-
LeaderElection: enableLeaderElection,
7675
Port: 9443,
76+
LeaderElection: enableLeaderElection,
77+
LeaderElectionID: "e9b53b87.testproject.org",
7778
})
7879
if err != nil {
7980
setupLog.Error(err, "unable to start manager")

testdata/project-v2/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ func main() {
5757
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
5858
Scheme: scheme,
5959
MetricsBindAddress: metricsAddr,
60-
LeaderElection: enableLeaderElection,
6160
Port: 9443,
61+
LeaderElection: enableLeaderElection,
62+
LeaderElectionID: "dc1d9fac.testproject.org",
6263
})
6364
if err != nil {
6465
setupLog.Error(err, "unable to start manager")

0 commit comments

Comments
 (0)