From daa7040195b6e0f851090739ea2be1a4735667ca Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Thu, 14 Jan 2016 11:17:00 -0800 Subject: [PATCH] move leader election configuration into component configuration Signed-off-by: Mike Danese --- pkg/apis/componentconfig/types.go | 25 +++++++++++++++++ pkg/client/leaderelection/leaderelection.go | 28 +++++++------------ .../cmd/kube-scheduler/app/options/options.go | 7 +++-- plugin/cmd/kube-scheduler/app/server.go | 6 ++-- 4 files changed, 42 insertions(+), 24 deletions(-) diff --git a/pkg/apis/componentconfig/types.go b/pkg/apis/componentconfig/types.go index 818c378c9db..8c40d1bf44c 100644 --- a/pkg/apis/componentconfig/types.go +++ b/pkg/apis/componentconfig/types.go @@ -62,3 +62,28 @@ const ( ProxyModeUserspace ProxyMode = "userspace" ProxyModeIPTables ProxyMode = "iptables" ) + +// LeaderElectionConfiguration defines the configuration of leader election +// clients for components that can run with leader election enabled. +type LeaderElectionConfiguration struct { + // leaderElect enables a leader election client to gain leadership + // before executing the main loop. Enable this when running replicated + // components for high availability. + LeaderElect bool `json:"leaderElect"` + // leaseDuration is 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. + LeaseDuration unversioned.Duration `json:"leaseDuration"` + // renewDeadline is 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. + RenewDeadline unversioned.Duration `json:"renewDeadline"` + // retryPeriod is the duration the clients should wait between attempting + // acquisition and renewal of a leadership. This is only applicable if + // leader election is enabled. + RetryPeriod unversioned.Duration `json:"retryPeriod"` +} diff --git a/pkg/client/leaderelection/leaderelection.go b/pkg/client/leaderelection/leaderelection.go index 8b2eb098fe9..7ff424ba7f6 100644 --- a/pkg/client/leaderelection/leaderelection.go +++ b/pkg/client/leaderelection/leaderelection.go @@ -57,6 +57,7 @@ import ( "k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api/errors" "k8s.io/kubernetes/pkg/api/unversioned" + "k8s.io/kubernetes/pkg/apis/componentconfig" "k8s.io/kubernetes/pkg/client/record" client "k8s.io/kubernetes/pkg/client/unversioned" "k8s.io/kubernetes/pkg/util" @@ -331,41 +332,32 @@ func (l *LeaderElector) maybeReportTransition() { } } -func DefaultLeaderElectionCLIConfig() LeaderElectionCLIConfig { - return LeaderElectionCLIConfig{ +func DefaultLeaderElectionConfiguration() componentconfig.LeaderElectionConfiguration { + return componentconfig.LeaderElectionConfiguration{ LeaderElect: false, - LeaseDuration: DefaultLeaseDuration, - RenewDeadline: DefaultRenewDeadline, - RetryPeriod: DefaultRetryPeriod, + LeaseDuration: unversioned.Duration{DefaultLeaseDuration}, + RenewDeadline: unversioned.Duration{DefaultRenewDeadline}, + RetryPeriod: unversioned.Duration{DefaultRetryPeriod}, } } -// LeaderElectionCLIConfig is useful for embedding into component configuration objects -// to maintain consistent command line flags. -type LeaderElectionCLIConfig struct { - LeaderElect bool - LeaseDuration time.Duration - RenewDeadline time.Duration - RetryPeriod time.Duration -} - // BindFlags binds the common LeaderElectionCLIConfig flags to a flagset -func (l *LeaderElectionCLIConfig) BindFlags(fs *pflag.FlagSet) { +func BindFlags(l *componentconfig.LeaderElectionConfiguration, fs *pflag.FlagSet) { fs.BoolVar(&l.LeaderElect, "leader-elect", l.LeaderElect, ""+ "Start a leader election client and gain leadership before "+ "executing scheduler loop. Enable this when running replicated "+ "schedulers.") - fs.DurationVar(&l.LeaseDuration, "leader-elect-lease-duration", l.LeaseDuration, ""+ + fs.DurationVar(&l.LeaseDuration.Duration, "leader-elect-lease-duration", l.LeaseDuration.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(&l.RenewDeadline, "leader-elect-renew-deadline", l.RenewDeadline, ""+ + 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 "+ "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(&l.RetryPeriod, "leader-elect-retry-period", l.RetryPeriod, ""+ + fs.DurationVar(&l.RetryPeriod.Duration, "leader-elect-retry-period", l.RetryPeriod.Duration, ""+ "The duration the clients should wait between attempting acquisition and renewal "+ "of a leadership. This is only applicable if leader election is enabled.") } diff --git a/plugin/cmd/kube-scheduler/app/options/options.go b/plugin/cmd/kube-scheduler/app/options/options.go index 36ca7b589d5..d1bfe396ce3 100644 --- a/plugin/cmd/kube-scheduler/app/options/options.go +++ b/plugin/cmd/kube-scheduler/app/options/options.go @@ -21,6 +21,7 @@ import ( "net" "k8s.io/kubernetes/pkg/api" + "k8s.io/kubernetes/pkg/apis/componentconfig" "k8s.io/kubernetes/pkg/client/leaderelection" "k8s.io/kubernetes/pkg/master/ports" "k8s.io/kubernetes/plugin/pkg/scheduler/factory" @@ -42,7 +43,7 @@ type SchedulerServer struct { KubeAPIQPS float32 KubeAPIBurst int SchedulerName string - LeaderElection leaderelection.LeaderElectionCLIConfig + LeaderElection componentconfig.LeaderElectionConfiguration } // NewSchedulerServer creates a new SchedulerServer with default parameters @@ -56,7 +57,7 @@ func NewSchedulerServer() *SchedulerServer { KubeAPIQPS: 50.0, KubeAPIBurst: 100, SchedulerName: api.DefaultSchedulerName, - LeaderElection: leaderelection.DefaultLeaderElectionCLIConfig(), + LeaderElection: leaderelection.DefaultLeaderElectionConfiguration(), } return &s } @@ -75,5 +76,5 @@ func (s *SchedulerServer) AddFlags(fs *pflag.FlagSet) { fs.Float32Var(&s.KubeAPIQPS, "kube-api-qps", s.KubeAPIQPS, "QPS to use while talking with kubernetes apiserver") fs.IntVar(&s.KubeAPIBurst, "kube-api-burst", s.KubeAPIBurst, "Burst to use while talking with kubernetes apiserver") fs.StringVar(&s.SchedulerName, "scheduler-name", s.SchedulerName, "Name of the scheduler, used to select which pods will be processed by this scheduler, based on pod's annotation with key 'scheduler.alpha.kubernetes.io/name'") - s.LeaderElection.BindFlags(fs) + leaderelection.BindFlags(&s.LeaderElection, fs) } diff --git a/plugin/cmd/kube-scheduler/app/server.go b/plugin/cmd/kube-scheduler/app/server.go index 610098a5e50..0ce03d8e84a 100644 --- a/plugin/cmd/kube-scheduler/app/server.go +++ b/plugin/cmd/kube-scheduler/app/server.go @@ -136,9 +136,9 @@ func Run(s *options.SchedulerServer) error { Client: kubeClient, Identity: id, EventRecorder: config.Recorder, - LeaseDuration: s.LeaderElection.LeaseDuration, - RenewDeadline: s.LeaderElection.RenewDeadline, - RetryPeriod: s.LeaderElection.RetryPeriod, + LeaseDuration: s.LeaderElection.LeaseDuration.Duration, + RenewDeadline: s.LeaderElection.RenewDeadline.Duration, + RetryPeriod: s.LeaderElection.RetryPeriod.Duration, Callbacks: leaderelection.LeaderCallbacks{ OnStartedLeading: run, OnStoppedLeading: func() {