-
Notifications
You must be signed in to change notification settings - Fork 1
/
k8s.go
88 lines (63 loc) · 1.52 KB
/
k8s.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
package main
import (
"context"
"github.com/digineo/go-ping"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"log"
"net"
"os"
"time"
)
func getNodeList() ([]string, error) {
var simpleList []string
// Get configuration from within the cluster itself
config, err := rest.InClusterConfig()
if err != nil {
return simpleList, err
}
c, err := kubernetes.NewForConfig(config)
if err != nil {
return simpleList, err
}
nodes, err := c.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{
Limit: 500,
})
if err != nil {
return simpleList, err
}
for _, items := range nodes.Items {
simpleList = append(simpleList, items.Name)
}
return simpleList, nil
}
func getNodeName() (string, error) {
name := os.Getenv("NODE_NAME")
var err error
// This method is unreliable since it will return just the pod name.
if name == "" {
name, err = os.Hostname()
}
return name, err
}
func pingHost(destination string) (time.Duration, error) {
bind := "0.0.0.0"
var remoteAddr *net.IPAddr
var pinger *ping.Pinger
var rtt time.Duration
if remoteAddr, err := net.ResolveIPAddr("ip4", destination); err != nil {
return rtt, err
}
if pinger, err := ping.New(bind, ""); err != nil {
return rtt, err
}
defer pinger.Close()
timeout, _ := time.ParseDuration("30s")
rtt, err := pinger.PingAttempts(remoteAddr, timeout, int(3))
if err != nil {
return rtt, err
}
log.Printf("ping %s (%s) rtt=%v\n", destination, remoteAddr, rtt)
return rtt, nil
}