From 68db6a6922baeacef5d2bed9ef4261ce71700663 Mon Sep 17 00:00:00 2001 From: rambohe-ch Date: Fri, 15 Sep 2023 16:23:08 +0800 Subject: [PATCH] improve lease lock for yurt-manager component --- .../yurt-manager/templates/yurt-manager.yaml | 4 ++- charts/yurt-manager/values.yaml | 2 ++ cmd/yurt-manager/app/manager.go | 9 +++-- cmd/yurt-manager/app/options/generic.go | 33 +++++++++++-------- .../controller/apis/config/types.go | 18 +++++----- test/e2e/cmd/init/constants/constants.go | 1 - 6 files changed, 38 insertions(+), 29 deletions(-) diff --git a/charts/yurt-manager/templates/yurt-manager.yaml b/charts/yurt-manager/templates/yurt-manager.yaml index 7fe5908b353..225f9897e13 100644 --- a/charts/yurt-manager/templates/yurt-manager.yaml +++ b/charts/yurt-manager/templates/yurt-manager.yaml @@ -64,13 +64,15 @@ spec: key: node-role.kubernetes.io/control-plane containers: - args: - - --enable-leader-election=true - --metrics-addr=:{{ .Values.ports.metrics }} - --health-probe-addr=:{{ .Values.ports.healthProbe }} - --webhook-port={{ .Values.ports.webhook }} - --logtostderr=true - --v={{ .Values.log.level }} - --working-namespace={{ .Release.Namespace }} + {{- if .Values.leaderElectResourceName }} + - --leader-elect-resource-name={{ .Values.leaderElectResourceName }} + {{- end }} {{- if .Values.controllers }} - --controllers={{ .Values.controllers }} {{- end }} diff --git a/charts/yurt-manager/values.yaml b/charts/yurt-manager/values.yaml index a2c5152f028..a0901831067 100644 --- a/charts/yurt-manager/values.yaml +++ b/charts/yurt-manager/values.yaml @@ -26,6 +26,8 @@ controllers: "*" # format should be "foo,*" disableIndependentWebhooks: "" +leaderElectResourceName: "cloud-yurt-manager" + # resources of yurt-manager container resources: limits: diff --git a/cmd/yurt-manager/app/manager.go b/cmd/yurt-manager/app/manager.go index 9c11a76449a..461971a31db 100644 --- a/cmd/yurt-manager/app/manager.go +++ b/cmd/yurt-manager/app/manager.go @@ -26,7 +26,6 @@ import ( "k8s.io/apimachinery/pkg/util/wait" clientgoscheme "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" - "k8s.io/client-go/tools/leaderelection/resourcelock" cliflag "k8s.io/component-base/cli/flag" "k8s.io/component-base/cli/globalflag" "k8s.io/component-base/term" @@ -157,10 +156,10 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error { Scheme: scheme, MetricsBindAddress: c.ComponentConfig.Generic.MetricsAddr, HealthProbeBindAddress: c.ComponentConfig.Generic.HealthProbeAddr, - LeaderElection: c.ComponentConfig.Generic.EnableLeaderElection, - LeaderElectionID: YurtManager, - LeaderElectionNamespace: c.ComponentConfig.Generic.LeaderElectionNamespace, - LeaderElectionResourceLock: resourcelock.LeasesResourceLock, + LeaderElection: c.ComponentConfig.Generic.LeaderElection.LeaderElect, + LeaderElectionID: c.ComponentConfig.Generic.LeaderElection.ResourceName, + LeaderElectionNamespace: c.ComponentConfig.Generic.LeaderElection.ResourceNamespace, + LeaderElectionResourceLock: c.ComponentConfig.Generic.LeaderElection.ResourceLock, Port: util.GetWebHookPort(), Namespace: "", Logger: setupLog, diff --git a/cmd/yurt-manager/app/options/generic.go b/cmd/yurt-manager/app/options/generic.go index 783e74dc3ea..dfa862d3f05 100644 --- a/cmd/yurt-manager/app/options/generic.go +++ b/cmd/yurt-manager/app/options/generic.go @@ -22,6 +22,9 @@ import ( "github.com/spf13/pflag" "k8s.io/apimachinery/pkg/util/sets" + "k8s.io/client-go/tools/leaderelection/resourcelock" + componentbaseconfig "k8s.io/component-base/config" + "k8s.io/component-base/config/options" "github.com/openyurtio/openyurt/pkg/features" "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/apis/config" @@ -34,16 +37,20 @@ type GenericOptions struct { func NewGenericOptions() *GenericOptions { return &GenericOptions{ &config.GenericConfiguration{ - Version: false, - MetricsAddr: ":10271", - HealthProbeAddr: ":10272", - WebhookPort: 10273, - EnableLeaderElection: true, - LeaderElectionNamespace: "kube-system", - RestConfigQPS: 30, - RestConfigBurst: 50, - WorkingNamespace: "kube-system", - DisabledWebhooks: []string{}, + Version: false, + MetricsAddr: ":10271", + HealthProbeAddr: ":10272", + WebhookPort: 10273, + LeaderElection: componentbaseconfig.LeaderElectionConfiguration{ + LeaderElect: true, + ResourceLock: resourcelock.LeasesResourceLock, + ResourceName: "yurt-manager", + ResourceNamespace: "kube-system", + }, + RestConfigQPS: 30, + RestConfigBurst: 50, + WorkingNamespace: "kube-system", + DisabledWebhooks: []string{}, }, } } @@ -86,8 +93,8 @@ func (o *GenericOptions) ApplyTo(cfg *config.GenericConfiguration, controllerAli cfg.MetricsAddr = o.MetricsAddr cfg.HealthProbeAddr = o.HealthProbeAddr cfg.WebhookPort = o.WebhookPort - cfg.EnableLeaderElection = o.EnableLeaderElection - cfg.LeaderElectionNamespace = o.WorkingNamespace + cfg.LeaderElection = o.LeaderElection + cfg.LeaderElection.ResourceNamespace = o.WorkingNamespace cfg.RestConfigQPS = o.RestConfigQPS cfg.RestConfigBurst = o.RestConfigBurst cfg.WorkingNamespace = o.WorkingNamespace @@ -119,7 +126,7 @@ func (o *GenericOptions) AddFlags(fs *pflag.FlagSet, allControllers, disabledByD fs.StringVar(&o.MetricsAddr, "metrics-addr", o.MetricsAddr, "The address the metric endpoint binds to.") fs.StringVar(&o.HealthProbeAddr, "health-probe-addr", o.HealthProbeAddr, "The address the healthz/readyz endpoint binds to.") fs.IntVar(&o.WebhookPort, "webhook-port", o.WebhookPort, "The port on which to serve HTTPS for webhook server. It can't be switched off with 0") - fs.BoolVar(&o.EnableLeaderElection, "enable-leader-election", o.EnableLeaderElection, "Whether you need to enable leader election.") + options.BindLeaderElectionFlags(&o.LeaderElection, fs) fs.IntVar(&o.RestConfigQPS, "rest-config-qps", o.RestConfigQPS, "rest-config-qps.") fs.IntVar(&o.RestConfigBurst, "rest-config-burst", o.RestConfigBurst, "rest-config-burst.") fs.StringVar(&o.WorkingNamespace, "working-namespace", o.WorkingNamespace, "The namespace where the yurt-manager is working.") diff --git a/pkg/yurtmanager/controller/apis/config/types.go b/pkg/yurtmanager/controller/apis/config/types.go index aaeb974bf98..d5df32f337c 100644 --- a/pkg/yurtmanager/controller/apis/config/types.go +++ b/pkg/yurtmanager/controller/apis/config/types.go @@ -18,6 +18,7 @@ package config import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + componentbaseconfig "k8s.io/component-base/config" nodepoolconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodepool/config" platformadminconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/platformadmin/config" @@ -55,15 +56,14 @@ type YurtManagerConfiguration struct { } type GenericConfiguration struct { - Version bool - MetricsAddr string - HealthProbeAddr string - WebhookPort int - EnableLeaderElection bool - LeaderElectionNamespace string - RestConfigQPS int - RestConfigBurst int - WorkingNamespace string + Version bool + MetricsAddr string + HealthProbeAddr string + WebhookPort int + LeaderElection componentbaseconfig.LeaderElectionConfiguration + RestConfigQPS int + RestConfigBurst int + WorkingNamespace string // Controllers is the list of controllers to enable or disable // '*' means "all enabled by default controllers" // 'foo' means "enable 'foo'" diff --git a/test/e2e/cmd/init/constants/constants.go b/test/e2e/cmd/init/constants/constants.go index 3e3504ec57b..c0aa56761e8 100644 --- a/test/e2e/cmd/init/constants/constants.go +++ b/test/e2e/cmd/init/constants/constants.go @@ -96,7 +96,6 @@ spec: hostNetwork: true containers: - args: - - --enable-leader-election - --metrics-addr=:10271 - --health-probe-addr=:10272 - --webhook-port=10273