Merge pull request #111758 from SataQiu/fix-leaderelection-20220809

Make the validation logic about LeaderElectionConfiguration consistent between component-base and client-go
This commit is contained in:
Kubernetes Prow Robot 2022-08-23 19:00:14 -07:00 committed by GitHub
commit 0ac9784985
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 2 deletions

View File

@ -35,7 +35,7 @@ func BindLeaderElectionFlags(l *config.LeaderElectionConfiguration, fs *pflag.Fl
"election is enabled.") "election is enabled.")
fs.DurationVar(&l.RenewDeadline.Duration, "leader-elect-renew-deadline", l.RenewDeadline.Duration, ""+ fs.DurationVar(&l.RenewDeadline.Duration, "leader-elect-renew-deadline", l.RenewDeadline.Duration, ""+
"The interval between attempts by the acting master to renew a leadership slot "+ "The interval between attempts by the acting master to renew a leadership slot "+
"before it stops leading. This must be less than or equal to the lease duration. "+ "before it stops leading. This must be less than the lease duration. "+
"This is only applicable if leader election is enabled.") "This is only applicable if leader election is enabled.")
fs.DurationVar(&l.RetryPeriod.Duration, "leader-elect-retry-period", l.RetryPeriod.Duration, ""+ fs.DurationVar(&l.RetryPeriod.Duration, "leader-elect-retry-period", l.RetryPeriod.Duration, ""+
"The duration the clients should wait between attempting acquisition and renewal "+ "The duration the clients should wait between attempting acquisition and renewal "+

View File

@ -45,7 +45,7 @@ func ValidateLeaderElectionConfiguration(cc *config.LeaderElectionConfiguration,
if cc.RetryPeriod.Duration <= 0 { if cc.RetryPeriod.Duration <= 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("retryPeriod"), cc.RetryPeriod, "must be greater than zero")) allErrs = append(allErrs, field.Invalid(fldPath.Child("retryPeriod"), cc.RetryPeriod, "must be greater than zero"))
} }
if cc.LeaseDuration.Duration < cc.RenewDeadline.Duration { if cc.LeaseDuration.Duration <= cc.RenewDeadline.Duration {
allErrs = append(allErrs, field.Invalid(fldPath.Child("leaseDuration"), cc.RenewDeadline, "LeaseDuration must be greater than RenewDeadline")) allErrs = append(allErrs, field.Invalid(fldPath.Child("leaseDuration"), cc.RenewDeadline, "LeaseDuration must be greater than RenewDeadline"))
} }
if len(cc.ResourceLock) == 0 { if len(cc.ResourceLock) == 0 {

View File

@ -79,6 +79,9 @@ func TestValidateLeaderElectionConfiguration(t *testing.T) {
ResourceName: "name", ResourceName: "name",
} }
renewDeadlineEqualToLeaseDuration := validConfig.DeepCopy()
renewDeadlineEqualToLeaseDuration.RenewDeadline = metav1.Duration{Duration: 30 * time.Second}
renewDeadlineExceedsLeaseDuration := validConfig.DeepCopy() renewDeadlineExceedsLeaseDuration := validConfig.DeepCopy()
renewDeadlineExceedsLeaseDuration.RenewDeadline = metav1.Duration{Duration: 45 * time.Second} renewDeadlineExceedsLeaseDuration.RenewDeadline = metav1.Duration{Duration: 45 * time.Second}
@ -122,6 +125,10 @@ func TestValidateLeaderElectionConfiguration(t *testing.T) {
expectedToFail: false, expectedToFail: false,
config: LeaderElectButLeaderElectNotEnabled, config: LeaderElectButLeaderElectNotEnabled,
}, },
"bad-renew-deadline-equal-to-lease-duration": {
expectedToFail: true,
config: renewDeadlineEqualToLeaseDuration,
},
"bad-renew-deadline-exceeds-lease-duration": { "bad-renew-deadline-exceeds-lease-duration": {
expectedToFail: true, expectedToFail: true,
config: renewDeadlineExceedsLeaseDuration, config: renewDeadlineExceedsLeaseDuration,