Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ default a LeaderElectionID #1394

Merged
merged 1 commit into from
Feb 28, 2020
Merged

Conversation

mengqiy
Copy link
Member

@mengqiy mengqiy commented Feb 27, 2020

kubernetes-sigs/controller-runtime#446 removed the defaulting for LeaderElectionID.
It makes sense for controller-runtime. But kubebuilder should still be able to scaffold a project that work out-of-box.
So we will give users some instructions to update it in code comment.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 27, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mengqiy

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 27, 2020
@mengqiy
Copy link
Member Author

mengqiy commented Feb 27, 2020

This PR is a prerequisite for #1391.

@mengqiy mengqiy requested a review from droot February 27, 2020 00:07
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Feb 27, 2020
Copy link
Contributor

@Adirio Adirio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1 didn't support leader election, did it?

Minor comment

LeaderElection: enableLeaderElection,
// TODO(user): You MUST ensure LeaderElectionID is unique for your controller. Otherwise, 2 controllers using
// the same default LeaderElectionID in the same namespace will race and one of them will stop working silently.
LeaderElectionID: "replace-this-with-a-unique-id",
Copy link
Contributor

@Adirio Adirio Feb 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would create a unique string from domain and repo, adding some hashing too. Something like "{{ .Domain }}/{{ hash .Repo }}". WDYT?

hash could be something as simple as this example:

import (
	"fmt"
	"hash/fnv"
)

var hasher = fnv.New32a()

func hash(s string) string {
	hasher.Write([]byte(s))
	defer hasher.Reset()

	return fmt.Sprintf("%x", hasher.Sum(nil))
}

Copy link
Member Author

@mengqiy mengqiy Feb 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I need to change file.Template a bit to make it more flexible to pass the additional FuncMap around.

type Template interface {
	Builder
	// GetBody returns the template body
	GetBody() string
	// SetTemplateDefaults sets the default values for templates
	SetTemplateDefaults() error
	// GetFuncMap allow each template to provide its own FuncMap.
	GetFuncMap() template.FuncMap
}

EDIT: Not sure if this way is the best. Still trying to find a clean way to do it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, I would go for:

type CustomTemplate interface {
	Template
	GetFuncMap() template.FuncMap
}

This way only those that need it have to implement it.

But we could also implement it by having a HashedRepo field, set it in the SetTemplateDefaults, and then use it like "{{ .Domain }}/{{ .HashedRepo }}".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Adirio Any thoughts on current implementation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only NIT would be adding the Template to the UseCustomFuncMap

@mengqiy
Copy link
Member Author

mengqiy commented Feb 27, 2020

@Adirio v1 doesn't support leader election.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 27, 2020
@mengqiy
Copy link
Member Author

mengqiy commented Feb 27, 2020

PTAL

Copy link
Contributor

@Adirio Adirio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mengqiy
Copy link
Member Author

mengqiy commented Feb 27, 2020

/assign @estroz

Copy link
Contributor

@estroz estroz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 28, 2020
@k8s-ci-robot k8s-ci-robot merged commit 207685d into kubernetes-sigs:master Feb 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants