@@ -109,6 +109,7 @@ func TestSchedule(t *testing.T) {
109
109
},
110
110
},
111
111
},
112
+ MutatedHeaders : make (map [string ]string ),
112
113
},
113
114
},
114
115
{
@@ -172,6 +173,7 @@ func TestSchedule(t *testing.T) {
172
173
},
173
174
},
174
175
},
176
+ MutatedHeaders : make (map [string ]string ),
175
177
},
176
178
},
177
179
{
@@ -242,30 +244,41 @@ func TestSchedule(t *testing.T) {
242
244
243
245
func TestSchedulePlugins (t * testing.T ) {
244
246
tp1 := & TestPlugin {
245
- NameRes : "test1" ,
246
- ScoreRes : 0.3 ,
247
- FilterRes : []k8stypes.NamespacedName {{Name : "pod1" }, {Name : "pod2" }, {Name : "pod3" }},
247
+ NameRes : "test1" ,
248
+ ScoreRes : 0.3 ,
249
+ FilterRes : []k8stypes.NamespacedName {{Name : "pod1" }, {Name : "pod2" }, {Name : "pod3" }},
250
+ ReceivedRequestHeaders : make (map [string ]string ),
248
251
}
249
252
tp2 := & TestPlugin {
250
- NameRes : "test2" ,
251
- ScoreRes : 0.8 ,
252
- FilterRes : []k8stypes.NamespacedName {{Name : "pod1" }, {Name : "pod2" }},
253
+ NameRes : "test2" ,
254
+ ScoreRes : 0.8 ,
255
+ FilterRes : []k8stypes.NamespacedName {{Name : "pod1" }, {Name : "pod2" }},
256
+ ReceivedRequestHeaders : make (map [string ]string ),
253
257
}
254
258
tp_filterAll := & TestPlugin {
255
- NameRes : "filter all" ,
256
- FilterRes : []k8stypes.NamespacedName {},
259
+ NameRes : "filter all" ,
260
+ FilterRes : []k8stypes.NamespacedName {},
261
+ ReceivedRequestHeaders : make (map [string ]string ),
262
+ }
263
+ tp_headers := & TestPlugin {
264
+ NameRes : "headers" ,
265
+ FilterRes : []k8stypes.NamespacedName {{Name : "pod1" }, {Name : "pod2" }},
266
+ ExtraHeaders : map [string ]string {"x-unit-test" : "test 1 2 3" },
267
+ ReceivedRequestHeaders : make (map [string ]string ),
257
268
}
258
269
pickerPlugin := & TestPlugin {
259
270
NameRes : "picker" ,
260
271
PickRes : k8stypes.NamespacedName {Name : "pod1" },
261
272
}
262
273
263
274
tests := []struct {
264
- name string
265
- config SchedulerConfig
266
- input []* backendmetrics.FakePodMetrics
267
- wantTargetPod k8stypes.NamespacedName
268
- targetPodScore float64
275
+ name string
276
+ config SchedulerConfig
277
+ input []* backendmetrics.FakePodMetrics
278
+ requestHeaders map [string ]string
279
+ wantTargetPod k8stypes.NamespacedName
280
+ wantMutatedHeaders map [string ]string
281
+ targetPodScore float64
269
282
// Number of expected pods to score (after filter)
270
283
numPodsToScore int
271
284
err bool
@@ -287,10 +300,11 @@ func TestSchedulePlugins(t *testing.T) {
287
300
{Pod : & backend.Pod {NamespacedName : k8stypes.NamespacedName {Name : "pod2" }}},
288
301
{Pod : & backend.Pod {NamespacedName : k8stypes.NamespacedName {Name : "pod3" }}},
289
302
},
290
- wantTargetPod : k8stypes.NamespacedName {Name : "pod1" },
291
- targetPodScore : 1.1 ,
292
- numPodsToScore : 2 ,
293
- err : false ,
303
+ wantTargetPod : k8stypes.NamespacedName {Name : "pod1" },
304
+ wantMutatedHeaders : make (map [string ]string ),
305
+ targetPodScore : 1.1 ,
306
+ numPodsToScore : 2 ,
307
+ err : false ,
294
308
},
295
309
{
296
310
name : "all plugins executed successfully, different scorers weights" ,
@@ -309,10 +323,11 @@ func TestSchedulePlugins(t *testing.T) {
309
323
{Pod : & backend.Pod {NamespacedName : k8stypes.NamespacedName {Name : "pod2" }}},
310
324
{Pod : & backend.Pod {NamespacedName : k8stypes.NamespacedName {Name : "pod3" }}},
311
325
},
312
- wantTargetPod : k8stypes.NamespacedName {Name : "pod1" },
313
- targetPodScore : 50 ,
314
- numPodsToScore : 2 ,
315
- err : false ,
326
+ wantTargetPod : k8stypes.NamespacedName {Name : "pod1" },
327
+ wantMutatedHeaders : make (map [string ]string ),
328
+ targetPodScore : 50 ,
329
+ numPodsToScore : 2 ,
330
+ err : false ,
316
331
},
317
332
{
318
333
name : "filter all" ,
@@ -334,6 +349,33 @@ func TestSchedulePlugins(t *testing.T) {
334
349
numPodsToScore : 0 ,
335
350
err : true , // no available pods to server after filter all
336
351
},
352
+ {
353
+ name : "Mutate a header" ,
354
+ config : SchedulerConfig {
355
+ preSchedulePlugins : []plugins.PreSchedule {tp1 , tp2 },
356
+ filters : []plugins.Filter {tp_headers },
357
+ scorers : map [plugins.Scorer ]int {
358
+ tp1 : 1 ,
359
+ tp2 : 1 ,
360
+ },
361
+ picker : pickerPlugin ,
362
+ postSchedulePlugins : []plugins.PostSchedule {tp1 , tp2 },
363
+ },
364
+ input : []* backendmetrics.FakePodMetrics {
365
+ {Pod : & backend.Pod {NamespacedName : k8stypes.NamespacedName {Name : "pod1" }}},
366
+ {Pod : & backend.Pod {NamespacedName : k8stypes.NamespacedName {Name : "pod2" }}},
367
+ {Pod : & backend.Pod {NamespacedName : k8stypes.NamespacedName {Name : "pod3" }}},
368
+ },
369
+ requestHeaders : map [string ]string {
370
+ "Content-type" : "application/json" ,
371
+ "x-session-id" : "qazw-edcr-tgby-nhyu" ,
372
+ },
373
+ wantTargetPod : k8stypes.NamespacedName {Name : "pod1" },
374
+ wantMutatedHeaders : map [string ]string {"x-unit-test" : "test 1 2 3" },
375
+ targetPodScore : 1.1 ,
376
+ numPodsToScore : 2 ,
377
+ err : false , // no available pods to server after filter all
378
+ },
337
379
}
338
380
339
381
for _ , test := range tests {
@@ -372,7 +414,10 @@ func TestSchedulePlugins(t *testing.T) {
372
414
wantPod := & types.PodMetrics {
373
415
Pod : & backend.Pod {NamespacedName : test .wantTargetPod },
374
416
}
375
- wantRes := & types.Result {TargetPod : wantPod }
417
+ wantRes := & types.Result {
418
+ TargetPod : wantPod ,
419
+ MutatedHeaders : test .wantMutatedHeaders ,
420
+ }
376
421
if diff := cmp .Diff (wantRes , got ); diff != "" {
377
422
t .Errorf ("Unexpected output (-want +got): %v" , diff )
378
423
}
@@ -437,18 +482,20 @@ func (fds *fakeDataStore) PodGetAll() []backendmetrics.PodMetrics {
437
482
438
483
// TestPlugin is an implementation useful in unit tests.
439
484
type TestPlugin struct {
440
- NameRes string
441
- ScoreCallCount int
442
- NumOfScoredPods int
443
- ScoreRes float64
444
- FilterCallCount int
445
- FilterRes []k8stypes.NamespacedName
446
- PreScheduleCallCount int
447
- PostScheduleCallCount int
448
- PickCallCount int
449
- NumOfPickerCandidates int
450
- PickRes k8stypes.NamespacedName
451
- WinnderPodScore float64
485
+ NameRes string
486
+ ScoreCallCount int
487
+ NumOfScoredPods int
488
+ ScoreRes float64
489
+ FilterCallCount int
490
+ FilterRes []k8stypes.NamespacedName
491
+ PreScheduleCallCount int
492
+ PostScheduleCallCount int
493
+ PickCallCount int
494
+ NumOfPickerCandidates int
495
+ PickRes k8stypes.NamespacedName
496
+ WinnderPodScore float64
497
+ ExtraHeaders map [string ]string
498
+ ReceivedRequestHeaders map [string ]string
452
499
}
453
500
454
501
func (tp * TestPlugin ) Name () string { return tp .NameRes }
@@ -459,6 +506,12 @@ func (tp *TestPlugin) PreSchedule(ctx *types.SchedulingContext) {
459
506
460
507
func (tp * TestPlugin ) Filter (ctx * types.SchedulingContext , pods []types.Pod ) []types.Pod {
461
508
tp .FilterCallCount ++
509
+ for key , value := range tp .ExtraHeaders {
510
+ ctx .MutatedHeaders [key ] = value
511
+ }
512
+ for key , value := range ctx .Req .Headers {
513
+ tp .ReceivedRequestHeaders [key ] = value
514
+ }
462
515
return findPods (ctx , tp .FilterRes ... )
463
516
464
517
}
0 commit comments