From 0fc2dcc54cf4b274c6885b14ca9122d3ec88244f Mon Sep 17 00:00:00 2001 From: "chang.qiangqiang" Date: Sat, 25 Jun 2022 19:36:04 +0800 Subject: [PATCH] feat: add leader election options Signed-off-by: chang.qiangqiang --- cmd/agent/app/agent.go | 3 +++ cmd/agent/app/options/options.go | 19 +++++++++++++++++++ .../app/controllermanager.go | 3 +++ cmd/controller-manager/app/options/options.go | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/cmd/agent/app/agent.go b/cmd/agent/app/agent.go index d34447e185c4..33ca29529dfe 100644 --- a/cmd/agent/app/agent.go +++ b/cmd/agent/app/agent.go @@ -147,6 +147,9 @@ func run(ctx context.Context, karmadaConfig karmadactl.KarmadaConfig, opts *opti LeaderElectionID: fmt.Sprintf("karmada-agent-%s", opts.ClusterName), LeaderElectionNamespace: opts.LeaderElection.ResourceNamespace, LeaderElectionResourceLock: opts.LeaderElection.ResourceLock, + LeaseDuration: &opts.LeaderElection.LeaseDuration.Duration, + RenewDeadline: &opts.LeaderElection.RenewDeadline.Duration, + RetryPeriod: &opts.LeaderElection.RetryPeriod.Duration, HealthProbeBindAddress: net.JoinHostPort(opts.BindAddress, strconv.Itoa(opts.SecurePort)), LivenessEndpointName: "/healthz", MetricsBindAddress: opts.MetricsBindAddress, diff --git a/cmd/agent/app/options/options.go b/cmd/agent/app/options/options.go index 3e7a62ce9b23..d7320c0f94dd 100644 --- a/cmd/agent/app/options/options.go +++ b/cmd/agent/app/options/options.go @@ -21,6 +21,12 @@ const ( defaultPort = 10357 ) +var ( + defaultElectionLeaseDuration = metav1.Duration{Duration: 15 * time.Second} + defaultElectionRenewDeadline = metav1.Duration{Duration: 10 * time.Second} + defaultElectionRetryPeriod = metav1.Duration{Duration: 2 * time.Second} +) + // Options contains everything necessary to create and run controller-manager. type Options struct { // Controllers contains all controller names. @@ -111,6 +117,19 @@ func (o *Options) AddFlags(fs *pflag.FlagSet, allControllers []string) { "The secure port on which to serve HTTPS.") fs.BoolVar(&o.LeaderElection.LeaderElect, "leader-elect", true, "Start a leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability.") fs.StringVar(&o.LeaderElection.ResourceNamespace, "leader-elect-resource-namespace", util.NamespaceKarmadaSystem, "The namespace of resource object that is used for locking during leader election.") + fs.DurationVar(&o.LeaderElection.LeaseDuration.Duration, "leader-elect-lease-duration", defaultElectionLeaseDuration.Duration, ""+ + "The duration that non-leader candidates will wait after observing a leadership "+ + "renewal until attempting to acquire leadership of a led but unrenewed leader "+ + "slot. This is effectively the maximum duration that a leader can be stopped "+ + "before it is replaced by another candidate. This is only applicable if leader "+ + "election is enabled.") + fs.DurationVar(&o.LeaderElection.RenewDeadline.Duration, "leader-elect-renew-deadline", defaultElectionRenewDeadline.Duration, ""+ + "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. "+ + "This is only applicable if leader election is enabled.") + fs.DurationVar(&o.LeaderElection.RetryPeriod.Duration, "leader-elect-retry-period", defaultElectionRetryPeriod.Duration, ""+ + "The duration the clients should wait between attempting acquisition and renewal "+ + "of a leadership. This is only applicable if leader election is enabled.") fs.StringVar(&o.KarmadaKubeConfig, "karmada-kubeconfig", o.KarmadaKubeConfig, "Path to karmada control plane kubeconfig file.") fs.StringVar(&o.KarmadaContext, "karmada-context", "", "Name of the cluster context in karmada control plane kubeconfig file.") fs.StringVar(&o.ClusterName, "cluster-name", o.ClusterName, "Name of member cluster that the agent serves for.") diff --git a/cmd/controller-manager/app/controllermanager.go b/cmd/controller-manager/app/controllermanager.go index 323722982afd..16b53fa040a8 100644 --- a/cmd/controller-manager/app/controllermanager.go +++ b/cmd/controller-manager/app/controllermanager.go @@ -109,6 +109,9 @@ func Run(ctx context.Context, opts *options.Options) error { LeaderElection: opts.LeaderElection.LeaderElect, LeaderElectionID: opts.LeaderElection.ResourceName, LeaderElectionNamespace: opts.LeaderElection.ResourceNamespace, + LeaseDuration: &opts.LeaderElection.LeaseDuration.Duration, + RenewDeadline: &opts.LeaderElection.RenewDeadline.Duration, + RetryPeriod: &opts.LeaderElection.RetryPeriod.Duration, LeaderElectionResourceLock: opts.LeaderElection.ResourceLock, HealthProbeBindAddress: net.JoinHostPort(opts.BindAddress, strconv.Itoa(opts.SecurePort)), LivenessEndpointName: "/healthz", diff --git a/cmd/controller-manager/app/options/options.go b/cmd/controller-manager/app/options/options.go index f6e1fa40772e..2b0e6ef7560a 100644 --- a/cmd/controller-manager/app/options/options.go +++ b/cmd/controller-manager/app/options/options.go @@ -20,6 +20,12 @@ const ( defaultPort = 10357 ) +var ( + defaultElectionLeaseDuration = metav1.Duration{Duration: 15 * time.Second} + defaultElectionRenewDeadline = metav1.Duration{Duration: 10 * time.Second} + defaultElectionRetryPeriod = metav1.Duration{Duration: 2 * time.Second} +) + // Options contains everything necessary to create and run controller-manager. type Options struct { // Controllers is the list of controllers to enable or disable @@ -134,6 +140,19 @@ func (o *Options) AddFlags(flags *pflag.FlagSet, allControllers, disabledByDefau "Specifies how often karmada-controller-manager posts cluster status to karmada-apiserver.") flags.BoolVar(&o.LeaderElection.LeaderElect, "leader-elect", true, "Start a leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability.") flags.StringVar(&o.LeaderElection.ResourceNamespace, "leader-elect-resource-namespace", util.NamespaceKarmadaSystem, "The namespace of resource object that is used for locking during leader election.") + flags.DurationVar(&o.LeaderElection.LeaseDuration.Duration, "leader-elect-lease-duration", defaultElectionLeaseDuration.Duration, ""+ + "The duration that non-leader candidates will wait after observing a leadership "+ + "renewal until attempting to acquire leadership of a led but unrenewed leader "+ + "slot. This is effectively the maximum duration that a leader can be stopped "+ + "before it is replaced by another candidate. This is only applicable if leader "+ + "election is enabled.") + flags.DurationVar(&o.LeaderElection.RenewDeadline.Duration, "leader-elect-renew-deadline", defaultElectionRenewDeadline.Duration, ""+ + "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. "+ + "This is only applicable if leader election is enabled.") + flags.DurationVar(&o.LeaderElection.RetryPeriod.Duration, "leader-elect-retry-period", defaultElectionRetryPeriod.Duration, ""+ + "The duration the clients should wait between attempting acquisition and renewal "+ + "of a leadership. This is only applicable if leader election is enabled.") flags.DurationVar(&o.ClusterLeaseDuration.Duration, "cluster-lease-duration", 40*time.Second, "Specifies the expiration period of a cluster lease.") flags.Float64Var(&o.ClusterLeaseRenewIntervalFraction, "cluster-lease-renew-interval-fraction", 0.25,