/
service_detail.sp
763 lines (667 loc) · 14.8 KB
/
service_detail.sp
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
dashboard "service_detail" {
title = "Kubernetes Service Detail"
documentation = file("./dashboards/service/docs/service_detail.md")
tags = merge(local.service_common_tags, {
type = "Detail"
})
input "service_uid" {
title = "Select a Service:"
query = query.service_input
width = 4
}
container {
card {
width = 3
query = query.service_type
args = {
uid = self.input.service_uid.value
}
}
card {
width = 3
query = query.service_default_namespace
args = {
uid = self.input.service_uid.value
}
href = "/kubernetes_insights.dashboard.namespace_detail?input.namespace_uid={{.'UID' | @uri}}"
}
}
with "ingresses_for_service" {
query = query.ingresses_for_service
args = [self.input.service_uid.value]
}
with "pods_for_service" {
query = query.pods_for_service
args = [self.input.service_uid.value]
}
with "deployments_for_service" {
query = query.deployments_for_service
args = [self.input.service_uid.value]
}
with "replicasets_for_service" {
query = query.replicasets_for_service
args = [self.input.service_uid.value]
}
with "statefulsets_for_service" {
query = query.statefulsets_for_service
args = [self.input.service_uid.value]
}
container {
graph {
title = "Relationships"
type = "graph"
direction = "TD"
node {
base = node.service
args = {
service_uids = [self.input.service_uid.value]
}
}
node {
base = node.service_load_balancer
args = {
service_uids = [self.input.service_uid.value]
}
}
node {
base = node.ingress_rule
args = {
ingress_uids = with.ingresses_for_service.rows[*].uid
}
}
node {
base = node.ingress
args = {
ingress_uids = with.ingresses_for_service.rows[*].uid
}
}
node {
base = node.deployment
args = {
deployment_uids = with.deployments_for_service.rows[*].uid
}
}
node {
base = node.replicaset
args = {
replicaset_uids = with.replicasets_for_service.rows[*].uid
}
}
node {
base = node.statefulset
args = {
statefulset_uids = with.statefulsets_for_service.rows[*].uid
}
}
node {
base = node.pod
args = {
pod_uids = with.pods_for_service.rows[*].uid
}
}
node {
base = node.ingress_load_balancer
args = {
ingress_uids = with.ingresses_for_service.rows[*].uid
}
}
edge {
base = edge.ingress_load_balancer_to_ingress
args = {
ingress_uids = with.ingresses_for_service.rows[*].uid
}
}
edge {
base = edge.ingress_to_ingress_rule
args = {
ingress_uids = with.ingresses_for_service.rows[*].uid
}
}
edge {
base = edge.ingress_rule_to_service
args = {
ingress_uids = with.ingresses_for_service.rows[*].uid
}
}
edge {
base = edge.service_load_balancer_to_service
args = {
service_uids = [self.input.service_uid.value]
}
}
edge {
base = edge.service_to_deployment
args = {
service_uids = [self.input.service_uid.value]
}
}
edge {
base = edge.deployment_to_replicaset
args = {
deployment_uids = with.deployments_for_service.rows[*].uid
}
}
edge {
base = edge.service_to_statefulset
args = {
service_uids = [self.input.service_uid.value]
}
}
edge {
base = edge.replicaset_to_pod
args = {
replicaset_uids = with.replicasets_for_service.rows[*].uid
}
}
edge {
base = edge.statefulset_to_pod
args = {
statefulset_uids = with.statefulsets_for_service.rows[*].uid
}
}
}
}
container {
container {
width = 6
table {
title = "Overview"
type = "line"
width = 6
query = query.service_overview
args = {
uid = self.input.service_uid.value
}
column "Namespace" {
href = "/kubernetes_insights.dashboard.namespace_detail?input.namespace_uid={{.'Namespace UID' | @uri}}"
}
column "Namespace UID" {
display = "none"
}
}
table {
title = "Labels"
width = 6
query = query.service_labels
args = {
uid = self.input.service_uid.value
}
}
}
container {
width = 6
table {
title = "Annotations"
query = query.service_annotations
args = {
uid = self.input.service_uid.value
}
}
table {
title = "IP Details"
query = query.service_ip_details
args = {
uid = self.input.service_uid.value
}
}
}
container {
flow {
title = "Service Port Analysis"
query = query.service_tree
args = {
uid = self.input.service_uid.value
}
}
}
container {
table {
title = "Ports"
width = 6
query = query.service_ports
args = {
uid = self.input.service_uid.value
}
}
table {
title = "Pods"
width = 6
query = query.service_pods_detail
args = {
uid = self.input.service_uid.value
}
column "UID" {
display = "none"
}
column "Name" {
href = "${dashboard.pod_detail.url_path}?input.pod_uid={{.UID | @uri}}"
}
}
}
}
}
# Input queries
query "service_input" {
sql = <<-EOQ
select
title as label,
uid as value,
json_build_object(
'namespace', namespace,
'context_name', context_name
) as tags
from
kubernetes_service
order by
title;
EOQ
}
# Card queries
query "service_type" {
sql = <<-EOQ
select
'Type' as label,
type as value
from
kubernetes_service
where
uid = $1;
EOQ
param "uid" {}
}
query "service_default_namespace" {
sql = <<-EOQ
select
'Namespace' as label,
initcap(namespace) as value,
case when namespace = 'default' then 'alert' else 'ok' end as type,
n.uid as "UID"
from
kubernetes_service as s,
kubernetes_namespace as n
where
n.name = s.namespace
and n.context_name = s.context_name
and s.uid = $1;
EOQ
param "uid" {}
}
# With queries
query "pods_for_service" {
sql = <<-EOQ
select
p.uid as uid
from
kubernetes_service as s,
kubernetes_pod as p
where
p.selector_search = s.selector_query
and s.context_name = p.context_name
and s.uid = $1;
EOQ
}
query "replicasets_for_service" {
sql = <<-EOQ
select
pod_owner ->> 'uid' as uid
from
kubernetes_pod as pod,
jsonb_array_elements(pod.owner_references) as pod_owner,
kubernetes_service as s
where
s.uid = $1
and s.context_name = pod.context_name
and pod.selector_search = s.selector_query;
EOQ
}
query "statefulsets_for_service" {
sql = <<-EOQ
select
st.uid as uid
from
kubernetes_stateful_set as st,
kubernetes_service as s
where
st.service_name = s.name
and s.context_name = st.context_name
and s.uid = $1
union
select
distinct st.uid as uid
from
kubernetes_stateful_set as st,
kubernetes_pod as pod,
jsonb_array_elements(pod.owner_references) as pod_owner,
kubernetes_service as s
where
s.uid = $1
and s.context_name = st.context_name
and pod_owner ->> 'uid' = st.uid
and pod.selector_search = s.selector_query;
EOQ
}
query "deployments_for_service" {
sql = <<-EOQ
select
rs_owner ->> 'uid' as uid
from
kubernetes_replicaset as rs,
jsonb_array_elements(rs.owner_references) as rs_owner,
kubernetes_pod as pod,
jsonb_array_elements(pod.owner_references) as pod_owner,
kubernetes_service as s
where
s.uid = $1
and s.context_name = rs.context_name
and pod_owner ->> 'uid' = rs.uid
and pod.selector_search = s.selector_query;
EOQ
}
query "ingresses_for_service" {
sql = <<-EOQ
select
i.uid
from
kubernetes_service as s,
kubernetes_ingress as i,
jsonb_array_elements(rules) as r,
jsonb_array_elements(r -> 'http' -> 'paths') as p
where
s.name = p -> 'backend' -> 'service' ->> 'name'
and s.context_name = i.context_name
and s.uid = $1;
EOQ
}
# Other queries
query "service_overview" {
sql = <<-EOQ
select
s.name as "Name",
s.uid as "UID",
s.creation_timestamp as "Create Time",
n.uid as "Namespace UID",
n.name as "Namespace",
s.context_name as "Context Name"
from
kubernetes_service as s,
kubernetes_namespace as n
where
n.name = s.namespace
and n.context_name = s.context_name
and s.uid = $1;
EOQ
param "uid" {}
}
query "service_labels" {
sql = <<-EOQ
with jsondata as (
select
labels::json as label
from
kubernetes_service
where
uid = $1
)
select
key as "Key",
value as "Value"
from
jsondata,
json_each_text(label)
order by
key;
EOQ
param "uid" {}
}
query "service_annotations" {
sql = <<-EOQ
with jsondata as (
select
annotations::json as annotation
from
kubernetes_service
where
uid = $1
)
select
key as "Key",
value as "Value"
from
jsondata,
json_each_text(annotation)
order by
key;
EOQ
param "uid" {}
}
query "service_ports" {
sql = <<-EOQ
select
p ->> 'name' as "Name",
p ->> 'port' as "Port",
p ->> 'nodePort' as "Node Port",
p ->> 'protocol' as "Protocol",
p ->> 'targetPort' as "Target Port"
from
kubernetes_service,
jsonb_array_elements(ports) as p
where
uid = $1
order by
p ->> 'name';
EOQ
param "uid" {}
}
query "service_pods_detail" {
sql = <<-EOQ
select
name as "Name",
uid as "UID",
restart_policy as "Restart Policy",
node_name as "Node Name"
from
kubernetes_pod
where
selector_search in (select selector_query from kubernetes_service where uid = $1)
order by
name;
EOQ
param "uid" {}
}
query "service_ip_details" {
sql = <<-EOQ
select
cluster_ip as "Cluster IP",
l ->> 'ip' as "Load Balancer IP",
external_ips as "External IPs"
from
kubernetes_service,
jsonb_array_elements(load_balancer_ingress) as l
where
uid = $1
union
select
cluster_ip as "Cluster IP",
load_balancer_ingress::text as "Load Balancer IP",
external_ips as "External IPs"
from
kubernetes_service
where
uid = $1 and load_balancer_ingress is null;
EOQ
param "uid" {}
}
query "service_tree" {
sql = <<-EOQ
with pods as (
select
uid as pod_uid,
title as pod_title
from
kubernetes_pod
where
selector_search in (select selector_query from kubernetes_service where uid = $1)
),
services as (
select
uid,
title,
pod_uid,
pod_title,
cluster_ip,
external_ips,
selector_query,
l as lb,
p ->> 'protocol' as protocol_number,
concat(p ->> 'port','/', p ->> 'protocol') as port,
concat(p ->> 'targetPort','/', p ->> 'protocol') as targetPort
from
pods,
kubernetes_service,
jsonb_array_elements(ports) as p,
jsonb_array_elements(load_balancer_ingress) as l
where
uid = $1
union
select
uid,
title,
pod_uid,
pod_title,
cluster_ip,
external_ips,
selector_query,
load_balancer_ingress as lb,
p ->> 'protocol' as protocol_number,
concat(p ->> 'port','/', p ->> 'protocol') as port,
concat(p ->> 'targetPort','/', p ->> 'protocol') as targetPort
from
pods,
kubernetes_service,
jsonb_array_elements(ports) as p
where
uid = $1
and load_balancer_ingress is null
)
-- LB
select
lb::text as id,
lb ->> 'ip' as title,
'lb' as category,
null as from_id,
null as to_id
from
services
-- EIP
union all
select
eip as id,
eip as title,
'external_ip' as category,
null as from_id,
null as to_id
from
services,
jsonb_array_elements_text(external_ips) as eip
-- ClusterIP
union all
select
cluster_ip as id,
cluster_ip as title,
'cluster_ip' as category,
null as from_id,
null as to_id
from
services
-- Ports
union all
select
port as id,
port as title,
'port' as category,
null as from_id,
null as to_id
from
services
-- service
union all
select
distinct title as id,
title as title,
'service' as category,
null as from_id,
null as to_id
from
services
-- targetPorts
union all
select
concat(targetPort,' (Target Port)') as id,
targetPort as title,
'targetPort' as category,
null as from_id,
null as to_id
from
services
-- pods
union all
select
distinct pod_title as id,
pod_title as title,
'pod' as category,
null as from_id,
null as to_id
from
services
-- externalIP -> port
union select
null as id,
null as title,
'external_ip' as category,
eip as from_id,
port as to_id
from services,
jsonb_array_elements_text(external_ips) as eip
-- clusterIP -> port
union select
null as id,
null as title,
'cluster_ip' as category,
cluster_ip as from_id,
port as to_id
from services
-- lb -> port
union select
null as id,
null as title,
'lb' as category,
lb::text as from_id,
port as to_id
from services
-- port -> service
union select
null as id,
null as title,
'port' as category,
port as from_id,
title as to_id
from services
-- service -> target
union select
null as id,
null as title,
'service' as category,
title as from_id,
concat(targetPort,' (Target Port)') as to_id
from services
-- target -> pod
union select
null as id,
null as title,
'targetPort' as category,
concat(targetPort,' (Target Port)') as from_id,
pod_title as to_id
from services
EOQ
param "uid" {}
}