-
Notifications
You must be signed in to change notification settings - Fork 0
/
hostDetails.go
334 lines (292 loc) · 9.59 KB
/
hostDetails.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
package main
import (
"github.com/rivo/tview"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"strings"
)
type HostDetails struct {
Hostname string
Ansible_host string
Ip string
Access_ip string
Groups []string
Node_labels map[string]string
Node_taints []string
}
var hostDetails HostDetails
var tmpNodeLabels string
var tmpNodeTaints string
func initFlexHostDetails(hostname string, readonly bool) {
getHostDetails(hostname)
formHostDetails := tview.NewForm()
formHostDetails.AddTextView("Hostname: ", hostname, 0, 3, false, false)
formHostDetails.AddInputField("Ansible Host:", hostDetails.Ansible_host, 0, nil, func(text string) {
hostDetails.Ansible_host = text
writeBackHostDetails()
})
formHostDetails.AddInputField("IP:", hostDetails.Ip, 0, nil, func(text string) {
hostDetails.Ip = text
writeBackHostDetails()
})
formHostDetails.AddInputField("Access IP:", hostDetails.Access_ip, 0, nil, func(text string) {
hostDetails.Access_ip = text
writeBackHostDetails()
})
formHostDetails.AddTextView("Groups: ", strings.Join(hostDetails.Groups, "\n"), 0, 0, false, true)
var labelsString string
if len(hostDetails.Node_labels) != 0 {
labels, err := yaml.Marshal(&hostDetails.Node_labels)
check(err)
labelsString = string(labels)
}
formHostDetails.AddTextView("Node Labels: ", labelsString, 0, 0, false, true)
var taintsString string
if len(hostDetails.Node_taints) != 0 {
taints, err := yaml.Marshal(&hostDetails.Node_taints)
check(err)
taintsString = string(taints)
}
formHostDetails.AddTextView("Node Taints: ", taintsString, 0, 0, false, true)
formDown := tview.NewForm()
if readonly {
for i := 0; i < formHostDetails.GetFormItemCount(); i++ {
formHostDetails.GetFormItem(i).SetDisabled(true)
}
} else {
formDown.
AddButton("Edit Groups", func() {
initFormEditGroups()
pages.SwitchToPage("Edit Groups")
}).
AddButton("Edit Node Labels", func() {
flexEditNodeLabels.Clear()
initFlexEditNodeLabels()
pages.SwitchToPage("Edit Node Labels")
}).
AddButton("Edit Node Taints", func() {
flexEditNodeTaints.Clear()
initFlexEditNodeTaints()
pages.SwitchToPage("Edit Node Taints")
})
}
flexHostDetails.
SetDirection(tview.FlexRow).
AddItem(formHostDetails, 0, 1, true).
AddItem(formDown, 3, 1, false)
flexHostDetails.SetBorder(true)
}
func initFormEditGroups() {
formEditGroups.Clear(true)
var newGroups []string
checkedMap := map[string]string{}
for _, group := range hostDetails.Groups {
checkedMap[group] = "checked"
}
formEditGroups.SetTitle("Edit Group").SetBorder(true)
formEditGroups.AddTextView("Hostname: ", hostDetails.Hostname, 0, 3, false, false)
formEditGroups.AddCheckbox("kube_control_plane", slices.Contains(hostDetails.Groups, "kube_control_plane"), func(checked bool) {
if checked {
checkedMap["kube_control_plane"] = "checked"
} else {
delete(checkedMap, "kube_control_plane")
}
})
formEditGroups.AddCheckbox("kube_node", slices.Contains(hostDetails.Groups, "kube_node"), func(checked bool) {
if checked {
checkedMap["kube_node"] = "checked"
} else {
delete(checkedMap, "kube_node")
}
})
formEditGroups.AddCheckbox("etcd", slices.Contains(hostDetails.Groups, "etcd"), func(checked bool) {
if checked {
checkedMap["etcd"] = "checked"
} else {
delete(checkedMap, "etcd")
}
})
formEditGroups.AddCheckbox("calico_rr", slices.Contains(hostDetails.Groups, "calico_rr"), func(checked bool) {
if checked {
checkedMap["calico_rr"] = "checked"
} else {
delete(checkedMap, "calico_rr")
}
})
formEditGroups.AddButton("OK", func() {
for key := range checkedMap {
newGroups = append(newGroups, key)
}
hostDetails.Groups = newGroups
writeBackHostDetails()
flexHostDetails.Clear()
initFlexHostDetails(hostDetails.Hostname, false)
pages.SwitchToPage("Edit Hosts")
})
formEditGroups.AddButton("Cancel", func() {
pages.SwitchToPage("Edit Hosts")
})
}
func initFlexEditNodeLabels() {
flexEditNodeLabels.SetTitle("Edit Node Labels").SetBorder(true)
formEditLabels := tview.NewForm()
formEditLabels.SetBorder(true)
formEditLabels.AddTextView("Hostname: ", hostDetails.Hostname, 0, 3, false, false)
if tmpNodeLabels == "" && len(hostDetails.Node_labels) != 0 {
labels, err := yaml.Marshal(&hostDetails.Node_labels)
check(err)
tmpNodeLabels = string(labels)
}
formEditLabels.AddTextArea("Node Labels: ", tmpNodeLabels, 0, 0, 0, func(text string) {
tmpNodeLabels = text
})
formEditLabels.AddButton("OK", func() {
var newLabels map[string]string
err := yaml.Unmarshal([]byte(tmpNodeLabels), &newLabels)
if err != nil {
showErrorModal("Node labels format (YAML Map) is wrong.",
func(buttonIndex int, buttonLabel string) {
pages.SwitchToPage("Edit Node Labels")
})
} else {
hostDetails.Node_labels = newLabels
writeBackHostDetails()
tmpNodeLabels = ""
flexHostDetails.Clear()
initFlexHostDetails(hostDetails.Hostname, false)
pages.SwitchToPage("Edit Hosts")
}
})
formEditLabels.AddButton("Cancel", func() {
tmpNodeLabels = ""
pages.SwitchToPage("Edit Hosts")
})
formPredefinedLabels := tview.NewForm()
formPredefinedLabels.SetBorder(true)
var options []string
for key, value := range appConfig.Predefined_node_labels {
options = append(options, key+": "+value)
}
slices.Sort(options)
var selectedLabel string
formPredefinedLabels.AddDropDown("Predefined Labels", options, -1, func(option string, optionIndex int) {
selectedLabel = option
})
formPredefinedLabels.AddButton("<< Add", func() {
if selectedLabel != "" {
tmpNodeLabels = tmpNodeLabels + selectedLabel + "\n"
flexEditNodeLabels.Clear()
initFlexEditNodeLabels()
}
})
flexEditNodeLabels.
AddItem(formEditLabels, 0, 2, true).
AddItem(formPredefinedLabels, 0, 1, false)
}
func initFlexEditNodeTaints() {
flexEditNodeTaints.SetTitle("Edit Node Taints").SetBorder(true)
formEditTaints := tview.NewForm()
formEditTaints.SetBorder(true)
formEditTaints.AddTextView("Hostname: ", hostDetails.Hostname, 0, 3, false, false)
if tmpNodeTaints == "" && len(hostDetails.Node_taints) != 0 {
taints, err := yaml.Marshal(&hostDetails.Node_taints)
check(err)
tmpNodeTaints = string(taints)
}
formEditTaints.AddTextArea("Node Taints: ", tmpNodeTaints, 0, 0, 0, func(text string) {
tmpNodeTaints = text
})
formEditTaints.AddButton("OK", func() {
var newTaints []string
err := yaml.Unmarshal([]byte(tmpNodeTaints), &newTaints)
if err != nil {
showErrorModal("Node taints format (YAML Array) is wrong.",
func(buttonIndex int, buttonLabel string) {
pages.SwitchToPage("Edit Node Taints")
})
} else {
hostDetails.Node_taints = newTaints
writeBackHostDetails()
tmpNodeTaints = ""
flexHostDetails.Clear()
initFlexHostDetails(hostDetails.Hostname, false)
pages.SwitchToPage("Edit Hosts")
}
})
formEditTaints.AddButton("Cancel", func() {
tmpNodeTaints = ""
pages.SwitchToPage("Edit Hosts")
})
formPredefinedTaints := tview.NewForm()
formPredefinedTaints.SetBorder(true)
var options []string
for _, value := range appConfig.Predefined_node_taints {
options = append(options, "- "+value)
}
slices.Sort(options)
var selectedTaint string
formPredefinedTaints.AddDropDown("Predefined Taints", options, -1, func(option string, optionIndex int) {
selectedTaint = option
})
formPredefinedTaints.AddButton("<< Add", func() {
if selectedTaint != "" {
tmpNodeTaints = tmpNodeTaints + selectedTaint + "\n"
flexEditNodeTaints.Clear()
initFlexEditNodeTaints()
}
})
flexEditNodeTaints.
AddItem(formEditTaints, 0, 2, true).
AddItem(formPredefinedTaints, 0, 1, false)
}
func getHostDetails(hostname string) {
hostDetails.Hostname = hostname
hostDetails.Ansible_host = inventory.All.Hosts[hostname].Ansible_host
hostDetails.Ip = inventory.All.Hosts[hostname].Ip
hostDetails.Access_ip = inventory.All.Hosts[hostname].Access_ip
hostDetails.Node_labels = inventory.All.Hosts[hostname].Node_labels
hostDetails.Node_taints = inventory.All.Hosts[hostname].Node_taints
var groups []string
if _, ok := inventory.All.Children.Kube_control_plane.Hosts[hostname]; ok {
groups = append(groups, "kube_control_plane")
}
if _, ok := inventory.All.Children.Kube_node.Hosts[hostname]; ok {
groups = append(groups, "kube_node")
}
if _, ok := inventory.All.Children.Etcd.Hosts[hostname]; ok {
groups = append(groups, "etcd")
}
if _, ok := inventory.All.Children.Calico_rr.Hosts[hostname]; ok {
groups = append(groups, "calico_rr")
}
hostDetails.Groups = groups
}
func writeBackHostDetails() {
var h Host
h.Ansible_host = hostDetails.Ansible_host
h.Ip = hostDetails.Ip
h.Access_ip = hostDetails.Access_ip
h.Node_labels = hostDetails.Node_labels
h.Node_taints = hostDetails.Node_taints
if slices.Contains(hostDetails.Groups, "kube_control_plane") {
inventory.All.Children.Kube_control_plane.Hosts[hostDetails.Hostname] = make(map[any]any)
} else {
delete(inventory.All.Children.Kube_control_plane.Hosts, hostDetails.Hostname)
}
if slices.Contains(hostDetails.Groups, "kube_node") {
inventory.All.Children.Kube_node.Hosts[hostDetails.Hostname] = make(map[any]any)
} else {
delete(inventory.All.Children.Kube_node.Hosts, hostDetails.Hostname)
}
if slices.Contains(hostDetails.Groups, "etcd") {
inventory.All.Children.Etcd.Hosts[hostDetails.Hostname] = make(map[any]any)
} else {
delete(inventory.All.Children.Etcd.Hosts, hostDetails.Hostname)
}
if slices.Contains(hostDetails.Groups, "calico_rr") {
inventory.All.Children.Calico_rr.Hosts[hostDetails.Hostname] = make(map[any]any)
} else {
delete(inventory.All.Children.Calico_rr.Hosts, hostDetails.Hostname)
}
inventory.All.Hosts[hostDetails.Hostname] = h
}