-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
136 lines (117 loc) · 3.23 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"time"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
metricsv "k8s.io/metrics/pkg/client/clientset/versioned"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
func main() {
parseFlags()
const baseHeight = 0 // Minimum height of the table
const heightIncrement = 1 // Additional height per node or pod
const maxHeight = 30 // Maximum height of the table
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
log.Fatalf("Failed to build kubeconfig: %v", err)
}
metricsClient, err := metricsv.NewForConfig(config)
if err != nil {
panic(err.Error())
}
nodeMetricsList, err := metricsClient.MetricsV1beta1().NodeMetricses().List(context.Background(), v1.ListOptions{})
if err != nil {
panic(err.Error())
}
// Initialize Kubernetes clientset for node information
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
// Initialize termui
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
// Create a slice to hold the bar charts
// var barCharts []*widgets.BarChart
nodeTable := widgets.NewTable()
// Configure nodeTable...
podsTable := widgets.NewTable()
// Default configuration for podsTable...
if *podsFlag {
// Additional configuration specific to podsTable
podsTable.TextStyle = ui.NewStyle(ui.ColorWhite)
podsTable.RowSeparator = true
podsTable.BorderStyle = ui.NewStyle(ui.ColorGreen)
podsTable.Title = "Pods"
dynamicHeight := ((min(*countPods, maxHeight) + baseHeight) * 3) - 6 // Adjust baseHeight and maxHeight as needed
podsTable.SetRect(0, 0, 160, dynamicHeight)
updatePodsData(metricsClient, clientset, podsTable, *countPods)
} else {
nodeTable.TextStyle = ui.NewStyle(ui.ColorWhite)
nodeTable.RowSeparator = true
nodeTable.BorderStyle = ui.NewStyle(ui.ColorGreen)
nodeTable.Title = "Nodes"
nodeCount := len(nodeMetricsList.Items) // Get the count of nodes
dynamicHeight := 0 // Dynamic height of the table
if nodeCount > 5 {
dynamicHeight = ((baseHeight + (nodeCount * heightIncrement)) * 3) - 7 // Calculate dynamic height
} else {
dynamicHeight = baseHeight + (nodeCount * heightIncrement) // Calculate dynamic height
}
if dynamicHeight > maxHeight {
dynamicHeight = maxHeight
}
fmt.Println("Dynamic Height:", dynamicHeight)
nodeTable.SetRect(0, 0, 160, dynamicHeight)
updateNodesData(metricsClient, clientset, nodeTable)
}
if *podsFlag {
updatePodsData(
metricsClient,
clientset,
podsTable,
*countPods,
)
} else {
updateNodesData(
metricsClient,
clientset,
nodeTable,
)
}
// Ticker for updating data
ticker := time.NewTicker(20 * time.Millisecond)
defer ticker.Stop()
// Handle key q to quit
uiEvents := ui.PollEvents()
for {
select {
case e := <-uiEvents:
if e.ID == "q" || e.Type == ui.KeyboardEvent {
return
}
case <-ticker.C:
if *podsFlag {
updatePodsData(
metricsClient,
clientset,
nodeTable,
*countPods,
)
} else {
updateNodesData(
metricsClient,
clientset,
nodeTable,
)
}
}
}
}