From ee6e28b5cfac62be8cb89962c8d870186b674470 Mon Sep 17 00:00:00 2001 From: chenyixiang <283158956@qq.com> Date: Sun, 28 Jul 2019 16:30:45 +0800 Subject: [PATCH] add options for name and namespace of leaderelection object Change-Id: Iaa62f5f1c3b24a4cc567a840707d9eba7cf901b8 --- .../app/controllermanager.go | 4 ++-- .../app/options/options.go | 3 +++ .../app/options/options_test.go | 24 +++++++++++-------- .../app/controllermanager.go | 5 ++-- .../app/options/options.go | 2 ++ .../app/options/options_test.go | 12 ++++++---- pkg/client/leaderelectionconfig/config.go | 6 +++++ .../src/k8s.io/component-base/config/types.go | 6 +++++ .../component-base/config/v1alpha1/types.go | 6 +++++ .../v1alpha1/zz_generated.conversion.go | 4 ++++ 10 files changed, 53 insertions(+), 19 deletions(-) diff --git a/cmd/cloud-controller-manager/app/controllermanager.go b/cmd/cloud-controller-manager/app/controllermanager.go index 26b37ab9266..726e06b63bc 100644 --- a/cmd/cloud-controller-manager/app/controllermanager.go +++ b/cmd/cloud-controller-manager/app/controllermanager.go @@ -186,8 +186,8 @@ func Run(c *cloudcontrollerconfig.CompletedConfig, stopCh <-chan struct{}) error // Lock required for leader election rl, err := resourcelock.New(c.ComponentConfig.Generic.LeaderElection.ResourceLock, - "kube-system", - "cloud-controller-manager", + c.ComponentConfig.Generic.LeaderElection.ResourceNamespace, + c.ComponentConfig.Generic.LeaderElection.ResourceName, c.LeaderElectionClient.CoreV1(), c.LeaderElectionClient.CoordinationV1(), resourcelock.ResourceLockConfig{ diff --git a/cmd/cloud-controller-manager/app/options/options.go b/cmd/cloud-controller-manager/app/options/options.go index 205e5da99b3..d64f0b02f51 100644 --- a/cmd/cloud-controller-manager/app/options/options.go +++ b/cmd/cloud-controller-manager/app/options/options.go @@ -107,6 +107,9 @@ func NewCloudControllerManagerOptions() (*CloudControllerManagerOptions, error) s.SecureServing.ServerCert.PairName = "cloud-controller-manager" s.SecureServing.BindPort = ports.CloudControllerManagerPort + s.Generic.LeaderElection.ResourceName = "cloud-controller-manager" + s.Generic.LeaderElection.ResourceNamespace = "kube-system" + return &s, nil } diff --git a/cmd/cloud-controller-manager/app/options/options_test.go b/cmd/cloud-controller-manager/app/options/options_test.go index 9505238b025..80eab7bafab 100644 --- a/cmd/cloud-controller-manager/app/options/options_test.go +++ b/cmd/cloud-controller-manager/app/options/options_test.go @@ -49,11 +49,13 @@ func TestDefaultFlags(t *testing.T) { }, ControllerStartInterval: metav1.Duration{Duration: 0}, LeaderElection: componentbaseconfig.LeaderElectionConfiguration{ - ResourceLock: "endpoints", - LeaderElect: true, - LeaseDuration: metav1.Duration{Duration: 15 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 10 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 2 * time.Second}, + ResourceLock: "endpoints", + LeaderElect: true, + LeaseDuration: metav1.Duration{Duration: 15 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 10 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 2 * time.Second}, + ResourceName: "cloud-controller-manager", + ResourceNamespace: "kube-system", }, Controllers: []string{"*"}, }, @@ -178,11 +180,13 @@ func TestAddFlags(t *testing.T) { }, ControllerStartInterval: metav1.Duration{Duration: 2 * time.Minute}, LeaderElection: componentbaseconfig.LeaderElectionConfiguration{ - ResourceLock: "configmap", - LeaderElect: false, - LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceLock: "configmap", + LeaderElect: false, + LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceName: "cloud-controller-manager", + ResourceNamespace: "kube-system", }, Controllers: []string{"foo", "bar"}, }, diff --git a/cmd/kube-controller-manager/app/controllermanager.go b/cmd/kube-controller-manager/app/controllermanager.go index 6fd08cfdc06..5a4da72fa6c 100644 --- a/cmd/kube-controller-manager/app/controllermanager.go +++ b/cmd/kube-controller-manager/app/controllermanager.go @@ -257,9 +257,10 @@ func Run(c *config.CompletedConfig, stopCh <-chan struct{}) error { // add a uniquifier so that two processes on the same host don't accidentally both become active id = id + "_" + string(uuid.NewUUID()) + rl, err := resourcelock.New(c.ComponentConfig.Generic.LeaderElection.ResourceLock, - "kube-system", - "kube-controller-manager", + c.ComponentConfig.Generic.LeaderElection.ResourceNamespace, + c.ComponentConfig.Generic.LeaderElection.ResourceName, c.LeaderElectionClient.CoreV1(), c.LeaderElectionClient.CoordinationV1(), resourcelock.ResourceLockConfig{ diff --git a/cmd/kube-controller-manager/app/options/options.go b/cmd/kube-controller-manager/app/options/options.go index cad1edb2979..cfa0d9d709c 100644 --- a/cmd/kube-controller-manager/app/options/options.go +++ b/cmd/kube-controller-manager/app/options/options.go @@ -184,6 +184,8 @@ func NewKubeControllerManagerOptions() (*KubeControllerManagerOptions, error) { } s.GarbageCollectorController.GCIgnoredResources = gcIgnoredResources + s.Generic.LeaderElection.ResourceName = "kube-controller-manager" + s.Generic.LeaderElection.ResourceNamespace = "kube-system" return &s, nil } diff --git a/cmd/kube-controller-manager/app/options/options_test.go b/cmd/kube-controller-manager/app/options/options_test.go index c1b10aea590..6290bcf1b5a 100644 --- a/cmd/kube-controller-manager/app/options/options_test.go +++ b/cmd/kube-controller-manager/app/options/options_test.go @@ -154,11 +154,13 @@ func TestAddFlags(t *testing.T) { }, ControllerStartInterval: metav1.Duration{Duration: 2 * time.Minute}, LeaderElection: componentbaseconfig.LeaderElectionConfiguration{ - ResourceLock: "configmap", - LeaderElect: false, - LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, - RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, - RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceLock: "configmap", + LeaderElect: false, + LeaseDuration: metav1.Duration{Duration: 30 * time.Second}, + RenewDeadline: metav1.Duration{Duration: 15 * time.Second}, + RetryPeriod: metav1.Duration{Duration: 5 * time.Second}, + ResourceName: "kube-controller-manager", + ResourceNamespace: "kube-system", }, Controllers: []string{"foo", "bar"}, }, diff --git a/pkg/client/leaderelectionconfig/config.go b/pkg/client/leaderelectionconfig/config.go index 223e24fa51b..bbdc977cc54 100644 --- a/pkg/client/leaderelectionconfig/config.go +++ b/pkg/client/leaderelectionconfig/config.go @@ -43,4 +43,10 @@ func BindFlags(l *componentbaseconfig.LeaderElectionConfiguration, fs *pflag.Fla fs.StringVar(&l.ResourceLock, "leader-elect-resource-lock", l.ResourceLock, ""+ "The type of resource object that is used for locking during "+ "leader election. Supported options are `endpoints` (default) and `configmaps`.") + fs.StringVar(&l.ResourceName, "leader-elect-resource-name", l.ResourceName, ""+ + "The name of resource object that is used for locking during "+ + "leader election.") + fs.StringVar(&l.ResourceNamespace, "leader-elect-resource-namespace", l.ResourceNamespace, ""+ + "The namespace of resource object that is used for locking during "+ + "leader election.") } diff --git a/staging/src/k8s.io/component-base/config/types.go b/staging/src/k8s.io/component-base/config/types.go index f0a20915cc8..da11e03c2c6 100644 --- a/staging/src/k8s.io/component-base/config/types.go +++ b/staging/src/k8s.io/component-base/config/types.go @@ -62,6 +62,12 @@ type LeaderElectionConfiguration struct { // resourceLock indicates the resource object type that will be used to lock // during leader election cycles. ResourceLock string + // resourceName indicates the name of resource object that will be used to lock + // during leader election cycles. + ResourceName string + // resourceName indicates the namespace of resource object that will be used to lock + // during leader election cycles. + ResourceNamespace string } // DebuggingConfiguration holds configuration for Debugging related features. diff --git a/staging/src/k8s.io/component-base/config/v1alpha1/types.go b/staging/src/k8s.io/component-base/config/v1alpha1/types.go index d5a0391a209..a5202e3e9d2 100644 --- a/staging/src/k8s.io/component-base/config/v1alpha1/types.go +++ b/staging/src/k8s.io/component-base/config/v1alpha1/types.go @@ -48,6 +48,12 @@ type LeaderElectionConfiguration struct { // resourceLock indicates the resource object type that will be used to lock // during leader election cycles. ResourceLock string `json:"resourceLock"` + // resourceName indicates the name of resource object that will be used to lock + // during leader election cycles. + ResourceName string `json:"resourceName"` + // resourceName indicates the namespace of resource object that will be used to lock + // during leader election cycles. + ResourceNamespace string `json:"resourceNamespace"` } // DebuggingConfiguration holds configuration for Debugging related features. diff --git a/staging/src/k8s.io/component-base/config/v1alpha1/zz_generated.conversion.go b/staging/src/k8s.io/component-base/config/v1alpha1/zz_generated.conversion.go index 55f0dac0390..c62ee734b89 100644 --- a/staging/src/k8s.io/component-base/config/v1alpha1/zz_generated.conversion.go +++ b/staging/src/k8s.io/component-base/config/v1alpha1/zz_generated.conversion.go @@ -135,6 +135,8 @@ func autoConvert_v1alpha1_LeaderElectionConfiguration_To_config_LeaderElectionCo out.RenewDeadline = in.RenewDeadline out.RetryPeriod = in.RetryPeriod out.ResourceLock = in.ResourceLock + out.ResourceName = in.ResourceName + out.ResourceNamespace = in.ResourceNamespace return nil } @@ -146,5 +148,7 @@ func autoConvert_config_LeaderElectionConfiguration_To_v1alpha1_LeaderElectionCo out.RenewDeadline = in.RenewDeadline out.RetryPeriod = in.RetryPeriod out.ResourceLock = in.ResourceLock + out.ResourceName = in.ResourceName + out.ResourceNamespace = in.ResourceNamespace return nil }