-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
101 lines (93 loc) · 2.14 KB
/
app.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
package main
import (
"changeme/k8s"
"context"
"strings"
)
// App struct
type App struct {
ctx context.Context
nodesManager k8s.NodesManager
}
// NewApp creates a new App application struct
func NewApp() *App {
nm, err := k8s.NewNodesManager()
if err != nil {
panic(err) // FIXME
}
return &App{
nodesManager: nm,
}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
type QueriedNodes struct {
Nodes []k8s.Node `json:"nodes"`
Labels map[string][]string `json:"labels"`
}
func indexLabels(nodes []k8s.Node) map[string][]string {
labels := make(map[string][]string)
seen := make(map[string]map[string]struct{})
for _, n := range nodes {
for k, v := range n.Labels {
if _, ok := seen[k]; !ok {
seen[k] = make(map[string]struct{})
}
if _, ok := seen[k][v]; ok {
continue
} else {
seen[k][v] = struct{}{}
labels[k] = append(labels[k], v)
}
}
}
return labels
}
func (a *App) ListNodes(shouldClearCache bool, labelSelectors string) QueriedNodes {
nodes, err := a.nodesManager.GetNodes(a.ctx, shouldClearCache)
if err != nil {
// TODO: Show error message
panic(err)
}
labelsIndexForAllNodes := indexLabels(nodes)
labelSelectors = strings.TrimSpace(labelSelectors)
if len(labelSelectors) == 0 {
return QueriedNodes{
Nodes: nodes,
Labels: labelsIndexForAllNodes,
}
}
var selectors [][2]string
for _, selector := range strings.Split(labelSelectors, ",") {
parts := strings.SplitN(selector, "=", 2)
if len(parts) != 2 {
continue
}
key, value := parts[0], parts[1]
selectors = append(selectors, [2]string{key, value})
}
var selected []k8s.Node
for _, n := range nodes {
for _, selector := range selectors {
k, v := selector[0], selector[1]
if n.Labels[k] == v {
selected = append(selected, n)
break
}
}
}
return QueriedNodes{
Nodes: selected,
Labels: labelsIndexForAllNodes,
}
}
func (a *App) GetCurrentContext() string {
currentContext, err := a.nodesManager.GetCurrentContext()
if err != nil {
panic(err)
}
return currentContext
}