-
Notifications
You must be signed in to change notification settings - Fork 443
/
ping.go
271 lines (252 loc) · 7.89 KB
/
ping.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
package pinger
import (
"context"
"fmt"
"math"
"net"
"os"
"strings"
"time"
goping "github.com/oilbeater/go-ping"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"
"github.com/kubeovn/kube-ovn/pkg/util"
)
func StartPinger(config *Configuration, e *Exporter) {
errHappens := false
for {
if config.NetworkMode == "kube-ovn" {
if checkOvs(config) != nil {
errHappens = true
}
if checkOvnController(config) != nil {
errHappens = true
}
if checkPortBindings(config) != nil {
errHappens = true
}
e.ovsMetricsUpdate()
}
if ping(config) != nil {
errHappens = true
}
if config.Mode != "server" {
break
}
time.Sleep(time.Duration(config.Interval) * time.Second)
}
if errHappens && config.ExitCode != 0 {
os.Exit(config.ExitCode)
}
}
func ping(config *Configuration) error {
errHappens := false
if checkApiServer(config) != nil {
errHappens = true
}
if pingPods(config) != nil {
errHappens = true
}
if pingNodes(config) != nil {
errHappens = true
}
if internalNslookup(config) != nil {
errHappens = true
}
if config.ExternalDNS != "" {
if externalNslookup(config) != nil {
errHappens = true
}
}
if config.ExternalAddress != "" {
if pingExternal(config) != nil {
errHappens = true
}
}
if errHappens {
return fmt.Errorf("ping failed")
}
return nil
}
func pingNodes(config *Configuration) error {
klog.Infof("start to check node connectivity")
nodes, err := config.KubeClient.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
if err != nil {
klog.Errorf("failed to list nodes, %v", err)
return err
}
var pingErr error
for _, no := range nodes.Items {
for _, addr := range no.Status.Addresses {
if addr.Type == v1.NodeInternalIP && util.ContainsString(config.PodProtocols, util.CheckProtocol(addr.Address)) {
func(nodeIP, nodeName string) {
pinger, err := goping.NewPinger(nodeIP)
if err != nil {
klog.Errorf("failed to init pinger, %v", err)
pingErr = err
return
}
pinger.SetPrivileged(true)
pinger.Timeout = 30 * time.Second
pinger.Count = 3
pinger.Interval = 100 * time.Millisecond
pinger.Debug = true
pinger.Run()
stats := pinger.Statistics()
klog.Infof("ping node: %s %s, count: %d, loss count %d, average rtt %.2fms",
nodeName, nodeIP, pinger.Count, int(math.Abs(float64(stats.PacketsSent-stats.PacketsRecv))), float64(stats.AvgRtt)/float64(time.Millisecond))
if int(math.Abs(float64(stats.PacketsSent-stats.PacketsRecv))) != 0 {
pingErr = fmt.Errorf("ping failed")
}
SetNodePingMetrics(
config.NodeName,
config.HostIP,
config.PodName,
no.Name, addr.Address,
float64(stats.AvgRtt)/float64(time.Millisecond),
int(math.Abs(float64(stats.PacketsSent-stats.PacketsRecv))),
int(float64(stats.PacketsSent)))
}(addr.Address, no.Name)
}
}
}
return pingErr
}
func pingPods(config *Configuration) error {
klog.Infof("start to check pod connectivity")
ds, err := config.KubeClient.AppsV1().DaemonSets(config.DaemonSetNamespace).Get(context.Background(), config.DaemonSetName, metav1.GetOptions{})
if err != nil {
klog.Errorf("failed to get peer ds: %v", err)
return err
}
pods, err := config.KubeClient.CoreV1().Pods(config.DaemonSetNamespace).List(context.Background(), metav1.ListOptions{LabelSelector: labels.Set(ds.Spec.Selector.MatchLabels).String()})
if err != nil {
klog.Errorf("failed to list peer pods: %v", err)
return err
}
var pingErr error
for _, pod := range pods.Items {
for _, podIP := range pod.Status.PodIPs {
if util.ContainsString(config.PodProtocols, util.CheckProtocol(podIP.IP)) {
func(podIp, podName, nodeIP, nodeName string) {
pinger, err := goping.NewPinger(podIp)
if err != nil {
klog.Errorf("failed to init pinger, %v", err)
pingErr = err
return
}
pinger.SetPrivileged(true)
pinger.Timeout = 1 * time.Second
pinger.Debug = true
pinger.Count = 3
pinger.Interval = 1 * time.Millisecond
pinger.Run()
stats := pinger.Statistics()
klog.Infof("ping pod: %s %s, count: %d, loss count %d, average rtt %.2fms",
podName, podIp, pinger.Count, int(math.Abs(float64(stats.PacketsSent-stats.PacketsRecv))), float64(stats.AvgRtt)/float64(time.Millisecond))
if int(math.Abs(float64(stats.PacketsSent-stats.PacketsRecv))) != 0 {
pingErr = fmt.Errorf("ping failed")
}
SetPodPingMetrics(
config.NodeName,
config.HostIP,
config.PodName,
nodeName,
nodeIP,
podIp,
float64(stats.AvgRtt)/float64(time.Millisecond),
int(math.Abs(float64(stats.PacketsSent-stats.PacketsRecv))),
int(float64(stats.PacketsSent)))
}(podIP.IP, pod.Name, pod.Status.HostIP, pod.Spec.NodeName)
}
}
}
return pingErr
}
func pingExternal(config *Configuration) error {
if config.ExternalAddress == "" {
return nil
}
addresses := strings.Split(config.ExternalAddress, ",")
for _, addr := range addresses {
if !util.ContainsString(config.PodProtocols, util.CheckProtocol(addr)) {
continue
}
klog.Infof("start to check ping external to %s", addr)
pinger, err := goping.NewPinger(addr)
if err != nil {
klog.Errorf("failed to init pinger, %v", err)
return err
}
pinger.SetPrivileged(true)
pinger.Timeout = 5 * time.Second
pinger.Debug = true
pinger.Count = 3
pinger.Interval = 1 * time.Millisecond
pinger.Run()
stats := pinger.Statistics()
klog.Infof("ping external address: %s, total count: %d, loss count %d, average rtt %.2fms",
addr, pinger.Count, int(math.Abs(float64(stats.PacketsSent-stats.PacketsRecv))), float64(stats.AvgRtt)/float64(time.Millisecond))
SetExternalPingMetrics(
config.NodeName,
config.HostIP,
config.PodIP,
addr,
float64(stats.AvgRtt)/float64(time.Millisecond),
int(math.Abs(float64(stats.PacketsSent-stats.PacketsRecv))))
if int(math.Abs(float64(stats.PacketsSent-stats.PacketsRecv))) != 0 {
return fmt.Errorf("ping failed")
}
}
return nil
}
func internalNslookup(config *Configuration) error {
klog.Infof("start to check dns connectivity")
t1 := time.Now()
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
var r net.Resolver
addrs, err := r.LookupHost(ctx, config.InternalDNS)
elapsed := time.Since(t1)
if err != nil {
klog.Errorf("failed to resolve dns %s, %v", config.InternalDNS, err)
SetInternalDnsUnhealthyMetrics(config.NodeName)
return err
}
SetInternalDnsHealthyMetrics(config.NodeName, float64(elapsed)/float64(time.Millisecond))
klog.Infof("resolve dns %s to %v in %.2fms", config.InternalDNS, addrs, float64(elapsed)/float64(time.Millisecond))
return nil
}
func externalNslookup(config *Configuration) error {
klog.Infof("start to check dns connectivity")
t1 := time.Now()
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
var r net.Resolver
addrs, err := r.LookupHost(ctx, config.ExternalDNS)
elapsed := time.Since(t1)
if err != nil {
klog.Errorf("failed to resolve dns %s, %v", config.ExternalDNS, err)
SetExternalDnsUnhealthyMetrics(config.NodeName)
return err
}
SetExternalDnsHealthyMetrics(config.NodeName, float64(elapsed)/float64(time.Millisecond))
klog.Infof("resolve dns %s to %v in %.2fms", config.ExternalDNS, addrs, float64(elapsed)/float64(time.Millisecond))
return nil
}
func checkApiServer(config *Configuration) error {
klog.Infof("start to check apiserver connectivity")
t1 := time.Now()
_, err := config.KubeClient.Discovery().ServerVersion()
elapsed := time.Since(t1)
if err != nil {
klog.Errorf("failed to connect to apiserver: %v", err)
SetApiserverUnhealthyMetrics(config.NodeName)
return err
}
klog.Infof("connect to apiserver success in %.2fms", float64(elapsed)/float64(time.Millisecond))
SetApiserverHealthyMetrics(config.NodeName, float64(elapsed)/float64(time.Millisecond))
return nil
}