generated from replicatedhq/krew-plugin-template
/
builder.go
972 lines (818 loc) · 23.6 KB
/
builder.go
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
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
package plugin
import (
"errors"
"os"
"strconv"
"strings"
v1 "k8s.io/api/core/v1"
)
type Looper interface {
BuildBranch(info BuilderInformation, rows [][]Cell) ([]Cell, error)
BuildContainerSpec(container v1.Container, info BuilderInformation) ([][]Cell, error)
BuildEphemeralContainerSpec(container v1.EphemeralContainer, info BuilderInformation) ([][]Cell, error)
BuildContainerStatus(container v1.ContainerStatus, info BuilderInformation) ([][]Cell, error)
BuildPodRow(pod v1.Pod, info BuilderInformation) ([][]Cell, error)
Headers() []string
HideColumns(info BuilderInformation) []int
}
type RowBuilder struct {
Connection *Connector
Table *Table
CommonFlags commonFlags
PodName []string // list of pod names to retrieve
LoopStatus bool // do we need to loop over v1.Pod.Status.ContainerStatus
LoopSpec bool // should we loop over v1.Pod.Spec.Containers
LabelNodeName string
labelNodeValue string
LabelPodName string
labelPodValue string
AnnotationPodName string
annotationPodValue string
ShowTreeView bool // show the standard tree view with the resource sets as the root
ShowPodName bool
ShowInitContainers bool
ShowContainerType bool
ShowNodeTree bool // show the tree view with the nodes at the root level rather than just the resource sets at root
DontListContainers bool // dont loop through containers, only the main pod
FilterList map[string]matchValue // used to filter out rows from the table during Print function
CalcFiltered bool // the filterd out rows are included in the branch calculations
DefaultHeaderLen int
InputFilename string // filename to be used as the source instead of reading pod information from k8s api
StdinChanged bool // have we been run as part of a shell redirect
annotationLabel map[string]map[string]map[string]map[string]string
head []string
filter []matchFilter
columnByNames []string // show only these named columns
}
type BuilderInformation struct {
// Pod *v1.Pod
Data ParentData
PodName string
ContainerType string // single letter type id
Namespace string
NodeName string
Name string // objects name
TreeView bool
TypeName string // k8s kind
}
type matchFilter struct {
value string
comparison int // 1:>, 2:<, 3:!
compareEql bool // true:==, true:<=, true:>=
set bool
}
type matchValue struct {
operator string
value string
}
// SetFlagsFrom sets the common flags to match the values retrieved from the passed object
func (b *RowBuilder) SetFlagsFrom(commonFlagList commonFlags) {
log := logger{location: "RowBuilder:SetFlagsFrom"}
log.Debug("Start")
b.CommonFlags = commonFlagList
b.ShowTreeView = commonFlagList.showTreeView
b.ShowNodeTree = commonFlagList.showNodeTree
b.LabelNodeName = commonFlagList.labelNodeName
b.LabelPodName = commonFlagList.labelPodName
b.AnnotationPodName = commonFlagList.annotationPodName
b.FilterList = b.CommonFlags.filterList
b.CalcFiltered = b.CommonFlags.calcMatchOnly
b.InputFilename = b.CommonFlags.inputFilename
// we always show the pod name by default
b.ShowPodName = true
// if a single pod is selected we dont need to show its name
if len(b.PodName) == 1 {
if len(b.PodName[0]) >= 1 {
log.Debug("builder.ShowPodName = false")
b.ShowPodName = false
}
}
if !b.ShowContainerType {
b.ShowContainerType = b.CommonFlags.showContainerType
}
if len(commonFlagList.showColumnByName) > 0 {
columns := strings.TrimSpace(commonFlagList.showColumnByName)
columnNames := strings.ToUpper(columns)
b.columnByNames = strings.Split(columnNames, ",")
}
if commonFlagList.showInitContainers {
b.ShowInitContainers = true
}
}
func (b RowBuilder) HasStdinChanged() (bool, error) {
// check if our input has been redirected
fileinfo, err := os.Stdin.Stat()
if err != nil {
return false, err
}
if (fileinfo.Mode() & os.ModeCharDevice) == 0 {
return true, nil
}
return false, nil
}
// Build
func (b *RowBuilder) Build(loop Looper) error {
var podList []v1.Pod
var err error
log := logger{location: "RowBuilder:Build"}
log.Debug("Start")
info := BuilderInformation{TreeView: b.ShowTreeView}
// check if our input has been redirected
b.StdinChanged, err = b.HasStdinChanged()
if err != nil {
return err
}
err = b.LoadHeaders(loop, &info)
if err != nil {
return err
}
if len(b.InputFilename) == 0 && !b.StdinChanged {
podList, err = b.Connection.GetPods(b.PodName)
} else {
podList, err = b.loadYaml(b.InputFilename)
}
if err != nil {
return err
}
if b.ShowTreeView {
err := b.populateAnnotationsLabels(podList)
if err != nil {
return err
}
ol := b.Connection.BuildOwnersList()
for _, value := range ol {
var rowid int
if b.ShowNodeTree {
rowid = b.Table.AddPlaceHolderRow()
}
info.NodeName = value.name
totals, err := b.walkTreeCreateRow(loop, &info, *value)
if err != nil {
return err
}
if b.ShowNodeTree {
info.Namespace = value.namespace
info.Name = value.name
info.ContainerType = TypeIDNode
info.TypeName = value.kind
info.NodeName = ""
if len(totals) > 0 {
partOut, _ := loop.BuildBranch(info, totals)
tblOut := b.makeFullRow(&info, value.indent, partOut)
if len(tblOut) > 0 {
b.Table.UpdatePlaceHolderRow(rowid, tblOut)
}
} else {
b.Table.HidePlaceHolderRow(rowid)
}
}
}
} else {
err := b.BuildContainerTable(loop, &info, podList)
if err != nil {
return err
}
}
if len(b.columnByNames) > 0 {
err := b.Table.HideOnlyNamedColumns(b.columnByNames)
if err != nil {
return err
}
}
return nil
}
// walkTreeCreateRow - recursive function to loop over each child item along with all sub children, buildPodTree
//
// is called on each child with the results passed to Sum so we can calculate parent values from the children
func (b *RowBuilder) walkTreeCreateRow(loop Looper, info *BuilderInformation, parent LeafNode) ([][]Cell, error) {
var totals [][]Cell
log := logger{location: "RowBuilder:walkTreeCreateRow"}
log.Debug("Start")
for _, value := range parent.child {
var partOut [][]Cell
var err error
rowid := b.Table.AddPlaceHolderRow()
info.Namespace = value.namespace
info.Name = value.name
info.TypeName = value.kind
info.ContainerType = value.kindIndicator
info.Data = value.data
if value.kind == "Pod" {
infoPod := *info
infoPod.PodName = value.data.pod.Name
infoPod.NodeName = value.data.pod.Spec.NodeName
// check if we have any labels that need to be shown as columns
b.setValuesAnnotationLabel(info.Data.pod)
partOut, err = b.podLoop(loop, infoPod, value.data.pod, value.indent+1)
if err != nil {
return [][]Cell{}, err
}
} else {
// make the row for the table header line
infoSet := *info
partOut, err = b.walkTreeCreateRow(loop, &infoSet, *value)
}
if err != nil {
return [][]Cell{}, err
}
infoSet := *info
tblBranch, err := loop.BuildBranch(infoSet, partOut)
if err != nil {
return [][]Cell{}, err
} else {
tblOut := b.makeFullRow(&infoSet, value.indent, tblBranch)
if b.matchShouldExclude(tblOut) {
b.Table.HidePlaceHolderRow(rowid)
} else {
b.Table.UpdatePlaceHolderRow(rowid, tblOut)
if b.CalcFiltered {
totals = append(totals, tblBranch)
}
}
if !b.CalcFiltered {
//dont add up the filtered out rows
totals = append(totals, tblBranch)
}
}
// }
b.labelNodeValue = ""
b.labelPodValue = ""
b.annotationPodValue = ""
}
return totals, nil
}
// matchShouldExclude checks the match filter and returns true if the row should be excluded from output
func (b *RowBuilder) matchShouldExclude(tblOut []Cell) bool {
var fValue float64
var iValue int64
exclude := true
if len(b.FilterList) == 0 {
return false
}
for idx, cell := range tblOut {
filter := b.filter[idx]
// do we have an exclude filter set that we need to process
if !filter.set {
continue
}
if cell.typ == 0 {
exclude = b.canExcludeMatchString(filter, cell.text, filter.value)
}
if cell.typ == 1 {
// convert filter.value to number
iValue, _ = strconv.ParseInt(filter.value, 10, 64)
exclude = b.canExcludeMatchInt(filter, cell.number, iValue)
}
if cell.typ == 2 {
// convert filter.value to float
fValue, _ = strconv.ParseFloat(filter.value, 64)
exclude = b.canExcludeMatchFloat(filter, cell.float, fValue)
}
if exclude {
return true
}
}
return false
}
// check if any labels or annotations are needed and set their values
func (b *RowBuilder) setValuesAnnotationLabel(pod v1.Pod) {
if b.LabelNodeName != "" {
b.labelNodeValue = b.annotationLabel["label"]["node"][pod.Spec.NodeName][b.LabelNodeName]
}
if b.LabelPodName != "" {
b.labelPodValue = b.annotationLabel["label"]["pod"][pod.Name][b.LabelPodName]
}
if b.AnnotationPodName != "" {
b.annotationPodValue = b.annotationLabel["annotation"]["pod"][pod.Name][b.AnnotationPodName]
}
}
func (b *RowBuilder) populateAnnotationsLabels(podList []v1.Pod) error {
log := logger{location: "RowBuilder:populateAnnotationsLabels"}
log.Debug("Start")
// type kind pod label value
b.annotationLabel = make(map[string]map[string]map[string]map[string]string)
b.annotationLabel["label"] = make(map[string]map[string]map[string]string)
b.annotationLabel["annotation"] = make(map[string]map[string]map[string]string)
if b.LabelNodeName != "" {
log.Debug("b.LabelNodeName", b.LabelNodeName)
nodeLabels, err := b.Connection.GetNodeLabels(podList)
if err != nil {
return err
}
b.annotationLabel["label"]["node"] = nodeLabels
}
if b.LabelPodName != "" {
log.Debug("b.LabelPodName", b.LabelPodName)
podLabels, err := b.Connection.GetPodLabels(podList)
if err != nil {
return err
}
b.annotationLabel["label"]["pod"] = podLabels
}
if b.AnnotationPodName != "" {
log.Debug("b.AnnotationPodName", b.AnnotationPodName)
podAnnotations, err := b.Connection.GetPodAnnotations(podList)
if err != nil {
return err
}
b.annotationLabel["annotation"]["pod"] = podAnnotations
}
return nil
}
// Build normal table
func (b *RowBuilder) BuildContainerTable(loop Looper, info *BuilderInformation, podList []v1.Pod) error {
log := logger{location: "RowBuilder:BuildContainerTable"}
log.Debug("Start")
err := b.populateAnnotationsLabels(podList)
if err != nil {
return err
}
for _, pod := range podList {
log.Debug("pod.Name =", pod.Name)
infoPod := *info
// we havent come from a tree list so we need to set infoPod.Data.pod manually
infoPod.Data.pod = pod
infoPod.PodName = pod.Name
infoPod.Namespace = pod.Namespace
infoPod.NodeName = pod.Spec.NodeName
infoPod.ContainerType = TypeIDPod
infoPod.TypeName = TypeNamePod
// check if we have any labels that need to be shown as columns
b.setValuesAnnotationLabel(pod)
_, err := b.podLoop(loop, infoPod, pod, 0)
if err != nil {
return err
}
}
if err := b.Table.SortByNames(b.CommonFlags.sortList...); err != nil {
return err
}
return nil
}
// LoadHeaders sets the default column headers hiding as needed
func (b *RowBuilder) LoadHeaders(loop Looper, info *BuilderInformation) error {
var tblHead []string
var hideColumns []int
log := logger{location: "RowBuilder:LoadHeaders"}
log.Debug("Start")
tblHead = b.getDefaultHead(info)
// save the default lengh now as we need to use it in other functions
defaultHeaderLen := len(tblHead)
log.Debug("len(defaultHeaderLen) =", defaultHeaderLen)
b.DefaultHeaderLen = defaultHeaderLen
hideColumns = loop.HideColumns(*info)
tblHead = append(tblHead, loop.Headers()...)
log.Debug("len(tblHead) =", len(tblHead))
log.Debug("tblHead =", tblHead)
b.Table.SetHeader(tblHead...)
log.Debug("len(b.FilterList) =", len(b.FilterList))
if len(b.FilterList) >= 1 {
b.head = tblHead // we need a local copy of the header for filters to work
b.filter = make([]matchFilter, len(b.head))
err := b.setFilter(b.FilterList)
if err != nil {
return err
}
}
b.setVisibleColumns(info)
log.Debug("len(hideColumns) =", len(hideColumns))
for _, id := range hideColumns {
b.Table.HideColumn(defaultHeaderLen + id)
}
return nil
}
// takes a filter as a string to exclude matching rows from the Print function
// filter is in the form COLUMN_NAME OPERATOR VALUE, where operator can be one of <,>,<=,>=,!=,=,==
func (b *RowBuilder) setFilter(filter map[string]matchValue) error {
for words, match := range filter {
// the smallest header name is T making a valid string "T=0"
if len([]rune(words)) < 1 {
continue
}
found := false
columnName := ""
operator := ""
value := ""
if len(match.operator) > 0 && len(words) > 0 {
columnName = words
operator = match.operator
value = match.value
found = true
}
if found {
idx := -1
for i := 0; i < len(b.head); i++ {
if columnName == b.head[i] {
idx = i
break
}
}
if idx == -1 {
return errors.New("invalid column name specified")
}
switch operator {
case "=":
fallthrough
case "==":
b.filter[idx].comparison = 0
b.filter[idx].compareEql = true
case "<=":
b.filter[idx].comparison = 2
b.filter[idx].compareEql = true
case ">=":
b.filter[idx].comparison = 1
b.filter[idx].compareEql = true
case "<":
b.filter[idx].comparison = 2
case ">":
b.filter[idx].comparison = 1
case "!=":
b.filter[idx].comparison = 3
b.filter[idx].compareEql = false
default:
return errors.New("invalid operator found")
}
if len(value) <= 0 {
return errors.New("invalid value specified for filter")
}
b.filter[idx].value = value
b.filter[idx].set = true
}
}
return nil
}
// SetVisibleColumns hides default columns based on various flags
func (b *RowBuilder) setVisibleColumns(info *BuilderInformation) {
log := logger{location: "RowBuilder:SetVisibleColumns"}
log.Debug("Start")
if !b.ShowContainerType {
b.Table.HideColumn(0)
}
if info.TreeView {
// only hide the nodename in tree view
if !b.CommonFlags.showNodeName {
b.Table.HideColumn(2)
}
return
}
if !b.CommonFlags.showNamespaceName {
b.Table.HideColumn(1)
}
if !b.CommonFlags.showNodeName {
b.Table.HideColumn(2)
}
if !b.ShowPodName {
// we need to hide the pod name in the table
b.Table.HideColumn(3)
}
if b.DontListContainers {
b.Table.HideColumn(4)
}
}
// PodLoop given a pod we loop over all containers adding to the table as we go
//
// returns a copy of rows added and nil on success
func (b *RowBuilder) podLoop(loop Looper, info BuilderInformation, pod v1.Pod, indentLevel int) ([][]Cell, error) {
var podRowsOut [][]Cell
log := logger{location: "RowBuilder:PodLoop"}
log.Debug("Start")
if b.DontListContainers {
log.Debug("skipping containers")
info.ContainerType = TypeIDPod
info.TypeName = TypeNamePod
allRows, err := loop.BuildPodRow(pod, info)
if err != nil {
return [][]Cell{}, err
}
for _, row := range allRows {
rowsOut := b.makeFullRow(&info, indentLevel, row)
if !b.matchShouldExclude(rowsOut) {
b.Table.AddRow(rowsOut...)
}
}
podRowsOut = append(podRowsOut, allRows...)
return podRowsOut, nil
}
if b.ShowInitContainers {
log.Debug("loop init Container")
info.ContainerType = TypeIDInitContainer
info.TypeName = TypeNameInitContainer
if b.LoopStatus {
log.Debug("processing LoopStatus")
for _, container := range pod.Status.InitContainerStatuses {
// should the container be processed
log.Debug("processing -", container.Name)
if skipContainerName(b.CommonFlags, container.Name) {
continue
}
info.Name = container.Name
allRows, err := loop.BuildContainerStatus(container, info)
if err != nil {
return [][]Cell{}, err
}
for _, row := range allRows {
rowsOut := b.makeFullRow(&info, indentLevel, row)
if !b.matchShouldExclude(rowsOut) {
b.Table.AddRow(rowsOut...)
}
}
podRowsOut = append(podRowsOut, allRows...)
}
}
if b.LoopSpec {
log.Debug("processing LoopSpec")
for _, container := range pod.Spec.InitContainers {
// should the container be processed
log.Debug("processing -", container.Name)
if skipContainerName(b.CommonFlags, container.Name) {
continue
}
info.Name = container.Name
allRows, err := loop.BuildContainerSpec(container, info)
if err != nil {
return [][]Cell{}, err
}
for _, row := range allRows {
rowsOut := b.makeFullRow(&info, indentLevel, row)
if !b.matchShouldExclude(rowsOut) {
b.Table.AddRow(rowsOut...)
}
}
podRowsOut = append(podRowsOut, allRows...)
}
}
}
// now show the container line
log.Debug("loop standard Container")
info.ContainerType = TypeIDContainer
info.TypeName = "Container"
if b.LoopStatus {
log.Debug("processing LoopStatus")
for _, container := range pod.Status.ContainerStatuses {
// should the container be processed
if skipContainerName(b.CommonFlags, container.Name) {
continue
}
log.Debug("processing -", container.Name)
info.Name = container.Name
allRows, err := loop.BuildContainerStatus(container, info)
if err != nil {
return [][]Cell{}, err
}
for _, row := range allRows {
rowsOut := b.makeFullRow(&info, indentLevel, row)
if !b.matchShouldExclude(rowsOut) {
b.Table.AddRow(rowsOut...)
}
}
podRowsOut = append(podRowsOut, allRows...)
}
}
if b.LoopSpec {
log.Debug("processing LoopSpec")
for _, container := range pod.Spec.Containers {
// should the container be processed
if skipContainerName(b.CommonFlags, container.Name) {
log.Debug("Skipping container:", container.Name)
continue
}
log.Debug("processing -", container.Name)
info.Name = container.Name
allRows, err := loop.BuildContainerSpec(container, info)
if err != nil {
return [][]Cell{}, err
}
for _, row := range allRows {
rowsOut := b.makeFullRow(&info, indentLevel, row)
if !b.matchShouldExclude(rowsOut) {
b.Table.AddRow(rowsOut...)
}
}
podRowsOut = append(podRowsOut, allRows...)
}
}
log.Debug("loop ephemeral Container")
info.ContainerType = TypeIDEphemeralContainer
info.TypeName = TypeNameEphemeralContainer
if b.LoopStatus {
log.Debug("processing LoopStatus")
for _, container := range pod.Status.EphemeralContainerStatuses {
// should the container be processed
if skipContainerName(b.CommonFlags, container.Name) {
continue
}
log.Debug("processing -", container.Name)
info.Name = container.Name
allRows, err := loop.BuildContainerStatus(container, info)
if err != nil {
return [][]Cell{}, err
}
for _, row := range allRows {
rowsOut := b.makeFullRow(&info, indentLevel, row)
if !b.matchShouldExclude(rowsOut) {
b.Table.AddRow(rowsOut...)
}
}
podRowsOut = append(podRowsOut, allRows...)
}
}
if b.LoopSpec {
log.Debug("processing LoopSpec")
for _, container := range pod.Spec.EphemeralContainers {
// should the container be processed
if skipContainerName(b.CommonFlags, container.Name) {
continue
}
log.Debug("processing -", container.Name)
info.Name = container.Name
allRows, err := loop.BuildEphemeralContainerSpec(container, info)
if err != nil {
return [][]Cell{}, err
}
for _, row := range allRows {
rowsOut := b.makeFullRow(&info, indentLevel, row)
if !b.matchShouldExclude(rowsOut) {
b.Table.AddRow(rowsOut...)
}
}
podRowsOut = append(podRowsOut, allRows...)
}
}
return podRowsOut, nil
}
// makeFullRow adds the listed columns to the default columns, outputs
//
// the complete row as a list of columns
func (b *RowBuilder) makeFullRow(info *BuilderInformation, indentLevel int, columns ...[]Cell) []Cell {
log := logger{location: "RowBuilder:makeFullRow"}
log.Debug("Start")
rowList := b.getDefaultCells(info)
if b.LabelNodeName != "" {
rowList = append(rowList, NewCellText(b.labelNodeValue))
}
if b.LabelPodName != "" {
rowList = append(rowList, NewCellText(b.labelPodValue))
}
if b.AnnotationPodName != "" {
rowList = append(rowList, NewCellText(b.annotationPodValue))
}
if info.TreeView {
name := ""
// default cells dont have name column, need to add it in tree view
if len(info.TypeName) == 0 {
name = info.Name
} else {
name = info.TypeName + "/" + info.Name
}
if !b.ShowNodeTree {
rowList = append(rowList, NewCellTextIndent(name, indentLevel-1))
} else {
rowList = append(rowList, NewCellTextIndent(name, indentLevel))
}
}
for _, c := range columns {
rowList = append(rowList, c...)
}
log.Debug("len(rowList) =", len(rowList))
return rowList
}
// GetDefaultHead: returns the common headers in order
func (b *RowBuilder) getDefaultHead(info *BuilderInformation) []string {
log := logger{location: "RowBuilder:GetDefaultHead"}
log.Debug("Start")
var headList []string
log.Debug("b.info.TreeView =", info.TreeView)
if info.TreeView {
// in tree view we only create the namespace and nodename columns, the name colume is created outside of this
// function so we have full control over its contents
headList = []string{
"T", "NAMESPACE", "NODE",
}
// } else if b.DontListContainers {
// headList = []string{
// "T", "NAMESPACE", "NODE", "PODNAME",
// }
} else {
headList = []string{
"T", "NAMESPACE", "NODE", "PODNAME", "CONTAINER",
}
}
if b.LabelNodeName != "" {
log.Debug("LabelNodeName =", b.LabelNodeName)
headList = append(headList, b.LabelNodeName)
}
if b.LabelPodName != "" {
log.Debug("LabelPodName =", b.LabelPodName)
headList = append(headList, b.LabelPodName)
}
if b.AnnotationPodName != "" {
log.Debug("AnnotationPodName =", b.AnnotationPodName)
headList = append(headList, b.AnnotationPodName)
}
if info.TreeView {
headList = append(headList, "NAME")
}
log.Debug("headList =", headList)
return headList
}
// GetDefaultCells: returns an array of cells prepopulated with the common information
func (b *RowBuilder) getDefaultCells(info *BuilderInformation) []Cell {
log := logger{location: "RowBuilder:GetDefaultCells"}
log.Debug("Start")
if info.TreeView {
return []Cell{
NewCellText(info.ContainerType),
NewCellText(info.Namespace),
NewCellText(info.NodeName),
}
} else {
return []Cell{
NewCellText(info.ContainerType),
NewCellText(info.Namespace),
NewCellText(info.NodeName),
NewCellText(info.PodName),
NewCellText(info.Name),
}
}
}
func (b RowBuilder) canExcludeMatchString(filter matchFilter, val1 string, val2 string) bool {
// equals
if filter.compareEql {
if strMatch(val1, val2) {
return false
}
}
// not
if filter.comparison == 3 {
if !strMatch(val1, val2) {
return false
}
}
// bigger
if filter.comparison == 1 {
if val1 > val2 {
return false
}
}
// smaller
if filter.comparison == 2 {
if val1 < val2 {
return false
}
}
return true
}
func (b RowBuilder) canExcludeMatchInt(filter matchFilter, val1 int64, val2 int64) bool {
// equals
if filter.compareEql {
if val1 == val2 {
return false
}
}
// not equals
if filter.comparison == 3 {
if val1 != val2 {
return false
}
}
// bigger
if filter.comparison == 1 {
if val1 > val2 {
return false
}
}
// smaller
if filter.comparison == 2 {
if val1 < val2 {
return false
}
}
return true
}
func (b RowBuilder) canExcludeMatchFloat(filter matchFilter, val1 float64, val2 float64) bool {
// equals
if filter.compareEql {
if val1 == val2 {
return false
}
}
// not equals
if filter.comparison == 3 {
if val1 != val2 {
return false
}
}
// bigger
if filter.comparison == 1 {
if val1 > val2 {
return false
}
}
// smaller
if filter.comparison == 2 {
if val1 < val2 {
return false
}
}
return true
}