-
Notifications
You must be signed in to change notification settings - Fork 0
/
podwatcher.go
55 lines (50 loc) · 1.14 KB
/
podwatcher.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
package pkg
import (
"fmt"
"net"
"time"
)
func getListOfPods(headlesssvc string) ([]string, error) {
var pods = make([]string, 0)
ips, err := net.LookupIP(headlesssvc)
if err != nil {
return nil, fmt.Errorf("Error in nslookup %+v", err)
}
for _, ip := range ips {
pods = append(pods, ip.String())
}
return pods, nil
}
func difference(a, b []string) []string {
mb := make(map[string]struct{}, len(b))
for _, x := range b {
mb[x] = struct{}{}
}
var diff []string
for _, x := range a {
if _, found := mb[x]; !found {
diff = append(diff, x)
}
}
return diff
}
func Watch(headlessService string, interval time.Duration, newPodCallBack func(address []string), failPodCallback func(address []string)) {
var pods []string
for {
currentPods, err := getListOfPods(headlessService)
if err != nil {
fmt.Printf("Error in retriving pods %+v\n", err)
} else {
newPods := difference(currentPods, pods)
if len(newPods) > 0 {
newPodCallBack(newPods)
}
failedPods := difference(pods, currentPods)
if len(failedPods) > 0 {
failPodCallback(failedPods)
}
pods = currentPods
}
time.Sleep(interval * time.Second)
}
}