This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
scheduler_tables.sql
617 lines (568 loc) · 21.9 KB
/
scheduler_tables.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
create table node_info
(
name varchar(36) not null primary key,
unschedulable boolean not null,
out_of_disk boolean not null,
memory_pressure boolean not null,
disk_pressure boolean not null,
pid_pressure boolean not null,
ready boolean not null,
network_unavailable boolean not null,
cpu_capacity bigint not null,
memory_capacity bigint not null,
ephemeral_storage_capacity bigint not null,
pods_capacity bigint not null,
cpu_allocatable bigint not null,
memory_allocatable bigint not null,
ephemeral_storage_allocatable bigint not null,
pods_allocatable bigint not null,
cpu_allocated bigint not null,
memory_allocated bigint not null,
ephemeral_storage_allocated bigint not null,
pods_allocated bigint not null
);
create table pod_info
(
pod_name varchar(100) not null primary key,
status varchar(36) not null,
node_name varchar(36) null,
namespace varchar(100) not null,
cpu_request bigint not null,
memory_request bigint not null,
ephemeral_storage_request bigint not null,
pods_request bigint not null,
owner_name varchar(100) not null,
creation_timestamp varchar(100) not null,
priority integer not null,
schedulerName varchar(50),
has_node_selector_labels boolean not null,
has_pod_affinity_requirements boolean not null,
has_pod_anti_affinity_requirements boolean not null,
equivalence_class bigint not null,
qos_class varchar(10) not null,
resourceVersion bigint not null
);
-- This table tracks the "ContainerPorts" fields of each pod.
-- It is used to enforce the PodFitsHostPorts constraint.
create table pod_ports_request
(
pod_name varchar(100) not null,
host_ip varchar(100) not null,
host_port integer not null,
host_protocol varchar(10) not null,
foreign key(pod_name) references pod_info(pod_name) on delete cascade
);
-- This table tracks the set of hostports in use at each node.
-- Also used to enforce the PodFitsHostPorts constraint.
create table container_host_ports
(
pod_name varchar(100) not null,
node_name varchar(36) not null,
host_ip varchar(100) not null,
host_port integer not null,
host_protocol varchar(10) not null,
foreign key(pod_name) references pod_info(pod_name) on delete cascade,
foreign key(node_name) references node_info(name) on delete cascade
);
-- Tracks the set of node selector labels per pod.
create table pod_node_selector_labels
(
pod_name varchar(100) not null,
term integer not null,
match_expression integer not null,
num_match_expressions integer not null,
label_key varchar(100) not null,
label_operator varchar(12) not null,
label_value varchar(36) null,
foreign key(pod_name) references pod_info(pod_name) on delete cascade
);
-- Tracks the set of pod affinity match expressions.
create table pod_affinity_match_expressions
(
pod_name varchar(100) not null,
label_selector integer not null,
match_expression integer not null,
num_match_expressions integer not null,
label_key varchar(100) not null,
label_operator varchar(30) not null,
label_value array not null,
topology_key varchar(100) not null,
foreign key(pod_name) references pod_info(pod_name) on delete cascade
);
-- Tracks the set of pod anti-affinity match expressions.
create table pod_anti_affinity_match_expressions
(
pod_name varchar(100) not null,
label_selector integer not null,
match_expression integer not null,
num_match_expressions integer not null,
label_key varchar(100) not null,
label_operator varchar(30) not null,
label_value array not null,
topology_key varchar(100) not null,
foreign key(pod_name) references pod_info(pod_name) on delete cascade
);
-- Tracks the set of labels per pod, and indicates if
-- any of them are also node selector labels
create table pod_labels
(
pod_name varchar(100) not null,
label_key varchar(100) not null,
label_value varchar(36) not null,
foreign key(pod_name) references pod_info(pod_name) on delete cascade,
primary key(pod_name, label_key, label_value)
);
-- Tracks the set of labels per node
create table node_labels
(
node_name varchar(36) not null,
label_key varchar(100) not null,
label_value varchar(36) not null,
foreign key(node_name) references node_info(name) on delete cascade
);
-- Volume labels
create table volume_labels
(
volume_name varchar(36) not null,
pod_name varchar(100) not null,
label_key varchar(100) not null,
label_value varchar(36) not null,
foreign key(pod_name) references pod_info(pod_name) on delete cascade
);
-- For pods that have ports exposed
create table pod_by_service
(
pod_name varchar(100) not null,
service_name varchar(100) not null,
foreign key(pod_name) references pod_info(pod_name) on delete cascade
);
-- Service affinity labels
create table service_affinity_labels
(
label_key varchar(100) not null
);
-- Labels present on node
create table labels_to_check_for_presence
(
label_key varchar(100) not null,
present boolean not null
);
-- Node taints
create table node_taints
(
node_name varchar(36) not null,
taint_key varchar(100) not null,
taint_value varchar(100),
taint_effect varchar(100) not null,
foreign key(node_name) references node_info(name) on delete cascade
);
-- Pod taints.
create table pod_tolerations
(
pod_name varchar(100) not null,
tolerations_key varchar(100),
tolerations_value varchar(100),
tolerations_effect varchar(100),
tolerations_operator varchar(100),
foreign key(pod_name) references pod_info(pod_name) on delete cascade
);
-- Tracks the set of node images that are already
-- available at a node
create table node_images
(
node_name varchar(36) not null,
image_name varchar(200) not null,
image_size bigint not null,
foreign key(node_name) references node_info(name) on delete cascade
);
-- Tracks the container images required by each pod
create table pod_images
(
pod_name varchar(100) not null,
image_name varchar(200) not null,
foreign key(pod_name) references pod_info(pod_name) on delete cascade
);
-- Select all pods that need to be scheduled.
-- We also indicate boolean values to check whether
-- a pod has node selector or pod affinity labels,
-- and whether pod affinity rules already yields some subset of
-- nodes that we can assign pods to.
create view pods_to_assign_no_limit as
select
pod_name,
status,
node_name as controllable__node_name,
namespace,
cpu_request,
memory_request,
ephemeral_storage_request,
pods_request,
owner_name,
creation_timestamp,
has_node_selector_labels,
has_pod_affinity_requirements,
has_pod_anti_affinity_requirements,
equivalence_class,
qos_class
from pod_info
where status = 'Pending' and node_name is null and schedulerName = 'dcm-scheduler';
create view assigned_pods as
select
pod_name,
status,
node_name,
namespace,
cpu_request,
memory_request,
ephemeral_storage_request,
pods_request,
owner_name,
creation_timestamp,
has_node_selector_labels,
has_pod_affinity_requirements,
has_pod_anti_affinity_requirements,
equivalence_class,
qos_class
from pod_info
where node_name is not null;
-- This view is updated dynamically to change the limit. This
-- pattern is required because there is no clean way to enforce
-- a dynamic "LIMIT" clause.
create table batch_size
(
pendingPodsLimit integer not null primary key
);
create view pods_to_assign as
select * from pods_to_assign_no_limit limit 50;
-- Pods with port requests
create view pods_with_port_requests as
select pods_to_assign.controllable__node_name as controllable__node_name,
pod_ports_request.host_port as host_port,
pod_ports_request.host_ip as host_ip,
pod_ports_request.host_protocol as host_protocol
from pods_to_assign
join pod_ports_request
on pod_ports_request.pod_name = pods_to_assign.pod_name;
-- Pod node selectors
create view pod_node_selector_matches as
select pods_to_assign.pod_name as pod_name,
node_labels.node_name as node_name
from pods_to_assign
join pod_node_selector_labels
on pods_to_assign.pod_name = pod_node_selector_labels.pod_name
join node_labels
on
(pod_node_selector_labels.label_operator = 'In'
and pod_node_selector_labels.label_key = node_labels.label_key
and pod_node_selector_labels.label_value = node_labels.label_value)
or (pod_node_selector_labels.label_operator = 'Exists'
and pod_node_selector_labels.label_key = node_labels.label_key)
or (pod_node_selector_labels.label_operator = 'NotIn')
or (pod_node_selector_labels.label_operator = 'DoesNotExist')
where pods_to_assign.has_node_selector_labels = true
group by pods_to_assign.pod_name, node_labels.node_name, pod_node_selector_labels.term,
pod_node_selector_labels.label_operator, pod_node_selector_labels.num_match_expressions
having case pod_node_selector_labels.label_operator
when 'NotIn'
then not(any(pod_node_selector_labels.label_key = node_labels.label_key
and pod_node_selector_labels.label_value = node_labels.label_value))
when 'DoesNotExist'
then not(any(pod_node_selector_labels.label_key = node_labels.label_key))
else count(distinct match_expression) = pod_node_selector_labels.num_match_expressions
end;
create index pod_info_idx on pod_info (status, node_name);
create index pod_node_selector_labels_fk_idx on pod_node_selector_labels (pod_name);
create index node_labels_idx on node_labels (label_key, label_value);
-- Inter pod affinity
create view inter_pod_affinity_matches_inner_exists_pending as
select
pods_to_assign_A.pod_name as pods_to_assign_pod_name,
pod_labels.pod_name as pod_labels_pod_name,
pod_affinity_match_expressions.label_selector,
pod_affinity_match_expressions.topology_key,
pod_affinity_match_expressions.label_operator,
pod_affinity_match_expressions.num_match_expressions,
pod_affinity_match_expressions.match_expression,
pods_to_assign_B.controllable__node_name as node_name
from
pods_to_assign as pods_to_assign_A
join pod_affinity_match_expressions on pods_to_assign_A.pod_name = pod_affinity_match_expressions.pod_name
join pod_labels on (
pod_affinity_match_expressions.label_operator = 'Exists'
and pod_affinity_match_expressions.label_key = pod_labels.label_key
)
join pods_to_assign as pods_to_assign_B on pod_labels.pod_name = pods_to_assign_B.pod_name
where pods_to_assign_A.has_pod_affinity_requirements = true;
create view inter_pod_affinity_matches_inner_exists_scheduled as
select
pods_to_assign.pod_name as pods_to_assign_pod_name,
pod_labels.pod_name as pod_labels_pod_name,
pod_affinity_match_expressions.label_selector,
pod_affinity_match_expressions.topology_key,
pod_affinity_match_expressions.label_operator,
pod_affinity_match_expressions.num_match_expressions,
pod_affinity_match_expressions.match_expression,
assigned_pods.node_name
from
pods_to_assign
join pod_affinity_match_expressions on pods_to_assign.pod_name = pod_affinity_match_expressions.pod_name
join pod_labels on (
pod_affinity_match_expressions.label_operator = 'Exists'
and pod_affinity_match_expressions.label_key = pod_labels.label_key
)
join assigned_pods on pod_labels.pod_name = assigned_pods.pod_name
where pods_to_assign.has_pod_affinity_requirements = true;
create view inter_pod_affinity_matches_inner_in_pending as
select
pods_to_assign_A.pod_name as pods_to_assign_pod_name,
pod_labels.pod_name as pod_labels_pod_name,
pod_affinity_match_expressions.label_selector,
pod_affinity_match_expressions.topology_key,
pod_affinity_match_expressions.label_operator,
pod_affinity_match_expressions.num_match_expressions,
pod_affinity_match_expressions.match_expression,
pods_to_assign_B.controllable__node_name as node_name
from
pods_to_assign as pods_to_assign_A
join pod_affinity_match_expressions on pods_to_assign_A.pod_name = pod_affinity_match_expressions.pod_name
join pod_labels on (
pod_affinity_match_expressions.label_operator = 'In'
and pod_affinity_match_expressions.label_key = pod_labels.label_key
and pod_labels.label_value in (unnest(pod_affinity_match_expressions.label_value))
)
join pods_to_assign as pods_to_assign_B on pod_labels.pod_name = pods_to_assign_B.pod_name
where pods_to_assign_A.has_pod_affinity_requirements = true;
create view inter_pod_affinity_matches_inner_in_scheduled as
select
pods_to_assign.pod_name as pods_to_assign_pod_name,
pod_labels.pod_name as pod_labels_pod_name,
pod_affinity_match_expressions.label_selector,
pod_affinity_match_expressions.topology_key,
pod_affinity_match_expressions.label_operator,
pod_affinity_match_expressions.num_match_expressions,
pod_affinity_match_expressions.match_expression,
assigned_pods.node_name
from
pods_to_assign
join pod_affinity_match_expressions on pods_to_assign.pod_name = pod_affinity_match_expressions.pod_name
join pod_labels on (
pod_affinity_match_expressions.label_operator = 'In'
and pod_affinity_match_expressions.label_key = pod_labels.label_key
and pod_labels.label_value in (unnest(pod_affinity_match_expressions.label_value))
)
join assigned_pods on pod_labels.pod_name = assigned_pods.pod_name
where pods_to_assign.has_pod_affinity_requirements = true;
create view inter_pod_affinity_matches_inner_pending as
select
pods_to_assign_pod_name as pod_name,
pod_labels_pod_name as matches,
node_name as node_name
from
((select * from inter_pod_affinity_matches_inner_in_pending)
union
(select * from inter_pod_affinity_matches_inner_exists_pending))
group by
pods_to_assign_pod_name,
pod_labels_pod_name,
label_selector,
topology_key,
label_operator,
num_match_expressions,
node_name
having
count(distinct match_expression) = num_match_expressions;
create view inter_pod_affinity_matches_inner_scheduled as
select
pods_to_assign_pod_name as pod_name,
pod_labels_pod_name as matches,
node_name as node_name
from
((select * from inter_pod_affinity_matches_inner_in_scheduled)
union
(select * from inter_pod_affinity_matches_inner_exists_scheduled))
group by
pods_to_assign_pod_name,
pod_labels_pod_name,
label_selector,
topology_key,
label_operator,
num_match_expressions,
node_name
having
count(distinct match_expression) = num_match_expressions;
create view inter_pod_affinity_matches_pending as
select pod_name, array_agg(matches) as matches, num_matches from
(select *, count(*) over (partition by pod_name) as num_matches
from inter_pod_affinity_matches_inner_pending)
where (num_matches = 1 or pod_name != matches)
group by pod_name, num_matches;
create view inter_pod_affinity_matches_scheduled as
select *, count(*) over (partition by pod_name) as num_matches from inter_pod_affinity_matches_inner_scheduled;
create index pod_affinity_match_expressions_idx on pod_affinity_match_expressions (pod_name);
create index pod_anti_affinity_match_expressions_idx on pod_anti_affinity_match_expressions (pod_name);
create index pod_labels_idx on pod_labels (label_key, label_value);
create view inter_pod_anti_affinity_matches_inner_exists_pending as
select
pods_to_assign_A.pod_name as pods_to_assign_pod_name,
pod_labels.pod_name as pod_labels_pod_name,
pod_anti_affinity_match_expressions.label_selector,
pod_anti_affinity_match_expressions.topology_key,
pod_anti_affinity_match_expressions.label_operator,
pod_anti_affinity_match_expressions.num_match_expressions,
pod_anti_affinity_match_expressions.match_expression,
pods_to_assign_B.controllable__node_name as node_name
from
pods_to_assign as pods_to_assign_A
join pod_anti_affinity_match_expressions on pods_to_assign_A.pod_name = pod_anti_affinity_match_expressions.pod_name
join pod_labels on (
pod_anti_affinity_match_expressions.label_operator = 'Exists'
and pod_anti_affinity_match_expressions.label_key = pod_labels.label_key
)
join pods_to_assign as pods_to_assign_B on pod_labels.pod_name = pods_to_assign_B.pod_name
where pods_to_assign_A.has_pod_anti_affinity_requirements = true;
create view inter_pod_anti_affinity_matches_inner_exists_scheduled as
select
pods_to_assign.pod_name as pods_to_assign_pod_name,
pod_labels.pod_name as pod_labels_pod_name,
pod_anti_affinity_match_expressions.label_selector,
pod_anti_affinity_match_expressions.topology_key,
pod_anti_affinity_match_expressions.label_operator,
pod_anti_affinity_match_expressions.num_match_expressions,
pod_anti_affinity_match_expressions.match_expression,
assigned_pods.node_name
from
pods_to_assign
join pod_anti_affinity_match_expressions on pods_to_assign.pod_name = pod_anti_affinity_match_expressions.pod_name
join pod_labels on (
pod_anti_affinity_match_expressions.label_operator = 'Exists'
and pod_anti_affinity_match_expressions.label_key = pod_labels.label_key
)
join assigned_pods on pod_labels.pod_name = assigned_pods.pod_name
where pods_to_assign.has_pod_anti_affinity_requirements = true;
create view inter_pod_anti_affinity_matches_inner_in_pending as
select
pods_to_assign_A.pod_name as pods_to_assign_pod_name,
pod_labels.pod_name as pod_labels_pod_name,
pod_anti_affinity_match_expressions.label_selector,
pod_anti_affinity_match_expressions.topology_key,
pod_anti_affinity_match_expressions.label_operator,
pod_anti_affinity_match_expressions.num_match_expressions,
pod_anti_affinity_match_expressions.match_expression,
pods_to_assign_A.controllable__node_name as node_name
from
pods_to_assign as pods_to_assign_A
join pod_anti_affinity_match_expressions on pods_to_assign_A.pod_name = pod_anti_affinity_match_expressions.pod_name
join pod_labels on (
pod_anti_affinity_match_expressions.label_operator = 'In'
and pod_anti_affinity_match_expressions.label_key = pod_labels.label_key
and pod_labels.label_value in (unnest(pod_anti_affinity_match_expressions.label_value))
)
join pods_to_assign as pods_to_assign_B on pod_labels.pod_name = pods_to_assign_B.pod_name
where pods_to_assign_A.has_pod_anti_affinity_requirements = true;
create view inter_pod_anti_affinity_matches_inner_in_scheduled as
select
pods_to_assign.pod_name as pods_to_assign_pod_name,
pod_labels.pod_name as pod_labels_pod_name,
pod_anti_affinity_match_expressions.label_selector,
pod_anti_affinity_match_expressions.topology_key,
pod_anti_affinity_match_expressions.label_operator,
pod_anti_affinity_match_expressions.num_match_expressions,
pod_anti_affinity_match_expressions.match_expression,
assigned_pods.node_name
from
pods_to_assign
join pod_anti_affinity_match_expressions on pods_to_assign.pod_name = pod_anti_affinity_match_expressions.pod_name
join pod_labels on (
pod_anti_affinity_match_expressions.label_operator = 'In'
and pod_anti_affinity_match_expressions.label_key = pod_labels.label_key
and pod_labels.label_value in (unnest(pod_anti_affinity_match_expressions.label_value))
)
join assigned_pods on pod_labels.pod_name = assigned_pods.pod_name
where pods_to_assign.has_pod_anti_affinity_requirements = true;
create view inter_pod_anti_affinity_matches_inner_pending as
select
pods_to_assign_pod_name as pod_name,
pod_labels_pod_name as matches,
node_name as node_name
from
((select * from inter_pod_anti_affinity_matches_inner_in_pending)
union
(select * from inter_pod_anti_affinity_matches_inner_exists_pending))
group by
pods_to_assign_pod_name,
pod_labels_pod_name,
label_selector,
topology_key,
label_operator,
num_match_expressions,
node_name
having
count(distinct match_expression) = num_match_expressions;
create view inter_pod_anti_affinity_matches_inner_scheduled as
select
pods_to_assign_pod_name as pod_name,
pod_labels_pod_name as matches,
node_name as node_name
from
((select * from inter_pod_anti_affinity_matches_inner_in_scheduled)
union
(select * from inter_pod_anti_affinity_matches_inner_exists_scheduled))
group by
pods_to_assign_pod_name,
pod_labels_pod_name,
label_selector,
topology_key,
label_operator,
num_match_expressions,
node_name
having
count(distinct match_expression) = num_match_expressions;
create view inter_pod_anti_affinity_matches_pending as
select pod_name, array_agg(matches) as matches, num_matches from
(select *, count(*) over (partition by pod_name) as num_matches
from inter_pod_anti_affinity_matches_inner_pending)
where (pod_name != matches)
group by pod_name, num_matches;
create view inter_pod_anti_affinity_matches_scheduled as
select pod_name, array_agg(node_name) as matches, num_matches from
(select *, count(*) over (partition by pod_name) as num_matches from inter_pod_anti_affinity_matches_inner_scheduled)
where (pod_name != matches)
group by pod_name, num_matches;
-- Spare capacity
create view spare_capacity_per_node as
select name as name,
cpu_allocatable - cpu_allocated as cpu_remaining,
memory_allocatable - memory_allocated as memory_remaining,
pods_allocatable - pods_allocated as pods_remaining
from node_info
where unschedulable = false and
memory_pressure = false and
out_of_disk = false and
disk_pressure = false and
pid_pressure = false and
network_unavailable = false and
ready = true and
cpu_allocated < cpu_allocatable and
memory_allocated < memory_allocatable and
pods_allocated < pods_allocatable;
-- Taints and tolerations
create view pods_that_tolerate_node_taints as
select pods_to_assign.pod_name as pod_name,
A.node_name as node_name
from pods_to_assign
join pod_tolerations
on pods_to_assign.pod_name = pod_tolerations.pod_name
join (select *, count(*) over (partition by node_name) as num_taints from node_taints) as A
on pod_tolerations.tolerations_key = A.taint_key
and (pod_tolerations.tolerations_effect = null
or pod_tolerations.tolerations_effect = A.taint_effect)
and (pod_tolerations.tolerations_operator = 'Exists'
or pod_tolerations.tolerations_value = A.taint_value)
group by pod_tolerations.pod_name, A.node_name, A.num_taints
having count(*) = A.num_taints;
create view nodes_that_have_tolerations as
select distinct node_name from node_taints;
-- Avoid overloaded nodes or nodes that report being under resource pressure
create view allowed_nodes as
select name
from spare_capacity_per_node;