Merge pull request #106852 from wojtek-t/remove_legacy_leader_election

Remove support for Endpoints and ConfigMaps lock from leader election
This commit is contained in:
Kubernetes Prow Robot 2021-12-09 12:21:03 -08:00 committed by GitHub
commit 12b4bef7bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 107 deletions

View File

@ -66,11 +66,6 @@ func createLockObject(t *testing.T, objectType, namespace, name string, record *
return return
} }
// Will test leader election using endpoints as the resource
func TestTryAcquireOrRenewEndpoints(t *testing.T) {
testTryAcquireOrRenew(t, "endpoints")
}
type Reactor struct { type Reactor struct {
verb string verb string
objectType string objectType string
@ -259,24 +254,14 @@ func testTryAcquireOrRenew(t *testing.T, objectType string) {
}) })
switch objectType { switch objectType {
case "endpoints":
lock = &rl.EndpointsLock{
EndpointsMeta: objectMeta,
LockConfig: resourceLockConfig,
Client: c.CoreV1(),
}
case "configmaps":
lock = &rl.ConfigMapLock{
ConfigMapMeta: objectMeta,
LockConfig: resourceLockConfig,
Client: c.CoreV1(),
}
case "leases": case "leases":
lock = &rl.LeaseLock{ lock = &rl.LeaseLock{
LeaseMeta: objectMeta, LeaseMeta: objectMeta,
LockConfig: resourceLockConfig, LockConfig: resourceLockConfig,
Client: c.CoordinationV1(), Client: c.CoordinationV1(),
} }
default:
t.Fatalf("Unknown objectType: %v", objectType)
} }
lec := LeaderElectionConfig{ lec := LeaderElectionConfig{
@ -325,11 +310,6 @@ func testTryAcquireOrRenew(t *testing.T, objectType string) {
} }
} }
// Will test leader election using configmap as the resource
func TestTryAcquireOrRenewConfigMaps(t *testing.T) {
testTryAcquireOrRenew(t, "configmaps")
}
// Will test leader election using lease as the resource // Will test leader election using lease as the resource
func TestTryAcquireOrRenewLeases(t *testing.T) { func TestTryAcquireOrRenewLeases(t *testing.T) {
testTryAcquireOrRenew(t, "leases") testTryAcquireOrRenew(t, "leases")
@ -364,9 +344,9 @@ func TestLeaseSpecToLeaderElectionRecordRoundTrip(t *testing.T) {
func multiLockType(t *testing.T, objectType string) (primaryType, secondaryType string) { func multiLockType(t *testing.T, objectType string) (primaryType, secondaryType string) {
switch objectType { switch objectType {
case rl.EndpointsLeasesResourceLock: case rl.EndpointsLeasesResourceLock:
return rl.EndpointsResourceLock, rl.LeasesResourceLock return "endpoints", rl.LeasesResourceLock
case rl.ConfigMapsLeasesResourceLock: case rl.ConfigMapsLeasesResourceLock:
return rl.ConfigMapsResourceLock, rl.LeasesResourceLock return "configmaps", rl.LeasesResourceLock
default: default:
t.Fatal("unexpected objType:" + objectType) t.Fatal("unexpected objType:" + objectType)
} }
@ -818,9 +798,7 @@ func testTryAcquireOrRenewMultiLock(t *testing.T, objectType string) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
var reportedLeader string var reportedLeader string
var lock rl.Interface
objectMeta := metav1.ObjectMeta{Namespace: "foo", Name: "bar"}
resourceLockConfig := rl.ResourceLockConfig{ resourceLockConfig := rl.ResourceLockConfig{
Identity: "baz", Identity: "baz",
EventRecorder: &record.FakeRecorder{}, EventRecorder: &record.FakeRecorder{},
@ -834,33 +812,9 @@ func testTryAcquireOrRenewMultiLock(t *testing.T, objectType string) {
return true, nil, fmt.Errorf("unreachable action") return true, nil, fmt.Errorf("unreachable action")
}) })
switch objectType { lock, err := rl.New(objectType, "foo", "bar", c.CoreV1(), c.CoordinationV1(), resourceLockConfig)
case rl.EndpointsLeasesResourceLock: if err != nil {
lock = &rl.MultiLock{ t.Fatalf("Couldn't create lock: %v", err)
Primary: &rl.EndpointsLock{
EndpointsMeta: objectMeta,
LockConfig: resourceLockConfig,
Client: c.CoreV1(),
},
Secondary: &rl.LeaseLock{
LeaseMeta: objectMeta,
LockConfig: resourceLockConfig,
Client: c.CoordinationV1(),
},
}
case rl.ConfigMapsLeasesResourceLock:
lock = &rl.MultiLock{
Primary: &rl.ConfigMapLock{
ConfigMapMeta: objectMeta,
LockConfig: resourceLockConfig,
Client: c.CoreV1(),
},
Secondary: &rl.LeaseLock{
LeaseMeta: objectMeta,
LockConfig: resourceLockConfig,
Client: c.CoordinationV1(),
},
}
} }
lec := LeaderElectionConfig{ lec := LeaderElectionConfig{
@ -983,24 +937,14 @@ func testReleaseLease(t *testing.T, objectType string) {
}) })
switch objectType { switch objectType {
case "endpoints":
lock = &rl.EndpointsLock{
EndpointsMeta: objectMeta,
LockConfig: resourceLockConfig,
Client: c.CoreV1(),
}
case "configmaps":
lock = &rl.ConfigMapLock{
ConfigMapMeta: objectMeta,
LockConfig: resourceLockConfig,
Client: c.CoreV1(),
}
case "leases": case "leases":
lock = &rl.LeaseLock{ lock = &rl.LeaseLock{
LeaseMeta: objectMeta, LeaseMeta: objectMeta,
LockConfig: resourceLockConfig, LockConfig: resourceLockConfig,
Client: c.CoordinationV1(), Client: c.CoordinationV1(),
} }
default:
t.Fatalf("Unknown objectType: %v", objectType)
} }
lec := LeaderElectionConfig{ lec := LeaderElectionConfig{
@ -1058,29 +1002,11 @@ func testReleaseLease(t *testing.T, objectType string) {
} }
} }
// Will test leader election using endpoints as the resource
func TestReleaseLeaseEndpoints(t *testing.T) {
testReleaseLease(t, "endpoints")
}
// Will test leader election using endpoints as the resource
func TestReleaseLeaseConfigMaps(t *testing.T) {
testReleaseLease(t, "configmaps")
}
// Will test leader election using endpoints as the resource // Will test leader election using endpoints as the resource
func TestReleaseLeaseLeases(t *testing.T) { func TestReleaseLeaseLeases(t *testing.T) {
testReleaseLease(t, "leases") testReleaseLease(t, "leases")
} }
func TestReleaseOnCancellation_Endpoints(t *testing.T) {
testReleaseOnCancellation(t, "endpoints")
}
func TestReleaseOnCancellation_ConfigMaps(t *testing.T) {
testReleaseOnCancellation(t, "configmaps")
}
func TestReleaseOnCancellation_Leases(t *testing.T) { func TestReleaseOnCancellation_Leases(t *testing.T) {
testReleaseOnCancellation(t, "leases") testReleaseOnCancellation(t, "leases")
} }

View File

@ -32,7 +32,7 @@ import (
// and use ConfigMaps as the means to pass that configuration // and use ConfigMaps as the means to pass that configuration
// data we will likely move to deprecate the Endpoints lock. // data we will likely move to deprecate the Endpoints lock.
type ConfigMapLock struct { type configMapLock struct {
// ConfigMapMeta should contain a Name and a Namespace of a // ConfigMapMeta should contain a Name and a Namespace of a
// ConfigMapMeta object that the LeaderElector will attempt to lead. // ConfigMapMeta object that the LeaderElector will attempt to lead.
ConfigMapMeta metav1.ObjectMeta ConfigMapMeta metav1.ObjectMeta
@ -42,7 +42,7 @@ type ConfigMapLock struct {
} }
// Get returns the election record from a ConfigMap Annotation // Get returns the election record from a ConfigMap Annotation
func (cml *ConfigMapLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) { func (cml *configMapLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) {
var record LeaderElectionRecord var record LeaderElectionRecord
var err error var err error
cml.cm, err = cml.Client.ConfigMaps(cml.ConfigMapMeta.Namespace).Get(ctx, cml.ConfigMapMeta.Name, metav1.GetOptions{}) cml.cm, err = cml.Client.ConfigMaps(cml.ConfigMapMeta.Namespace).Get(ctx, cml.ConfigMapMeta.Name, metav1.GetOptions{})
@ -63,7 +63,7 @@ func (cml *ConfigMapLock) Get(ctx context.Context) (*LeaderElectionRecord, []byt
} }
// Create attempts to create a LeaderElectionRecord annotation // Create attempts to create a LeaderElectionRecord annotation
func (cml *ConfigMapLock) Create(ctx context.Context, ler LeaderElectionRecord) error { func (cml *configMapLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
recordBytes, err := json.Marshal(ler) recordBytes, err := json.Marshal(ler)
if err != nil { if err != nil {
return err return err
@ -81,7 +81,7 @@ func (cml *ConfigMapLock) Create(ctx context.Context, ler LeaderElectionRecord)
} }
// Update will update an existing annotation on a given resource. // Update will update an existing annotation on a given resource.
func (cml *ConfigMapLock) Update(ctx context.Context, ler LeaderElectionRecord) error { func (cml *configMapLock) Update(ctx context.Context, ler LeaderElectionRecord) error {
if cml.cm == nil { if cml.cm == nil {
return errors.New("configmap not initialized, call get or create first") return errors.New("configmap not initialized, call get or create first")
} }
@ -102,7 +102,7 @@ func (cml *ConfigMapLock) Update(ctx context.Context, ler LeaderElectionRecord)
} }
// RecordEvent in leader election while adding meta-data // RecordEvent in leader election while adding meta-data
func (cml *ConfigMapLock) RecordEvent(s string) { func (cml *configMapLock) RecordEvent(s string) {
if cml.LockConfig.EventRecorder == nil { if cml.LockConfig.EventRecorder == nil {
return return
} }
@ -116,11 +116,11 @@ func (cml *ConfigMapLock) RecordEvent(s string) {
// Describe is used to convert details on current resource lock // Describe is used to convert details on current resource lock
// into a string // into a string
func (cml *ConfigMapLock) Describe() string { func (cml *configMapLock) Describe() string {
return fmt.Sprintf("%v/%v", cml.ConfigMapMeta.Namespace, cml.ConfigMapMeta.Name) return fmt.Sprintf("%v/%v", cml.ConfigMapMeta.Namespace, cml.ConfigMapMeta.Name)
} }
// Identity returns the Identity of the lock // Identity returns the Identity of the lock
func (cml *ConfigMapLock) Identity() string { func (cml *configMapLock) Identity() string {
return cml.LockConfig.Identity return cml.LockConfig.Identity
} }

View File

@ -27,7 +27,7 @@ import (
corev1client "k8s.io/client-go/kubernetes/typed/core/v1" corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
) )
type EndpointsLock struct { type endpointsLock struct {
// EndpointsMeta should contain a Name and a Namespace of an // EndpointsMeta should contain a Name and a Namespace of an
// Endpoints object that the LeaderElector will attempt to lead. // Endpoints object that the LeaderElector will attempt to lead.
EndpointsMeta metav1.ObjectMeta EndpointsMeta metav1.ObjectMeta
@ -37,7 +37,7 @@ type EndpointsLock struct {
} }
// Get returns the election record from a Endpoints Annotation // Get returns the election record from a Endpoints Annotation
func (el *EndpointsLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) { func (el *endpointsLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) {
var record LeaderElectionRecord var record LeaderElectionRecord
var err error var err error
el.e, err = el.Client.Endpoints(el.EndpointsMeta.Namespace).Get(ctx, el.EndpointsMeta.Name, metav1.GetOptions{}) el.e, err = el.Client.Endpoints(el.EndpointsMeta.Namespace).Get(ctx, el.EndpointsMeta.Name, metav1.GetOptions{})
@ -58,7 +58,7 @@ func (el *EndpointsLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte
} }
// Create attempts to create a LeaderElectionRecord annotation // Create attempts to create a LeaderElectionRecord annotation
func (el *EndpointsLock) Create(ctx context.Context, ler LeaderElectionRecord) error { func (el *endpointsLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
recordBytes, err := json.Marshal(ler) recordBytes, err := json.Marshal(ler)
if err != nil { if err != nil {
return err return err
@ -76,7 +76,7 @@ func (el *EndpointsLock) Create(ctx context.Context, ler LeaderElectionRecord) e
} }
// Update will update and existing annotation on a given resource. // Update will update and existing annotation on a given resource.
func (el *EndpointsLock) Update(ctx context.Context, ler LeaderElectionRecord) error { func (el *endpointsLock) Update(ctx context.Context, ler LeaderElectionRecord) error {
if el.e == nil { if el.e == nil {
return errors.New("endpoint not initialized, call get or create first") return errors.New("endpoint not initialized, call get or create first")
} }
@ -97,7 +97,7 @@ func (el *EndpointsLock) Update(ctx context.Context, ler LeaderElectionRecord) e
} }
// RecordEvent in leader election while adding meta-data // RecordEvent in leader election while adding meta-data
func (el *EndpointsLock) RecordEvent(s string) { func (el *endpointsLock) RecordEvent(s string) {
if el.LockConfig.EventRecorder == nil { if el.LockConfig.EventRecorder == nil {
return return
} }
@ -111,11 +111,11 @@ func (el *EndpointsLock) RecordEvent(s string) {
// Describe is used to convert details on current resource lock // Describe is used to convert details on current resource lock
// into a string // into a string
func (el *EndpointsLock) Describe() string { func (el *endpointsLock) Describe() string {
return fmt.Sprintf("%v/%v", el.EndpointsMeta.Namespace, el.EndpointsMeta.Name) return fmt.Sprintf("%v/%v", el.EndpointsMeta.Namespace, el.EndpointsMeta.Name)
} }
// Identity returns the Identity of the lock // Identity returns the Identity of the lock
func (el *EndpointsLock) Identity() string { func (el *endpointsLock) Identity() string {
return el.LockConfig.Identity return el.LockConfig.Identity
} }

View File

@ -31,11 +31,77 @@ import (
const ( const (
LeaderElectionRecordAnnotationKey = "control-plane.alpha.kubernetes.io/leader" LeaderElectionRecordAnnotationKey = "control-plane.alpha.kubernetes.io/leader"
EndpointsResourceLock = "endpoints" endpointsResourceLock = "endpoints"
ConfigMapsResourceLock = "configmaps" configMapsResourceLock = "configmaps"
LeasesResourceLock = "leases" LeasesResourceLock = "leases"
EndpointsLeasesResourceLock = "endpointsleases" // When using EndpointsLeasesResourceLock, you need to ensure that
ConfigMapsLeasesResourceLock = "configmapsleases" // API Priority & Fairness is configured with non-default flow-schema
// that will catch the necessary operations on leader-election related
// endpoint objects.
//
// The example of such flow scheme could look like this:
// apiVersion: flowcontrol.apiserver.k8s.io/v1beta2
// kind: FlowSchema
// metadata:
// name: my-leader-election
// spec:
// distinguisherMethod:
// type: ByUser
// matchingPrecedence: 200
// priorityLevelConfiguration:
// name: leader-election # reference the <leader-election> PL
// rules:
// - resourceRules:
// - apiGroups:
// - ""
// namespaces:
// - '*'
// resources:
// - endpoints
// verbs:
// - get
// - create
// - update
// subjects:
// - kind: ServiceAccount
// serviceAccount:
// name: '*'
// namespace: kube-system
EndpointsLeasesResourceLock = "endpointsleases"
// When using EndpointsLeasesResourceLock, you need to ensure that
// API Priority & Fairness is configured with non-default flow-schema
// that will catch the necessary operations on leader-election related
// configmap objects.
//
// The example of such flow scheme could look like this:
// apiVersion: flowcontrol.apiserver.k8s.io/v1beta2
// kind: FlowSchema
// metadata:
// name: my-leader-election
// spec:
// distinguisherMethod:
// type: ByUser
// matchingPrecedence: 200
// priorityLevelConfiguration:
// name: leader-election # reference the <leader-election> PL
// rules:
// - resourceRules:
// - apiGroups:
// - ""
// namespaces:
// - '*'
// resources:
// - configmaps
// verbs:
// - get
// - create
// - update
// subjects:
// - kind: ServiceAccount
// serviceAccount:
// name: '*'
// namespace: kube-system
ConfigMapsLeasesResourceLock = "configmapsleases"
) )
// LeaderElectionRecord is the record that is stored in the leader election annotation. // LeaderElectionRecord is the record that is stored in the leader election annotation.
@ -98,7 +164,7 @@ type Interface interface {
// Manufacture will create a lock of a given type according to the input parameters // Manufacture will create a lock of a given type according to the input parameters
func New(lockType string, ns string, name string, coreClient corev1.CoreV1Interface, coordinationClient coordinationv1.CoordinationV1Interface, rlc ResourceLockConfig) (Interface, error) { func New(lockType string, ns string, name string, coreClient corev1.CoreV1Interface, coordinationClient coordinationv1.CoordinationV1Interface, rlc ResourceLockConfig) (Interface, error) {
endpointsLock := &EndpointsLock{ endpointsLock := &endpointsLock{
EndpointsMeta: metav1.ObjectMeta{ EndpointsMeta: metav1.ObjectMeta{
Namespace: ns, Namespace: ns,
Name: name, Name: name,
@ -106,7 +172,7 @@ func New(lockType string, ns string, name string, coreClient corev1.CoreV1Interf
Client: coreClient, Client: coreClient,
LockConfig: rlc, LockConfig: rlc,
} }
configmapLock := &ConfigMapLock{ configmapLock := &configMapLock{
ConfigMapMeta: metav1.ObjectMeta{ ConfigMapMeta: metav1.ObjectMeta{
Namespace: ns, Namespace: ns,
Name: name, Name: name,
@ -123,10 +189,10 @@ func New(lockType string, ns string, name string, coreClient corev1.CoreV1Interf
LockConfig: rlc, LockConfig: rlc,
} }
switch lockType { switch lockType {
case EndpointsResourceLock: case endpointsResourceLock:
return endpointsLock, nil return nil, fmt.Errorf("endpoints lock is removed, migrate to %s", EndpointsLeasesResourceLock)
case ConfigMapsResourceLock: case configMapsResourceLock:
return configmapLock, nil return nil, fmt.Errorf("configmaps lock is removed, migrate to %s", ConfigMapsLeasesResourceLock)
case LeasesResourceLock: case LeasesResourceLock:
return leaseLock, nil return leaseLock, nil
case EndpointsLeasesResourceLock: case EndpointsLeasesResourceLock: