@@ -26,7 +26,7 @@ import (
26
26
"k8s.io/utils/ptr"
27
27
)
28
28
29
- func TestReconcileHints_trafficDistribution_is_PreferClose (t * testing.T ) {
29
+ func TestReconcileHints (t * testing.T ) {
30
30
testCases := []struct {
31
31
name string
32
32
@@ -40,19 +40,22 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
40
40
wantSlicesUnchanged []* discoveryv1.EndpointSlice
41
41
}{
42
42
{
43
- name : "should set same zone hints" ,
43
+ name : "should set zone hints with PreferClose" ,
44
+
44
45
trafficDistribution : ptr .To (corev1 .ServiceTrafficDistributionPreferClose ),
45
46
slicesToCreate : []* discoveryv1.EndpointSlice {
46
47
{
47
48
Endpoints : []discoveryv1.Endpoint {
48
49
{
49
50
Addresses : []string {"10.0.0.1" },
50
51
Zone : ptr .To ("zone-a" ),
52
+ NodeName : ptr .To ("node-1" ),
51
53
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
52
54
},
53
55
{
54
56
Addresses : []string {"10.0.0.2" },
55
57
Zone : ptr .To ("zone-b" ),
58
+ NodeName : ptr .To ("node-2" ),
56
59
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
57
60
},
58
61
},
@@ -62,11 +65,13 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
62
65
{
63
66
Addresses : []string {"10.0.0.3" },
64
67
Zone : ptr .To ("zone-a" ),
68
+ NodeName : ptr .To ("node-3" ),
65
69
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
66
70
},
67
71
{
68
72
Addresses : []string {"10.0.0.4" },
69
73
Zone : ptr .To ("zone-b" ),
74
+ NodeName : ptr .To ("node-4" ),
70
75
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
71
76
},
72
77
},
@@ -78,11 +83,13 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
78
83
{
79
84
Addresses : []string {"10.0.0.5" },
80
85
Zone : ptr .To ("zone-a" ),
86
+ NodeName : ptr .To ("node-5" ),
81
87
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
82
88
},
83
89
{
84
90
Addresses : []string {"10.0.0.6" },
85
91
Zone : ptr .To ("zone-a" ),
92
+ NodeName : ptr .To ("node-6" ),
86
93
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
87
94
},
88
95
},
@@ -92,11 +99,13 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
92
99
{
93
100
Addresses : []string {"10.0.0.7" },
94
101
Zone : ptr .To ("zone-b" ),
102
+ NodeName : ptr .To ("node-7" ),
95
103
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
96
104
},
97
105
{
98
106
Addresses : []string {"10.0.0.8" },
99
107
Zone : ptr .To ("zone-c" ),
108
+ NodeName : ptr .To ("node-8" ),
100
109
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
101
110
},
102
111
},
@@ -108,12 +117,14 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
108
117
{
109
118
Addresses : []string {"10.0.0.1" },
110
119
Zone : ptr .To ("zone-a" ),
120
+ NodeName : ptr .To ("node-1" ),
111
121
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
112
122
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-a" }}},
113
123
},
114
124
{
115
125
Addresses : []string {"10.0.0.2" },
116
126
Zone : ptr .To ("zone-b" ),
127
+ NodeName : ptr .To ("node-2" ),
117
128
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
118
129
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-b" }}},
119
130
},
@@ -124,12 +135,14 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
124
135
{
125
136
Addresses : []string {"10.0.0.3" },
126
137
Zone : ptr .To ("zone-a" ),
138
+ NodeName : ptr .To ("node-3" ),
127
139
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
128
140
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-a" }}},
129
141
},
130
142
{
131
143
Addresses : []string {"10.0.0.4" },
132
144
Zone : ptr .To ("zone-b" ),
145
+ NodeName : ptr .To ("node-4" ),
133
146
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
134
147
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-b" }}},
135
148
},
@@ -142,12 +155,14 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
142
155
{
143
156
Addresses : []string {"10.0.0.5" },
144
157
Zone : ptr .To ("zone-a" ),
158
+ NodeName : ptr .To ("node-5" ),
145
159
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
146
160
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-a" }}},
147
161
},
148
162
{
149
163
Addresses : []string {"10.0.0.6" },
150
164
Zone : ptr .To ("zone-a" ),
165
+ NodeName : ptr .To ("node-6" ),
151
166
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
152
167
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-a" }}},
153
168
},
@@ -158,12 +173,14 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
158
173
{
159
174
Addresses : []string {"10.0.0.7" },
160
175
Zone : ptr .To ("zone-b" ),
176
+ NodeName : ptr .To ("node-7" ),
161
177
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
162
178
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-b" }}},
163
179
},
164
180
{
165
181
Addresses : []string {"10.0.0.8" },
166
182
Zone : ptr .To ("zone-c" ),
183
+ NodeName : ptr .To ("node-8" ),
167
184
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
168
185
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-c" }}},
169
186
},
@@ -172,14 +189,16 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
172
189
},
173
190
},
174
191
{
175
- name : "incorrect hints should be corrected" ,
192
+ name : "should correct incorrect hints with PreferClose" ,
193
+
176
194
trafficDistribution : ptr .To (corev1 .ServiceTrafficDistributionPreferClose ),
177
195
slicesToUpdate : []* discoveryv1.EndpointSlice {
178
196
{
179
197
Endpoints : []discoveryv1.Endpoint {
180
198
{
181
199
Addresses : []string {"10.0.0.1" },
182
200
Zone : ptr .To ("zone-a" ),
201
+ NodeName : ptr .To ("node-1" ),
183
202
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
184
203
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-b" }}}, // incorrect hint as per new heuristic
185
204
},
@@ -192,6 +211,7 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
192
211
{
193
212
Addresses : []string {"10.0.0.2" },
194
213
Zone : ptr .To ("zone-b" ),
214
+ NodeName : ptr .To ("node-2" ),
195
215
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
196
216
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-c" }}},
197
217
},
@@ -202,6 +222,7 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
202
222
{
203
223
Addresses : []string {"10.0.0.3" },
204
224
Zone : ptr .To ("zone-c" ),
225
+ NodeName : ptr .To ("node-3" ),
205
226
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
206
227
},
207
228
},
@@ -213,6 +234,7 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
213
234
{
214
235
Addresses : []string {"10.0.0.1" },
215
236
Zone : ptr .To ("zone-a" ),
237
+ NodeName : ptr .To ("node-1" ),
216
238
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
217
239
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-a" }}},
218
240
},
@@ -223,6 +245,7 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
223
245
{
224
246
Addresses : []string {"10.0.0.2" },
225
247
Zone : ptr .To ("zone-b" ),
248
+ NodeName : ptr .To ("node-2" ),
226
249
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
227
250
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-b" }}},
228
251
},
@@ -233,6 +256,7 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
233
256
{
234
257
Addresses : []string {"10.0.0.3" },
235
258
Zone : ptr .To ("zone-c" ),
259
+ NodeName : ptr .To ("node-3" ),
236
260
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
237
261
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-c" }}},
238
262
},
@@ -241,14 +265,16 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
241
265
},
242
266
},
243
267
{
244
- name : "unready endpoints should not trigger updates" ,
268
+ name : "unready endpoints should not trigger updates" ,
269
+
245
270
trafficDistribution : ptr .To (corev1 .ServiceTrafficDistributionPreferClose ),
246
271
slicesUnchanged : []* discoveryv1.EndpointSlice {
247
272
{
248
273
Endpoints : []discoveryv1.Endpoint {
249
274
{
250
275
Addresses : []string {"10.0.0.2" },
251
276
Zone : ptr .To ("zone-b" ),
277
+ NodeName : ptr .To ("node-2" ),
252
278
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (false )}, // endpoint is not ready
253
279
},
254
280
},
@@ -260,59 +286,31 @@ func TestReconcileHints_trafficDistribution_is_PreferClose(t *testing.T) {
260
286
{
261
287
Addresses : []string {"10.0.0.2" },
262
288
Zone : ptr .To ("zone-b" ),
289
+ NodeName : ptr .To ("node-2" ),
263
290
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (false )},
264
291
},
265
292
},
266
293
},
267
294
},
268
295
},
269
- }
270
-
271
- for _ , tc := range testCases {
272
- t .Run (tc .name , func (t * testing.T ) {
273
- gotSlicesToCreate , gotSlicesToUpdate , gotSlicesUnchanged := ReconcileHints (tc .trafficDistribution , tc .slicesToCreate , tc .slicesToUpdate , tc .slicesUnchanged )
274
-
275
- if diff := cmp .Diff (tc .wantSlicesToCreate , gotSlicesToCreate , cmpopts .EquateEmpty ()); diff != "" {
276
- t .Errorf ("ReconcileHints(...) returned unexpected diff in 'slicesToCreate': (-want, +got)\n %v" , diff )
277
- }
278
- if diff := cmp .Diff (tc .wantSlicesToUpdate , gotSlicesToUpdate , cmpopts .EquateEmpty ()); diff != "" {
279
- t .Errorf ("ReconcileHints(...) returned unexpected diff in 'slicesToUpdate': (-want, +got)\n %v" , diff )
280
- }
281
- if diff := cmp .Diff (tc .wantSlicesUnchanged , gotSlicesUnchanged , cmpopts .EquateEmpty ()); diff != "" {
282
- t .Errorf ("ReconcileHints(...) returned unexpected diff in 'slicesUnchanged': (-want, +got)\n %v" , diff )
283
- }
284
- })
285
- }
286
- }
287
-
288
- func TestReconcileHints_trafficDistribution_is_nil_or_empty (t * testing.T ) {
289
- testCases := []struct {
290
- name string
291
-
292
- trafficDistribution * string
293
- slicesToCreate []* discoveryv1.EndpointSlice
294
- slicesToUpdate []* discoveryv1.EndpointSlice
295
- slicesUnchanged []* discoveryv1.EndpointSlice
296
-
297
- wantSlicesToCreate []* discoveryv1.EndpointSlice
298
- wantSlicesToUpdate []* discoveryv1.EndpointSlice
299
- wantSlicesUnchanged []* discoveryv1.EndpointSlice
300
- }{
301
296
{
302
- name : "trafficDistribution='' should remove zone hints" ,
303
- trafficDistribution : ptr .To ("" ),
297
+ name : "should remove hints when trafficDistribution is unrecognized" ,
298
+
299
+ trafficDistribution : ptr .To ("Unknown" ),
304
300
slicesToCreate : []* discoveryv1.EndpointSlice {
305
301
{
306
302
Endpoints : []discoveryv1.Endpoint {
307
303
{
308
304
Addresses : []string {"10.0.0.1" },
309
305
Zone : ptr .To ("zone-a" ),
306
+ NodeName : ptr .To ("node-1" ),
310
307
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
311
308
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-a" }}},
312
309
},
313
310
{
314
311
Addresses : []string {"10.0.0.2" },
315
312
Zone : ptr .To ("zone-b" ),
313
+ NodeName : ptr .To ("node-2" ),
316
314
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
317
315
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-b" }}},
318
316
},
@@ -323,6 +321,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
323
321
{
324
322
Addresses : []string {"10.0.0.3" },
325
323
Zone : ptr .To ("zone-a" ),
324
+ NodeName : ptr .To ("node-3" ),
326
325
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
327
326
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-a" }}},
328
327
},
@@ -335,6 +334,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
335
334
{
336
335
Addresses : []string {"10.0.0.5" },
337
336
Zone : ptr .To ("zone-a" ),
337
+ NodeName : ptr .To ("node-5" ),
338
338
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
339
339
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-a" }}},
340
340
},
@@ -347,11 +347,13 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
347
347
{
348
348
Addresses : []string {"10.0.0.1" },
349
349
Zone : ptr .To ("zone-a" ),
350
+ NodeName : ptr .To ("node-1" ),
350
351
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
351
352
},
352
353
{
353
354
Addresses : []string {"10.0.0.2" },
354
355
Zone : ptr .To ("zone-b" ),
356
+ NodeName : ptr .To ("node-2" ),
355
357
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
356
358
},
357
359
},
@@ -361,6 +363,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
361
363
{
362
364
Addresses : []string {"10.0.0.3" },
363
365
Zone : ptr .To ("zone-a" ),
366
+ NodeName : ptr .To ("node-3" ),
364
367
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
365
368
},
366
369
},
@@ -372,21 +375,24 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
372
375
{
373
376
Addresses : []string {"10.0.0.5" },
374
377
Zone : ptr .To ("zone-a" ),
378
+ NodeName : ptr .To ("node-5" ),
375
379
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
376
380
},
377
381
},
378
382
},
379
383
},
380
384
},
381
385
{
382
- name : "trafficDistribution=nil should remove zone hints" ,
386
+ name : "should remove hints when trafficDistribution is unset" ,
387
+
383
388
trafficDistribution : nil ,
384
389
slicesToUpdate : []* discoveryv1.EndpointSlice {
385
390
{
386
391
Endpoints : []discoveryv1.Endpoint {
387
392
{
388
393
Addresses : []string {"10.0.0.5" },
389
394
Zone : ptr .To ("zone-a" ),
395
+ NodeName : ptr .To ("node-5" ),
390
396
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
391
397
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-a" }}},
392
398
},
@@ -399,6 +405,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
399
405
{
400
406
Addresses : []string {"10.0.0.6" },
401
407
Zone : ptr .To ("zone-b" ),
408
+ NodeName : ptr .To ("node-6" ),
402
409
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
403
410
Hints : & discoveryv1.EndpointHints {ForZones : []discoveryv1.ForZone {{Name : "zone-b" }}},
404
411
},
@@ -411,6 +418,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
411
418
{
412
419
Addresses : []string {"10.0.0.5" },
413
420
Zone : ptr .To ("zone-a" ),
421
+ NodeName : ptr .To ("node-5" ),
414
422
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
415
423
},
416
424
},
@@ -420,6 +428,7 @@ func TestReconcileHints_trafficDistribution_is_nil_or_empty(t *testing.T) {
420
428
{
421
429
Addresses : []string {"10.0.0.6" },
422
430
Zone : ptr .To ("zone-b" ),
431
+ NodeName : ptr .To ("node-6" ),
423
432
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
424
433
},
425
434
},
@@ -452,6 +461,7 @@ func TestReconcileHints_doesNotMutateUnchangedSlices(t *testing.T) {
452
461
{
453
462
Addresses : []string {"10.0.0.1" },
454
463
Zone : ptr .To ("zone-a" ),
464
+ NodeName : ptr .To ("node-1" ),
455
465
Conditions : discoveryv1.EndpointConditions {Ready : ptr .To (true )},
456
466
},
457
467
},
0 commit comments