-
-
Notifications
You must be signed in to change notification settings - Fork 285
/
node.go
174 lines (148 loc) Β· 3.77 KB
/
node.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Popeye
package lint
import (
"context"
"errors"
"github.com/derailed/popeye/internal"
"github.com/derailed/popeye/internal/client"
"github.com/derailed/popeye/internal/db"
"github.com/derailed/popeye/internal/issues"
v1 "k8s.io/api/core/v1"
)
type (
tolerations map[string]struct{}
// Node represents a Node linter.
Node struct {
*issues.Collector
db *db.DB
}
)
// NewNode returns a new instance.
func NewNode(co *issues.Collector, db *db.DB) *Node {
return &Node{
Collector: co,
db: db,
}
}
// Lint cleanse the resource.
func (n *Node) Lint(ctx context.Context) error {
nmx := make(client.NodesMetrics)
n.nodesMetrics(nmx)
tt, err := n.fetchPodTolerations()
if err != nil {
return err
}
txn, it := n.db.MustITFor(internal.Glossary[internal.NO])
defer txn.Abort()
for o := it.Next(); o != nil; o = it.Next() {
no := o.(*v1.Node)
fqn := no.Name
n.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, SpecFor(fqn, no))
n.checkConditions(ctx, no)
if err := n.checkTaints(ctx, no.Spec.Taints, tt); err != nil {
n.AddErr(ctx, err)
}
n.checkUtilization(ctx, nmx[fqn])
}
return nil
}
func (n *Node) checkTaints(ctx context.Context, taints []v1.Taint, tt tolerations) error {
for _, ta := range taints {
if _, ok := tt[mkKey(ta.Key, ta.Value)]; !ok {
n.AddCode(ctx, 700, ta.Key)
}
}
return nil
}
func (n *Node) fetchPodTolerations() (tolerations, error) {
tt := make(tolerations)
txn, it := n.db.MustITFor(internal.Glossary[internal.PO])
defer txn.Abort()
for o := it.Next(); o != nil; o = it.Next() {
po, ok := o.(*v1.Pod)
if !ok {
return nil, errors.New("po conversion failed")
}
for _, t := range po.Spec.Tolerations {
tt[mkKey(t.Key, t.Value)] = struct{}{}
}
}
return tt, nil
}
func mkKey(k, v string) string {
return k + ":" + v
}
func (n *Node) checkConditions(ctx context.Context, no *v1.Node) {
if no.Spec.Unschedulable {
n.AddCode(ctx, 711)
}
for _, c := range no.Status.Conditions {
if c.Status == v1.ConditionUnknown {
n.AddCode(ctx, 701)
}
if c.Type == v1.NodeReady && c.Status == v1.ConditionFalse {
n.AddCode(ctx, 702)
}
n.statusReport(ctx, c.Type, c.Status)
}
}
func (n *Node) statusReport(ctx context.Context, cond v1.NodeConditionType, status v1.ConditionStatus) {
if status == v1.ConditionFalse {
return
}
switch cond {
case v1.NodeMemoryPressure:
n.AddCode(ctx, 704)
case v1.NodeDiskPressure:
n.AddCode(ctx, 705)
case v1.NodePIDPressure:
n.AddCode(ctx, 706)
case v1.NodeNetworkUnavailable:
n.AddCode(ctx, 707)
}
}
func (n *Node) checkUtilization(ctx context.Context, mx client.NodeMetrics) {
if mx.Empty() {
n.AddCode(ctx, 708)
return
}
percCPU := ToPerc(toMC(mx.CurrentCPU), toMC(mx.AvailableCPU))
cpuLimit := int64(n.NodeCPULimit())
if percCPU > cpuLimit {
n.AddCode(ctx, 709, cpuLimit, percCPU)
}
percMEM := ToPerc(toMB(mx.CurrentMEM), toMB(mx.AvailableMEM))
memLimit := int64(n.NodeMEMLimit())
if percMEM > memLimit {
n.AddCode(ctx, 710, memLimit, percMEM)
}
}
func (n *Node) nodesMetrics(nmx client.NodesMetrics) {
mm, err := n.db.ListNMX()
if err != nil || len(mm) == 0 {
return
}
txn, it := n.db.MustITFor(internal.Glossary[internal.NO])
defer txn.Abort()
for o := it.Next(); o != nil; o = it.Next() {
no := o.(*v1.Node)
if len(no.Status.Allocatable) == 0 && len(no.Status.Capacity) == 0 {
continue
}
nmx[no.Name] = client.NodeMetrics{
AvailableCPU: *no.Status.Allocatable.Cpu(),
AvailableMEM: *no.Status.Allocatable.Memory(),
TotalCPU: *no.Status.Capacity.Cpu(),
TotalMEM: *no.Status.Capacity.Memory(),
}
}
for _, m := range mm {
if mx, ok := nmx[m.Name]; ok {
mx.CurrentCPU = *m.Usage.Cpu()
mx.CurrentMEM = *m.Usage.Memory()
nmx[m.Name] = mx
}
}
}