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

fix(app): Introduced flag to specify leader election behavious #2809

Merged
merged 1 commit into from
Apr 1, 2024

Conversation

CharlesQQ
Copy link
Contributor

resolve: #2798

@volcano-sh-bot
Copy link
Contributor

Welcome @CharlesQQ! It looks like this is your first PR to volcano-sh/volcano 🎉

@volcano-sh-bot volcano-sh-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 21, 2023
@CharlesQQ CharlesQQ changed the title fix(app): tntroduced flag to specify leader election behavious fix(app): Introduced flag to specify leader election behavious Apr 21, 2023
@lowang-bh
Copy link
Member

I think we should do some checks between those three parameters

@CharlesQQ
Copy link
Contributor Author

cc @wangyang0616

@wangyang0616
Copy link
Member

I agree with @lowang-bh is suggestion that the input parameters need to be verified.

@CharlesQQ
Copy link
Contributor Author

CharlesQQ commented May 4, 2023

@lowang-bh @wangyang0616
there have the checks for three parameter in leaderelection package, so no need to check again.

// NewLeaderElector creates a LeaderElector from a LeaderElectionConfig
func NewLeaderElector(lec LeaderElectionConfig) (*LeaderElector, error) {
if lec.LeaseDuration <= lec.RenewDeadline {
return nil, fmt.Errorf("leaseDuration must be greater than renewDeadline")
}
if lec.RenewDeadline <= time.Duration(JitterFactor*float64(lec.RetryPeriod)) {
return nil, fmt.Errorf("renewDeadline must be greater than retryPeriod*JitterFactor")
}
if lec.LeaseDuration < 1 {
return nil, fmt.Errorf("leaseDuration must be greater than zero")
}
if lec.RenewDeadline < 1 {
return nil, fmt.Errorf("renewDeadline must be greater than zero")
}
if lec.RetryPeriod < 1 {
return nil, fmt.Errorf("retryPeriod must be greater than zero")
}
if lec.Callbacks.OnStartedLeading == nil {
return nil, fmt.Errorf("OnStartedLeading callback must not be nil")
}
if lec.Callbacks.OnStoppedLeading == nil {
return nil, fmt.Errorf("OnStoppedLeading callback must not be nil")
}
if lec.Lock == nil {
return nil, fmt.Errorf("Lock must not be nil.")
}
le := LeaderElector{
config: lec,
clock: clock.RealClock{},
metrics: globalMetricsFactory.newLeaderMetrics(),
}
le.metrics.leaderOff(le.config.Name)
return &le, nil

@CharlesQQ
Copy link
Contributor Author

cc @wangyang0616

@@ -83,6 +93,19 @@ func (s *ServerOption) AddFlags(fs *pflag.FlagSet) {
fs.BoolVar(&s.EnableLeaderElection, "leader-elect", true, "Start a leader election client and gain leadership before "+
"executing the main loop. Enable this when running replicated vc-controller-manager for high availability; it is enabled by default")
fs.StringVar(&s.LockObjectNamespace, "lock-object-namespace", defaultLockObjectNamespace, "Define the namespace of the lock object; it is volcano-system by default")
fs.DurationVar(&s.LeaseDuration, "leader-elect-lease-duration", defaultElectionLeaseDuration, ""+
Copy link
Member

Choose a reason for hiding this comment

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

I'm thinking here mainly about the string here, whether to comply with the 120-character requirement, or to support legibility, is it easier to read if the two lines of text description information are closed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm thinking here mainly about the string here, whether to comply with the 120-character requirement, or to support legibility, is it easier to read if the two lines of text description information are closed?

It's same as kubernetes comments, sames that comply with the 120-character requirement
https://github.com/kubernetes/kubernetes/blob/ea30d100f6b13a47b4153362698b89256077776c/staging/src/k8s.io/component-base/config/options/leaderelectionconfig.go#L30C1-L42

Copy link

stale bot commented Dec 15, 2023

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Dec 15, 2023
@stale stale bot closed this Mar 17, 2024
@CharlesQQ
Copy link
Contributor Author

/reopen

@volcano-sh-bot
Copy link
Contributor

@CharlesQQ: Reopened this PR.

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@stale stale bot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Mar 25, 2024
@@ -47,6 +54,9 @@ type ServerOption struct {
CaCertData []byte
EnableLeaderElection bool
Copy link
Member

Choose a reason for hiding this comment

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

Leader election related parameters can be placed in an independent flagset, please refer to kubernets to make it more graceful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Monokaix updated! pls check again.

@CharlesQQ CharlesQQ requested a review from Monokaix March 25, 2024 04:10
@volcano-sh-bot volcano-sh-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 26, 2024
@@ -195,10 +192,10 @@ func (s *ServerOption) ParseCAFiles(decryptFunc DecryptFunc) error {
return nil
}

// Default new and registry a default one
Copy link
Member

Choose a reason for hiding this comment

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

Why is this commented?

Copy link
Contributor Author

@CharlesQQ CharlesQQ Mar 27, 2024

Choose a reason for hiding this comment

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

Why is this commented?

useless code, I will delete that.

pkg/util/util.go Outdated
@@ -32,3 +47,31 @@ func GenerateSchedulerName(schedulerNames []string) string {

return defaultSchedulerName
}

// BindLeaderElectionFlags binds the LeaderElectionConfiguration struct fields to a flagset
func BindLeaderElectionFlags(l *config.LeaderElectionConfiguration, fs *pflag.FlagSet) {
Copy link
Member

Choose a reason for hiding this comment

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

How about import k8s.io/component-base/config/options/leaderelectionconfig.go directly?

Copy link
Contributor Author

@CharlesQQ CharlesQQ Mar 27, 2024

Choose a reason for hiding this comment

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

How about import k8s.io/component-base/config/options/leaderelectionconfig.go directly?

en.., I want set default value in function BindLeaderElectionFlags; if not, must add a function to set LeaderElectionConfiguration default values.

Copy link
Member

Choose a reason for hiding this comment

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

But actually it's a copy from k8s: )

CertData []byte
KeyData []byte
CaCertData []byte
EnableLeaderElection bool
Copy link
Member

Choose a reason for hiding this comment

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

How about keeping the old one for better compatibility?

Copy link
Member

Choose a reason for hiding this comment

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

leader-elect flag is kept when using BindLeaderElectionFlags.

Copy link
Contributor Author

@CharlesQQ CharlesQQ Mar 27, 2024

Choose a reason for hiding this comment

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

How about keeping the old one for better compatibility?

Use leaderElection flagSet instead, it's a leaderElectionOption field.

Copy link
Member

Choose a reason for hiding this comment

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

But lock-object-namespace flag seems has been removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But lock-object-namespace flag seems has been removed.

use the leader-elect-resource-namespace flag instead

Copy link
Member

Choose a reason for hiding this comment

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

So plz also update params in helm : )

Copy link
Member

Choose a reason for hiding this comment

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

I think we should better keep the old flag and mark it deprecated first.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So plz also update params in helm I think we should better keep the old flag and mark it deprecated first.

sure!

Copy link
Member

Choose a reason for hiding this comment

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

I think we should better keep the old flag and mark it deprecated first.

I also means keeping the old one and marking it is deprecated in recent release.

return s
}
//// Default new and registry a default one
//func Default() *ServerOption {
Copy link
Member

Choose a reason for hiding this comment

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

Please keep it. It is used in other test packages.

Copy link
Contributor Author

@CharlesQQ CharlesQQ Mar 27, 2024

Choose a reason for hiding this comment

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

Please keep it. It is used in other test packages.

all right!

"github.com/spf13/pflag"
"k8s.io/component-base/config"
"os"
Copy link
Member

Choose a reason for hiding this comment

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

Plz sort imports.

Copy link
Contributor Author

@CharlesQQ CharlesQQ Mar 27, 2024

Choose a reason for hiding this comment

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

Plz sort imports.

done!

@CharlesQQ CharlesQQ force-pushed the leader-elect branch 4 times, most recently from 7f4fff8 to 0e650a5 Compare March 27, 2024 03:15
pkg/util/util.go Outdated
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/component-base/config"
"time"
Copy link
Member

Choose a reason for hiding this comment

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

sort imports.

@@ -147,7 +144,7 @@ func (s *ServerOption) AddFlags(fs *pflag.FlagSet) {

// CheckOptionOrDie check lock-object-namespace when LeaderElection is enabled.
func (s *ServerOption) CheckOptionOrDie() error {
if s.EnableLeaderElection && s.LockObjectNamespace == "" {
if s.LeaderElection.LeaderElect && s.LeaderElection.ResourceLock == "" {
return fmt.Errorf("lock-object-namespace must not be nil when LeaderElection is enabled")
Copy link
Member

Choose a reason for hiding this comment

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

Change the description too.

@@ -102,7 +100,7 @@ func (s *ServerOption) AddFlags(fs *pflag.FlagSet) {

// CheckOptionOrDie checks the LockObjectNamespace.
func (s *ServerOption) CheckOptionOrDie() error {
if s.EnableLeaderElection && s.LockObjectNamespace == "" {
if s.LeaderElection.LeaderElect && s.LeaderElection.ResourceLock == "" {
Copy link
Member

Choose a reason for hiding this comment

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

Here we can call ValidateLeaderElectionConfiguration to check all params.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All that is updated!

@CharlesQQ CharlesQQ force-pushed the leader-elect branch 2 times, most recently from e8bba62 to e095df9 Compare March 27, 2024 07:39

s.AddFlags(fs)
commonutil.LeaderElectionDefault(&s.LeaderElection)
componentbaseoptions.BindLeaderElectionFlags(&s.LeaderElection, fs)

Copy link
Member

Choose a reason for hiding this comment

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

Just mark LockObjectNamespace deprecated is not enough, we should set ResourceNamespace value to LockObjectNamespace when it's not empty: )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just mark LockObjectNamespace deprecated is not enough, we should set ResourceNamespace value to LockObjectNamespace when it's not empty: )

All right!

@CharlesQQ CharlesQQ force-pushed the leader-elect branch 3 times, most recently from f6892ba to 2913c74 Compare March 28, 2024 03:46
CertData []byte
KeyData []byte
CaCertData []byte
EnableLeaderElection bool
Copy link
Member

Choose a reason for hiding this comment

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

I think we should better keep the old flag and mark it deprecated first.

I also means keeping the old one and marking it is deprecated in recent release.

@@ -84,9 +89,7 @@ func (s *ServerOption) AddFlags(fs *pflag.FlagSet) {
"File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated "+
"after server cert).")
fs.StringVar(&s.KeyFile, "tls-private-key-file", s.KeyFile, "File containing the default x509 private key matching --tls-cert-file.")
fs.BoolVar(&s.EnableLeaderElection, "leader-elect", true, "Start a leader election client and gain leadership before "+
Copy link
Member

Choose a reason for hiding this comment

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

why this is removed?

// volcano scheduler will ignore pods with scheduler names other than specified with the option
fs.StringArrayVar(&s.SchedulerNames, "scheduler-name", []string{defaultSchedulerName}, "vc-scheduler will handle pods whose .spec.SchedulerName is same as scheduler-name")
fs.StringVar(&s.SchedulerConf, "scheduler-conf", "", "The absolute path of scheduler configuration file")
fs.DurationVar(&s.SchedulePeriod, "schedule-period", defaultSchedulerPeriod, "The period between each scheduling cycle")
fs.StringVar(&s.DefaultQueue, "default-queue", defaultQueue, "The default queue name of the job")
fs.BoolVar(&s.EnableLeaderElection, "leader-elect", true,
Copy link
Member

Choose a reason for hiding this comment

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

why this is removed, how about deprecated?

Copy link
Member

Choose a reason for hiding this comment

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

New flagset contains this flag, so it's not removed autually.

Signed-off-by: chang.qiangqiang <chang.qiangqiang@immomo.com>
@Monokaix
Copy link
Member

Monokaix commented Apr 1, 2024

/lgtm

@volcano-sh-bot volcano-sh-bot added the lgtm Indicates that a PR is ready to be merged. label Apr 1, 2024
Copy link
Member

@william-wang william-wang left a comment

Choose a reason for hiding this comment

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

/approve

@volcano-sh-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: william-wang

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

@volcano-sh-bot volcano-sh-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 1, 2024
@volcano-sh-bot volcano-sh-bot merged commit 1c8d95f into volcano-sh:master Apr 1, 2024
14 checks passed
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. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: add leader election options
6 participants