forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
184 lines (160 loc) · 5.67 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
175
176
177
178
179
180
181
182
183
184
package watcher
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/rancher/rancher/pkg/controllers/user/alert/manager"
"github.com/rancher/rancher/pkg/node"
"github.com/rancher/rancher/pkg/ticker"
"github.com/rancher/types/apis/core/v1"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
)
type NodeWatcher struct {
machineLister v3.NodeLister
nodeLister v1.NodeLister
clusterAlertLister v3.ClusterAlertLister
alertManager *manager.Manager
clusterName string
}
func StartNodeWatcher(ctx context.Context, cluster *config.UserContext, manager *manager.Manager) {
n := &NodeWatcher{
machineLister: cluster.Management.Management.Nodes(cluster.ClusterName).Controller().Lister(),
nodeLister: cluster.Core.Nodes("").Controller().Lister(),
clusterAlertLister: cluster.Management.Management.ClusterAlerts(cluster.ClusterName).Controller().Lister(),
alertManager: manager,
clusterName: cluster.ClusterName,
}
go n.watch(ctx, syncInterval)
}
func (w *NodeWatcher) watch(ctx context.Context, interval time.Duration) {
for range ticker.Context(ctx, interval) {
err := w.watchRule()
if err != nil {
logrus.Infof("Failed to watch node", err)
}
}
}
func (w *NodeWatcher) watchRule() error {
if w.alertManager.IsDeploy == false {
return nil
}
clusterAlerts, err := w.clusterAlertLister.List("", labels.NewSelector())
if err != nil {
return err
}
machines, err := w.machineLister.List("", labels.NewSelector())
if err != nil {
return err
}
for _, alert := range clusterAlerts {
if alert.Status.AlertState == "inactive" {
continue
}
if alert.Spec.TargetNode.NodeName != "" {
parts := strings.Split(alert.Spec.TargetNode.NodeName, ":")
if len(parts) != 2 {
continue
}
id := parts[1]
machine := getMachineByID(machines, id)
w.checkNodeCondition(alert, machine)
} else if alert.Spec.TargetNode.Selector != nil {
selector := labels.NewSelector()
for key, value := range alert.Spec.TargetNode.Selector {
r, err := labels.NewRequirement(key, selection.Equals, []string{value})
if err != nil {
logrus.Warnf("Fail to create new requirement foo %s: %v", key, err)
continue
}
selector = selector.Add(*r)
}
nodes, err := w.nodeLister.List("", selector)
if err != nil {
logrus.Warnf("Fail to list node: %v", err)
continue
}
for _, node := range nodes {
machine := getMachineByNodeName(machines, node.Name)
w.checkNodeCondition(alert, machine)
}
}
}
return nil
}
func getMachineByID(machines []*v3.Node, id string) *v3.Node {
for _, m := range machines {
if m.Name == id {
return m
}
}
return nil
}
func getMachineByNodeName(machines []*v3.Node, nodeName string) *v3.Node {
for _, m := range machines {
if node.GetNodeName(m) == nodeName {
return m
}
}
return nil
}
func (w *NodeWatcher) checkNodeCondition(alert *v3.ClusterAlert, machine *v3.Node) {
switch alert.Spec.TargetNode.Condition {
case "notready":
w.checkNodeReady(alert, machine)
case "mem":
w.checkNodeMemUsage(alert, machine)
case "cpu":
w.checkNodeCPUUsage(alert, machine)
}
}
func (w *NodeWatcher) checkNodeMemUsage(alert *v3.ClusterAlert, machine *v3.Node) {
alertID := alert.Namespace + "-" + alert.Name
if machine != nil {
total := machine.Status.InternalNodeStatus.Allocatable.Memory()
used := machine.Status.Requested.Memory()
if used.Value()*100.0/total.Value() > int64(alert.Spec.TargetNode.MemThreshold) {
title := fmt.Sprintf("The memory usage on the node %s is over %s%%", node.GetNodeName(machine), strconv.Itoa(alert.Spec.TargetNode.MemThreshold))
//TODO: how to set unit for display for Quantity
desc := fmt.Sprintf("*Alert Name*: %s\n*Cluster Name*: %s\n*Used Memory*: %s\n*Total Memory*: %s", alert.Spec.DisplayName, w.clusterName, used.String(), total.String())
if err := w.alertManager.SendAlert(alertID, desc, title, alert.Spec.Severity); err != nil {
logrus.Debugf("Failed to send alert: %v", err)
}
}
}
}
func (w *NodeWatcher) checkNodeCPUUsage(alert *v3.ClusterAlert, machine *v3.Node) {
alertID := alert.Namespace + "-" + alert.Name
if machine != nil {
total := machine.Status.InternalNodeStatus.Allocatable.Cpu()
used := machine.Status.Requested.Cpu()
if used.MilliValue()*100.0/total.MilliValue() > int64(alert.Spec.TargetNode.CPUThreshold) {
title := fmt.Sprintf("The CPU usage on the node %s is over %s%%", node.GetNodeName(machine), strconv.Itoa(alert.Spec.TargetNode.CPUThreshold))
desc := fmt.Sprintf("*Alert Name*: %s\n*Cluster Name*: %s\n*Used CPU*: %s m\n*Total CPU*: %s m", alert.Spec.DisplayName, w.clusterName, strconv.FormatInt(used.MilliValue(), 10), strconv.FormatInt(total.MilliValue(), 10))
if err := w.alertManager.SendAlert(alertID, desc, title, alert.Spec.Severity); err != nil {
logrus.Debugf("Failed to send alert: %v", err)
}
}
}
}
func (w *NodeWatcher) checkNodeReady(alert *v3.ClusterAlert, machine *v3.Node) {
alertID := alert.Namespace + "-" + alert.Name
for _, cond := range machine.Status.InternalNodeStatus.Conditions {
if cond.Type == corev1.NodeReady {
if cond.Status == corev1.ConditionFalse {
title := fmt.Sprintf("The kubelet on the node %s is not healthy", node.GetNodeName(machine))
desc := fmt.Sprintf("*Alert Name*: %s\n*Cluster Name*: %s\n*Logs*: %s", alert.Spec.DisplayName, w.clusterName, cond.Message)
if err := w.alertManager.SendAlert(alertID, desc, title, alert.Spec.Severity); err != nil {
logrus.Debugf("Failed to send alert: %v", err)
}
return
}
}
}
}