Skip to content

Commit

Permalink
Merge pull request #1812 from christopherhein/chore/refactor-hash-tem…
Browse files Browse the repository at this point in the history
…p-func

🌱  refactoring hash function to global template funcs
  • Loading branch information
k8s-ci-robot committed Nov 12, 2020
2 parents 271d886 + a0bde3c commit 097d34e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 45 deletions.
16 changes: 14 additions & 2 deletions pkg/model/file/funcmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,26 @@ limitations under the License.
package file

import (
"fmt"
"hash/fnv"
"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,
"title": strings.Title,
"lower": strings.ToLower,
"hashFNV": hashFNV,
}
}

// hashFNV will generate a random string useful for generating a unique string
func hashFNV(s string) (string, error) {
hasher := fnv.New32a()
if _, err := hasher.Write([]byte(s)); err != nil {
return "", err
}
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}
26 changes: 4 additions & 22 deletions pkg/plugin/v2/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ package templates

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

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

const defaultMainPath = "main.go"

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

// Main scaffolds a file that defines the controller manager entry point
type Main struct {
Expand All @@ -53,21 +50,6 @@ func (f *Main) SetTemplateDefaults() error {
return nil
}

func hash(s string) (string, error) {
hasher := fnv.New32a()
if _, err := hasher.Write([]byte(s)); err != nil {
return "", err
}
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 @@ -241,14 +223,14 @@ func main() {
"Enabling this will ensure there is only one active controller manager.")
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "{{ hash .Repo }}.{{ .Domain }}",
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "{{ hashFNV .Repo }}.{{ .Domain }}",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down
24 changes: 3 additions & 21 deletions pkg/plugin/v3/scaffolds/internal/templates/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ package templates

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

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

const defaultMainPath = "main.go"

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

// Main scaffolds a file that defines the controller manager entry point
type Main struct {
Expand All @@ -53,21 +50,6 @@ func (f *Main) SetTemplateDefaults() error {
return nil
}

func hash(s string) (string, error) {
hasher := fnv.New32a()
if _, err := hasher.Write([]byte(s)); err != nil {
return "", err
}
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 @@ -249,9 +231,9 @@ func main() {
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "{{ hash .Repo }}.{{ .Domain }}",
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "{{ hashFNV .Repo }}.{{ .Domain }}",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down

0 comments on commit 097d34e

Please sign in to comment.