-
Notifications
You must be signed in to change notification settings - Fork 2
/
kubernetesinfo.go
231 lines (188 loc) · 5.72 KB
/
kubernetesinfo.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package components
import (
"strings"
"github.com/cosi-project/runtime/pkg/resource"
"github.com/rivo/tview"
"github.com/siderolabs/gen/maps"
"github.com/aenix-io/talm/internal/pkg/dashboard/apidata"
"github.com/aenix-io/talm/internal/pkg/dashboard/resourcedata"
"github.com/siderolabs/talos/pkg/machinery/config/machine"
"github.com/siderolabs/talos/pkg/machinery/resources/config"
"github.com/siderolabs/talos/pkg/machinery/resources/k8s"
)
type staticPodStatuses struct {
apiServer string
controllerManager string
scheduler string
}
type kubernetesInfoData struct {
isControlPlane bool
kubernetesVersion string
kubeletStatus string
podStatuses staticPodStatuses
staticPodStatusMap map[resource.ID]*k8s.StaticPodStatus
}
// KubernetesInfo represents the kubernetes info widget.
type KubernetesInfo struct {
tview.TextView
selectedNode string
nodeMap map[string]*kubernetesInfoData
}
// NewKubernetesInfo initializes KubernetesInfo.
func NewKubernetesInfo() *KubernetesInfo {
kubernetes := &KubernetesInfo{
TextView: *tview.NewTextView(),
nodeMap: make(map[string]*kubernetesInfoData),
}
kubernetes.SetDynamicColors(true).
SetText(noData).
SetBorderPadding(1, 0, 1, 0)
return kubernetes
}
// OnNodeSelect implements the NodeSelectListener interface.
func (widget *KubernetesInfo) OnNodeSelect(node string) {
if node != widget.selectedNode {
widget.selectedNode = node
widget.redraw()
}
}
// OnResourceDataChange implements the ResourceDataListener interface.
func (widget *KubernetesInfo) OnResourceDataChange(data resourcedata.Data) {
widget.updateNodeData(data)
if data.Node == widget.selectedNode {
widget.redraw()
}
}
// OnAPIDataChange implements the APIDataListener interface.
func (widget *KubernetesInfo) OnAPIDataChange(node string, data *apidata.Data) {
nodeAPIData := data.Nodes[node]
widget.updateNodeAPIData(node, nodeAPIData)
if node == widget.selectedNode {
widget.redraw()
}
}
func (widget *KubernetesInfo) updateNodeData(data resourcedata.Data) {
nodeData := widget.getOrCreateNodeData(data.Node)
switch res := data.Resource.(type) {
case *k8s.KubeletSpec:
if data.Deleted {
nodeData.kubernetesVersion = notAvailable
} else {
imageParts := strings.Split(res.TypedSpec().Image, ":")
if len(imageParts) > 0 {
nodeData.kubernetesVersion = imageParts[len(imageParts)-1]
}
}
case *k8s.StaticPodStatus:
if data.Deleted {
delete(nodeData.staticPodStatusMap, res.Metadata().ID())
} else {
nodeData.staticPodStatusMap[res.Metadata().ID()] = res
}
nodeData.podStatuses = widget.staticPodStatuses(maps.Values(nodeData.staticPodStatusMap))
case *config.MachineType:
nodeData.isControlPlane = !data.Deleted && res.MachineType() == machine.TypeControlPlane
}
}
func (widget *KubernetesInfo) updateNodeAPIData(node string, data *apidata.Node) {
nodeData := widget.getOrCreateNodeData(node)
if data != nil && data.ServiceList != nil {
for _, info := range data.ServiceList.GetServices() {
if info.Id == "kubelet" {
nodeData.kubeletStatus = toHealthStatus(info.GetHealth().Healthy)
break
}
}
}
}
func (widget *KubernetesInfo) getOrCreateNodeData(node string) *kubernetesInfoData {
nodeData, ok := widget.nodeMap[node]
if !ok {
nodeData = &kubernetesInfoData{
kubernetesVersion: notAvailable,
kubeletStatus: notAvailable,
podStatuses: staticPodStatuses{
apiServer: notAvailable,
controllerManager: notAvailable,
scheduler: notAvailable,
},
staticPodStatusMap: make(map[resource.ID]*k8s.StaticPodStatus),
}
widget.nodeMap[node] = nodeData
}
return nodeData
}
func (widget *KubernetesInfo) redraw() {
data := widget.getOrCreateNodeData(widget.selectedNode)
fieldList := make([]field, 0, 5)
fieldList = append(fieldList,
field{
Name: "KUBERNETES",
Value: data.kubernetesVersion,
},
field{
Name: "KUBELET",
Value: data.kubeletStatus,
})
if data.isControlPlane {
fieldList = append(fieldList,
field{
Name: "APISERVER",
Value: data.podStatuses.apiServer,
},
field{
Name: "CONTROLLER-MANAGER",
Value: data.podStatuses.controllerManager,
},
field{
Name: "SCHEDULER",
Value: data.podStatuses.scheduler,
})
}
fields := fieldGroup{
fields: fieldList,
}
widget.SetText(fields.String())
}
func (widget *KubernetesInfo) staticPodStatuses(statuses []*k8s.StaticPodStatus) staticPodStatuses {
result := staticPodStatuses{
apiServer: notAvailable,
controllerManager: notAvailable,
scheduler: notAvailable,
}
isReady := func(podStatus map[string]any) string {
conditions, conditionsOk := podStatus["conditions"]
if !conditionsOk {
return notAvailable
}
conditionsSlc, conditionsSlcOk := conditions.([]any)
if !conditionsSlcOk {
return notAvailable
}
for _, condition := range conditionsSlc {
conditionObj, conditionObjOk := condition.(map[string]any)
if !conditionObjOk {
return notAvailable
}
if conditionObj["type"] == "Ready" {
return toHealthStatus(conditionObj["status"] == "True")
}
}
return notAvailable
}
for _, status := range statuses {
podStatus := status.TypedSpec().PodStatus
switch {
case strings.Contains(status.Metadata().ID(), "kube-apiserver"):
result.apiServer = isReady(podStatus)
case strings.Contains(status.Metadata().ID(), "kube-controller-manager"):
result.controllerManager = isReady(podStatus)
case strings.Contains(status.Metadata().ID(), "kube-scheduler"):
result.scheduler = isReady(podStatus)
}
}
return result
}