@@ -19,6 +19,7 @@ package apiserver
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "sync"
22
23
"testing"
23
24
"time"
24
25
@@ -36,6 +37,7 @@ import (
36
37
featuregatetesting "k8s.io/component-base/featuregate/testing"
37
38
"k8s.io/klog/v2"
38
39
apiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
40
+ "k8s.io/kubernetes/pkg/controlplane/apiserver"
39
41
"k8s.io/kubernetes/test/integration/framework"
40
42
)
41
43
@@ -50,10 +52,11 @@ func TestSingleLeaseCandidate(t *testing.T) {
50
52
config := server .ClientConfig
51
53
52
54
ctx , cancel := context .WithCancel (context .Background ())
55
+ defer cancel ()
53
56
cletest := setupCLE (config , ctx , cancel , t )
54
57
defer cletest .cleanup ()
55
58
go cletest .createAndRunFakeController ("foo1" , "default" , "foo" , "1.20.0" , "1.20.0" )
56
- cletest .pollForLease ("foo" , "default" , "foo1" )
59
+ cletest .pollForLease (ctx , "foo" , "default" , "foo1" )
57
60
}
58
61
59
62
func TestMultipleLeaseCandidate (t * testing.T ) {
@@ -67,14 +70,15 @@ func TestMultipleLeaseCandidate(t *testing.T) {
67
70
config := server .ClientConfig
68
71
69
72
ctx , cancel := context .WithCancel (context .Background ())
73
+ defer cancel ()
70
74
cletest := setupCLE (config , ctx , cancel , t )
71
75
defer cletest .cleanup ()
72
- go cletest .createAndRunFakeController ("foo1 " , "default" , "foo " , "1.20.0" , "1.20.0" )
73
- go cletest .createAndRunFakeController ("foo2 " , "default" , "foo " , "1.20.0" , "1.19.0" )
74
- go cletest .createAndRunFakeController ("foo3 " , "default" , "foo " , "1.19.0" , "1.19.0" )
75
- go cletest .createAndRunFakeController ("foo4 " , "default" , "foo " , "1.2.0" , "1.19.0" )
76
- go cletest .createAndRunFakeController ("foo5 " , "default" , "foo " , "1.20.0" , "1.19.0" )
77
- cletest .pollForLease ("foo " , "default" , "foo3 " )
76
+ go cletest .createAndRunFakeController ("baz1 " , "default" , "baz " , "1.20.0" , "1.20.0" )
77
+ go cletest .createAndRunFakeController ("baz2 " , "default" , "baz " , "1.20.0" , "1.19.0" )
78
+ go cletest .createAndRunFakeController ("baz3 " , "default" , "baz " , "1.19.0" , "1.19.0" )
79
+ go cletest .createAndRunFakeController ("baz4 " , "default" , "baz " , "1.2.0" , "1.19.0" )
80
+ go cletest .createAndRunFakeController ("baz5 " , "default" , "baz " , "1.20.0" , "1.19.0" )
81
+ cletest .pollForLease (ctx , "baz " , "default" , "baz3 " )
78
82
}
79
83
80
84
func TestLeaseSwapIfBetterAvailable (t * testing.T ) {
@@ -92,9 +96,9 @@ func TestLeaseSwapIfBetterAvailable(t *testing.T) {
92
96
defer cletest .cleanup ()
93
97
94
98
go cletest .createAndRunFakeController ("bar1" , "default" , "bar" , "1.20.0" , "1.20.0" )
95
- cletest .pollForLease ("bar" , "default" , "bar1" )
99
+ cletest .pollForLease (ctx , "bar" , "default" , "bar1" )
96
100
go cletest .createAndRunFakeController ("bar2" , "default" , "bar" , "1.19.0" , "1.19.0" )
97
- cletest .pollForLease ("bar" , "default" , "bar2" )
101
+ cletest .pollForLease (ctx , "bar" , "default" , "bar2" )
98
102
}
99
103
100
104
// TestUpgradeSkew tests that a legacy client and a CLE aware client operating on the same lease do not cause errors
@@ -109,20 +113,26 @@ func TestUpgradeSkew(t *testing.T) {
109
113
config := server .ClientConfig
110
114
111
115
ctx , cancel := context .WithCancel (context .Background ())
116
+ defer cancel ()
112
117
cletest := setupCLE (config , ctx , cancel , t )
113
118
defer cletest .cleanup ()
114
119
115
- go cletest .createAndRunFakeLegacyController ("foo1-130" , "default" , "foo " )
116
- cletest .pollForLease ("foo " , "default" , "foo1-130" )
117
- go cletest .createAndRunFakeController ("foo1-131" , "default" , "foo " , "1.31.0" , "1.31.0" )
120
+ go cletest .createAndRunFakeLegacyController ("foo1-130" , "default" , "foobar " )
121
+ cletest .pollForLease (ctx , "foobar " , "default" , "foo1-130" )
122
+ go cletest .createAndRunFakeController ("foo1-131" , "default" , "foobar " , "1.31.0" , "1.31.0" )
118
123
// running a new controller should not kick off old leader
119
- cletest .pollForLease ("foo " , "default" , "foo1-130" )
124
+ cletest .pollForLease (ctx , "foobar " , "default" , "foo1-130" )
120
125
cletest .cancelController ("foo1-130" , "default" )
121
- cletest .pollForLease ("foo " , "default" , "foo1-131" )
126
+ cletest .pollForLease (ctx , "foobar " , "default" , "foo1-131" )
122
127
}
123
128
124
129
func TestLeaseCandidateCleanup (t * testing.T ) {
125
130
featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , genericfeatures .CoordinatedLeaderElection , true )
131
+ apiserver .LeaseCandidateGCPeriod = 1 * time .Second
132
+
133
+ defer func () {
134
+ apiserver .LeaseCandidateGCPeriod = 30 * time .Minute
135
+ }()
126
136
127
137
server , err := apiservertesting .StartTestServer (t , apiservertesting .NewDefaultTestServerOptions (), nil , framework .SharedEtcd ())
128
138
if err != nil {
@@ -140,7 +150,7 @@ func TestLeaseCandidateCleanup(t *testing.T) {
140
150
Namespace : "default" ,
141
151
},
142
152
Spec : v1alpha1.LeaseCandidateSpec {
143
- LeaseName : "foobar " ,
153
+ LeaseName : "foobaz " ,
144
154
BinaryVersion : "0.1.0" ,
145
155
EmulationVersion : "0.1.0" ,
146
156
PreferredStrategies : []v1.CoordinatedLeaseStrategy {v1 .OldestEmulationVersion },
@@ -175,11 +185,14 @@ type cleTest struct {
175
185
config * rest.Config
176
186
clientset * kubernetes.Clientset
177
187
t * testing.T
188
+ mu sync.Mutex
178
189
ctxList map [string ]ctxCancelPair
179
190
}
180
191
181
- func (t cleTest ) createAndRunFakeLegacyController (name string , namespace string , targetLease string ) {
192
+ func (t * cleTest ) createAndRunFakeLegacyController (name string , namespace string , targetLease string ) {
182
193
ctx , cancel := context .WithCancel (context .Background ())
194
+ t .mu .Lock ()
195
+ defer t .mu .Unlock ()
183
196
t .ctxList [name + "/" + namespace ] = ctxCancelPair {ctx , cancel }
184
197
185
198
electionChecker := leaderelection .NewLeaderHealthzAdaptor (time .Second * 20 )
@@ -197,7 +210,7 @@ func (t cleTest) createAndRunFakeLegacyController(name string, namespace string,
197
210
})
198
211
199
212
}
200
- func (t cleTest ) createAndRunFakeController (name string , namespace string , targetLease string , binaryVersion string , compatibilityVersion string ) {
213
+ func (t * cleTest ) createAndRunFakeController (name string , namespace string , targetLease string , binaryVersion string , compatibilityVersion string ) {
201
214
identityLease , _ , err := leaderelection .NewCandidate (
202
215
t .clientset ,
203
216
namespace ,
@@ -212,7 +225,9 @@ func (t cleTest) createAndRunFakeController(name string, namespace string, targe
212
225
}
213
226
214
227
ctx , cancel := context .WithCancel (context .Background ())
228
+ t .mu .Lock ()
215
229
t .ctxList [name + "/" + namespace ] = ctxCancelPair {ctx , cancel }
230
+ t .mu .Unlock ()
216
231
go identityLease .Run (ctx )
217
232
218
233
electionChecker := leaderelection .NewLeaderHealthzAdaptor (time .Second * 20 )
@@ -266,8 +281,8 @@ func leaderElectAndRun(ctx context.Context, kubeconfig *rest.Config, lockIdentit
266
281
})
267
282
}
268
283
269
- func (t cleTest ) pollForLease (name , namespace , holder string ) {
270
- err := wait .PollUntilContextTimeout (t . ctxList [ "main" ]. ctx , 1000 * time .Millisecond , 25 * time .Second , true , func (ctx context.Context ) (done bool , err error ) {
284
+ func (t * cleTest ) pollForLease (ctx context. Context , name , namespace , holder string ) {
285
+ err := wait .PollUntilContextTimeout (ctx , 1000 * time .Millisecond , 25 * time .Second , true , func (ctx context.Context ) (done bool , err error ) {
271
286
lease , err := t .clientset .CoordinationV1 ().Leases (namespace ).Get (ctx , name , metav1.GetOptions {})
272
287
if err != nil {
273
288
fmt .Println (err )
@@ -280,29 +295,33 @@ func (t cleTest) pollForLease(name, namespace, holder string) {
280
295
}
281
296
}
282
297
283
- func (t cleTest ) cancelController (name , namespace string ) {
298
+ func (t * cleTest ) cancelController (name , namespace string ) {
299
+ t .mu .Lock ()
300
+ defer t .mu .Unlock ()
284
301
t .ctxList [name + "/" + namespace ].cancel ()
285
302
delete (t .ctxList , name + "/" + namespace )
286
303
}
287
304
288
- func (t cleTest ) cleanup () {
305
+ func (t * cleTest ) cleanup () {
306
+ t .mu .Lock ()
307
+ defer t .mu .Unlock ()
308
+ for _ , c := range t .ctxList {
309
+ c .cancel ()
310
+ }
289
311
err := t .clientset .CoordinationV1 ().Leases ("kube-system" ).Delete (context .TODO (), "leader-election-controller" , metav1.DeleteOptions {})
290
312
if err != nil && ! apierrors .IsNotFound (err ) {
291
313
t .t .Error (err )
292
314
}
293
- for _ , c := range t .ctxList {
294
- c .cancel ()
295
- }
296
315
}
297
316
298
- func setupCLE (config * rest.Config , ctx context.Context , cancel func (), t * testing.T ) cleTest {
317
+ func setupCLE (config * rest.Config , ctx context.Context , cancel func (), t * testing.T ) * cleTest {
299
318
clientset , err := kubernetes .NewForConfig (config )
300
319
if err != nil {
301
320
t .Fatal (err )
302
321
}
303
322
304
323
a := ctxCancelPair {ctx , cancel }
305
- return cleTest {
324
+ return & cleTest {
306
325
config : config ,
307
326
clientset : clientset ,
308
327
ctxList : map [string ]ctxCancelPair {"main" : a },
0 commit comments