Skip to content

Commit 276ea3e

Browse files
wojtek-tk8s-publishing-bot
authored andcommitted
Remove support for Endpoints and ConfigMaps lock from leader election
Kubernetes-commit: 29d9683cd0fb3cc81810a8b39715e5eb4c68b00e
1 parent cbd965e commit 276ea3e

File tree

4 files changed

+99
-107
lines changed

4 files changed

+99
-107
lines changed

tools/leaderelection/leaderelection_test.go

+9-83
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ func createLockObject(t *testing.T, objectType, namespace, name string, record *
6666
return
6767
}
6868

69-
// Will test leader election using endpoints as the resource
70-
func TestTryAcquireOrRenewEndpoints(t *testing.T) {
71-
testTryAcquireOrRenew(t, "endpoints")
72-
}
73-
7469
type Reactor struct {
7570
verb string
7671
objectType string
@@ -259,24 +254,14 @@ func testTryAcquireOrRenew(t *testing.T, objectType string) {
259254
})
260255

261256
switch objectType {
262-
case "endpoints":
263-
lock = &rl.EndpointsLock{
264-
EndpointsMeta: objectMeta,
265-
LockConfig: resourceLockConfig,
266-
Client: c.CoreV1(),
267-
}
268-
case "configmaps":
269-
lock = &rl.ConfigMapLock{
270-
ConfigMapMeta: objectMeta,
271-
LockConfig: resourceLockConfig,
272-
Client: c.CoreV1(),
273-
}
274257
case "leases":
275258
lock = &rl.LeaseLock{
276259
LeaseMeta: objectMeta,
277260
LockConfig: resourceLockConfig,
278261
Client: c.CoordinationV1(),
279262
}
263+
default:
264+
t.Fatalf("Unknown objectType: %v", objectType)
280265
}
281266

282267
lec := LeaderElectionConfig{
@@ -325,11 +310,6 @@ func testTryAcquireOrRenew(t *testing.T, objectType string) {
325310
}
326311
}
327312

328-
// Will test leader election using configmap as the resource
329-
func TestTryAcquireOrRenewConfigMaps(t *testing.T) {
330-
testTryAcquireOrRenew(t, "configmaps")
331-
}
332-
333313
// Will test leader election using lease as the resource
334314
func TestTryAcquireOrRenewLeases(t *testing.T) {
335315
testTryAcquireOrRenew(t, "leases")
@@ -364,9 +344,9 @@ func TestLeaseSpecToLeaderElectionRecordRoundTrip(t *testing.T) {
364344
func multiLockType(t *testing.T, objectType string) (primaryType, secondaryType string) {
365345
switch objectType {
366346
case rl.EndpointsLeasesResourceLock:
367-
return rl.EndpointsResourceLock, rl.LeasesResourceLock
347+
return "endpoints", rl.LeasesResourceLock
368348
case rl.ConfigMapsLeasesResourceLock:
369-
return rl.ConfigMapsResourceLock, rl.LeasesResourceLock
349+
return "configmaps", rl.LeasesResourceLock
370350
default:
371351
t.Fatal("unexpected objType:" + objectType)
372352
}
@@ -818,9 +798,7 @@ func testTryAcquireOrRenewMultiLock(t *testing.T, objectType string) {
818798
var wg sync.WaitGroup
819799
wg.Add(1)
820800
var reportedLeader string
821-
var lock rl.Interface
822801

823-
objectMeta := metav1.ObjectMeta{Namespace: "foo", Name: "bar"}
824802
resourceLockConfig := rl.ResourceLockConfig{
825803
Identity: "baz",
826804
EventRecorder: &record.FakeRecorder{},
@@ -834,33 +812,9 @@ func testTryAcquireOrRenewMultiLock(t *testing.T, objectType string) {
834812
return true, nil, fmt.Errorf("unreachable action")
835813
})
836814

837-
switch objectType {
838-
case rl.EndpointsLeasesResourceLock:
839-
lock = &rl.MultiLock{
840-
Primary: &rl.EndpointsLock{
841-
EndpointsMeta: objectMeta,
842-
LockConfig: resourceLockConfig,
843-
Client: c.CoreV1(),
844-
},
845-
Secondary: &rl.LeaseLock{
846-
LeaseMeta: objectMeta,
847-
LockConfig: resourceLockConfig,
848-
Client: c.CoordinationV1(),
849-
},
850-
}
851-
case rl.ConfigMapsLeasesResourceLock:
852-
lock = &rl.MultiLock{
853-
Primary: &rl.ConfigMapLock{
854-
ConfigMapMeta: objectMeta,
855-
LockConfig: resourceLockConfig,
856-
Client: c.CoreV1(),
857-
},
858-
Secondary: &rl.LeaseLock{
859-
LeaseMeta: objectMeta,
860-
LockConfig: resourceLockConfig,
861-
Client: c.CoordinationV1(),
862-
},
863-
}
815+
lock, err := rl.New(objectType, "foo", "bar", c.CoreV1(), c.CoordinationV1(), resourceLockConfig)
816+
if err != nil {
817+
t.Fatalf("Couldn't create lock: %v", err)
864818
}
865819

866820
lec := LeaderElectionConfig{
@@ -983,24 +937,14 @@ func testReleaseLease(t *testing.T, objectType string) {
983937
})
984938

985939
switch objectType {
986-
case "endpoints":
987-
lock = &rl.EndpointsLock{
988-
EndpointsMeta: objectMeta,
989-
LockConfig: resourceLockConfig,
990-
Client: c.CoreV1(),
991-
}
992-
case "configmaps":
993-
lock = &rl.ConfigMapLock{
994-
ConfigMapMeta: objectMeta,
995-
LockConfig: resourceLockConfig,
996-
Client: c.CoreV1(),
997-
}
998940
case "leases":
999941
lock = &rl.LeaseLock{
1000942
LeaseMeta: objectMeta,
1001943
LockConfig: resourceLockConfig,
1002944
Client: c.CoordinationV1(),
1003945
}
946+
default:
947+
t.Fatalf("Unknown objectType: %v", objectType)
1004948
}
1005949

1006950
lec := LeaderElectionConfig{
@@ -1058,29 +1002,11 @@ func testReleaseLease(t *testing.T, objectType string) {
10581002
}
10591003
}
10601004

1061-
// Will test leader election using endpoints as the resource
1062-
func TestReleaseLeaseEndpoints(t *testing.T) {
1063-
testReleaseLease(t, "endpoints")
1064-
}
1065-
1066-
// Will test leader election using endpoints as the resource
1067-
func TestReleaseLeaseConfigMaps(t *testing.T) {
1068-
testReleaseLease(t, "configmaps")
1069-
}
1070-
10711005
// Will test leader election using endpoints as the resource
10721006
func TestReleaseLeaseLeases(t *testing.T) {
10731007
testReleaseLease(t, "leases")
10741008
}
10751009

1076-
func TestReleaseOnCancellation_Endpoints(t *testing.T) {
1077-
testReleaseOnCancellation(t, "endpoints")
1078-
}
1079-
1080-
func TestReleaseOnCancellation_ConfigMaps(t *testing.T) {
1081-
testReleaseOnCancellation(t, "configmaps")
1082-
}
1083-
10841010
func TestReleaseOnCancellation_Leases(t *testing.T) {
10851011
testReleaseOnCancellation(t, "leases")
10861012
}

tools/leaderelection/resourcelock/configmaplock.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
// and use ConfigMaps as the means to pass that configuration
3333
// data we will likely move to deprecate the Endpoints lock.
3434

35-
type ConfigMapLock struct {
35+
type configMapLock struct {
3636
// ConfigMapMeta should contain a Name and a Namespace of a
3737
// ConfigMapMeta object that the LeaderElector will attempt to lead.
3838
ConfigMapMeta metav1.ObjectMeta
@@ -42,7 +42,7 @@ type ConfigMapLock struct {
4242
}
4343

4444
// Get returns the election record from a ConfigMap Annotation
45-
func (cml *ConfigMapLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) {
45+
func (cml *configMapLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) {
4646
var record LeaderElectionRecord
4747
var err error
4848
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
6363
}
6464

6565
// Create attempts to create a LeaderElectionRecord annotation
66-
func (cml *ConfigMapLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
66+
func (cml *configMapLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
6767
recordBytes, err := json.Marshal(ler)
6868
if err != nil {
6969
return err
@@ -81,7 +81,7 @@ func (cml *ConfigMapLock) Create(ctx context.Context, ler LeaderElectionRecord)
8181
}
8282

8383
// Update will update an existing annotation on a given resource.
84-
func (cml *ConfigMapLock) Update(ctx context.Context, ler LeaderElectionRecord) error {
84+
func (cml *configMapLock) Update(ctx context.Context, ler LeaderElectionRecord) error {
8585
if cml.cm == nil {
8686
return errors.New("configmap not initialized, call get or create first")
8787
}
@@ -102,7 +102,7 @@ func (cml *ConfigMapLock) Update(ctx context.Context, ler LeaderElectionRecord)
102102
}
103103

104104
// RecordEvent in leader election while adding meta-data
105-
func (cml *ConfigMapLock) RecordEvent(s string) {
105+
func (cml *configMapLock) RecordEvent(s string) {
106106
if cml.LockConfig.EventRecorder == nil {
107107
return
108108
}
@@ -116,11 +116,11 @@ func (cml *ConfigMapLock) RecordEvent(s string) {
116116

117117
// Describe is used to convert details on current resource lock
118118
// into a string
119-
func (cml *ConfigMapLock) Describe() string {
119+
func (cml *configMapLock) Describe() string {
120120
return fmt.Sprintf("%v/%v", cml.ConfigMapMeta.Namespace, cml.ConfigMapMeta.Name)
121121
}
122122

123123
// Identity returns the Identity of the lock
124-
func (cml *ConfigMapLock) Identity() string {
124+
func (cml *configMapLock) Identity() string {
125125
return cml.LockConfig.Identity
126126
}

tools/leaderelection/resourcelock/endpointslock.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
2828
)
2929

30-
type EndpointsLock struct {
30+
type endpointsLock struct {
3131
// EndpointsMeta should contain a Name and a Namespace of an
3232
// Endpoints object that the LeaderElector will attempt to lead.
3333
EndpointsMeta metav1.ObjectMeta
@@ -37,7 +37,7 @@ type EndpointsLock struct {
3737
}
3838

3939
// Get returns the election record from a Endpoints Annotation
40-
func (el *EndpointsLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) {
40+
func (el *endpointsLock) Get(ctx context.Context) (*LeaderElectionRecord, []byte, error) {
4141
var record LeaderElectionRecord
4242
var err error
4343
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
5858
}
5959

6060
// Create attempts to create a LeaderElectionRecord annotation
61-
func (el *EndpointsLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
61+
func (el *endpointsLock) Create(ctx context.Context, ler LeaderElectionRecord) error {
6262
recordBytes, err := json.Marshal(ler)
6363
if err != nil {
6464
return err
@@ -76,7 +76,7 @@ func (el *EndpointsLock) Create(ctx context.Context, ler LeaderElectionRecord) e
7676
}
7777

7878
// Update will update and existing annotation on a given resource.
79-
func (el *EndpointsLock) Update(ctx context.Context, ler LeaderElectionRecord) error {
79+
func (el *endpointsLock) Update(ctx context.Context, ler LeaderElectionRecord) error {
8080
if el.e == nil {
8181
return errors.New("endpoint not initialized, call get or create first")
8282
}
@@ -97,7 +97,7 @@ func (el *EndpointsLock) Update(ctx context.Context, ler LeaderElectionRecord) e
9797
}
9898

9999
// RecordEvent in leader election while adding meta-data
100-
func (el *EndpointsLock) RecordEvent(s string) {
100+
func (el *endpointsLock) RecordEvent(s string) {
101101
if el.LockConfig.EventRecorder == nil {
102102
return
103103
}
@@ -111,11 +111,11 @@ func (el *EndpointsLock) RecordEvent(s string) {
111111

112112
// Describe is used to convert details on current resource lock
113113
// into a string
114-
func (el *EndpointsLock) Describe() string {
114+
func (el *endpointsLock) Describe() string {
115115
return fmt.Sprintf("%v/%v", el.EndpointsMeta.Namespace, el.EndpointsMeta.Name)
116116
}
117117

118118
// Identity returns the Identity of the lock
119-
func (el *EndpointsLock) Identity() string {
119+
func (el *endpointsLock) Identity() string {
120120
return el.LockConfig.Identity
121121
}

tools/leaderelection/resourcelock/interface.go

+76-10
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,77 @@ import (
3131

3232
const (
3333
LeaderElectionRecordAnnotationKey = "control-plane.alpha.kubernetes.io/leader"
34-
EndpointsResourceLock = "endpoints"
35-
ConfigMapsResourceLock = "configmaps"
34+
endpointsResourceLock = "endpoints"
35+
configMapsResourceLock = "configmaps"
3636
LeasesResourceLock = "leases"
37-
EndpointsLeasesResourceLock = "endpointsleases"
38-
ConfigMapsLeasesResourceLock = "configmapsleases"
37+
// When using EndpointsLeasesResourceLock, you need to ensure that
38+
// API Priority & Fairness is configured with non-default flow-schema
39+
// that will catch the necessary operations on leader-election related
40+
// endpoint objects.
41+
//
42+
// The example of such flow scheme could look like this:
43+
// apiVersion: flowcontrol.apiserver.k8s.io/v1beta2
44+
// kind: FlowSchema
45+
// metadata:
46+
// name: my-leader-election
47+
// spec:
48+
// distinguisherMethod:
49+
// type: ByUser
50+
// matchingPrecedence: 200
51+
// priorityLevelConfiguration:
52+
// name: leader-election # reference the <leader-election> PL
53+
// rules:
54+
// - resourceRules:
55+
// - apiGroups:
56+
// - ""
57+
// namespaces:
58+
// - '*'
59+
// resources:
60+
// - endpoints
61+
// verbs:
62+
// - get
63+
// - create
64+
// - update
65+
// subjects:
66+
// - kind: ServiceAccount
67+
// serviceAccount:
68+
// name: '*'
69+
// namespace: kube-system
70+
EndpointsLeasesResourceLock = "endpointsleases"
71+
// When using EndpointsLeasesResourceLock, you need to ensure that
72+
// API Priority & Fairness is configured with non-default flow-schema
73+
// that will catch the necessary operations on leader-election related
74+
// configmap objects.
75+
//
76+
// The example of such flow scheme could look like this:
77+
// apiVersion: flowcontrol.apiserver.k8s.io/v1beta2
78+
// kind: FlowSchema
79+
// metadata:
80+
// name: my-leader-election
81+
// spec:
82+
// distinguisherMethod:
83+
// type: ByUser
84+
// matchingPrecedence: 200
85+
// priorityLevelConfiguration:
86+
// name: leader-election # reference the <leader-election> PL
87+
// rules:
88+
// - resourceRules:
89+
// - apiGroups:
90+
// - ""
91+
// namespaces:
92+
// - '*'
93+
// resources:
94+
// - configmaps
95+
// verbs:
96+
// - get
97+
// - create
98+
// - update
99+
// subjects:
100+
// - kind: ServiceAccount
101+
// serviceAccount:
102+
// name: '*'
103+
// namespace: kube-system
104+
ConfigMapsLeasesResourceLock = "configmapsleases"
39105
)
40106

41107
// LeaderElectionRecord is the record that is stored in the leader election annotation.
@@ -98,15 +164,15 @@ type Interface interface {
98164

99165
// Manufacture will create a lock of a given type according to the input parameters
100166
func New(lockType string, ns string, name string, coreClient corev1.CoreV1Interface, coordinationClient coordinationv1.CoordinationV1Interface, rlc ResourceLockConfig) (Interface, error) {
101-
endpointsLock := &EndpointsLock{
167+
endpointsLock := &endpointsLock{
102168
EndpointsMeta: metav1.ObjectMeta{
103169
Namespace: ns,
104170
Name: name,
105171
},
106172
Client: coreClient,
107173
LockConfig: rlc,
108174
}
109-
configmapLock := &ConfigMapLock{
175+
configmapLock := &configMapLock{
110176
ConfigMapMeta: metav1.ObjectMeta{
111177
Namespace: ns,
112178
Name: name,
@@ -123,10 +189,10 @@ func New(lockType string, ns string, name string, coreClient corev1.CoreV1Interf
123189
LockConfig: rlc,
124190
}
125191
switch lockType {
126-
case EndpointsResourceLock:
127-
return endpointsLock, nil
128-
case ConfigMapsResourceLock:
129-
return configmapLock, nil
192+
case endpointsResourceLock:
193+
return nil, fmt.Errorf("endpoints lock is removed, migrate to %s", EndpointsLeasesResourceLock)
194+
case configMapsResourceLock:
195+
return nil, fmt.Errorf("configmaps lock is removed, migrate to %s", ConfigMapsLeasesResourceLock)
130196
case LeasesResourceLock:
131197
return leaseLock, nil
132198
case EndpointsLeasesResourceLock:

0 commit comments

Comments
 (0)