-
Notifications
You must be signed in to change notification settings - Fork 0
/
polling.go
94 lines (72 loc) · 1.66 KB
/
polling.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
package main
// Copyright (c) 2019, Vladimir Masarik
import (
"context"
"log"
"os"
"strconv"
"time"
"go.etcd.io/etcd/clientv3"
)
func getCounter(ctx context.Context, client *clientv3.Client) int {
getResp, err := client.Get(ctx, "counter")
if err != nil {
log.Fatal(err)
}
nodeCount, err := strconv.Atoi(string(getResp.Kvs[0].Value))
if err != nil {
log.Fatal(err)
}
return nodeCount
}
func main() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{"etcd.openshift-logging:2379"},
DialTimeout: 5 * time.Second,
})
if err != nil {
log.Fatal(err)
return
}
defer cli.Close()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
nodeCountBase, err := strconv.Atoi(os.Getenv("NODE_COUNT"))
if err != nil {
log.Fatal(err)
return
}
keyValue := clientv3.NewKV(cli)
_, err = keyValue.Txn(ctx).
If(clientv3.Compare(clientv3.Version("counter"), "<", 1)).
Then(clientv3.OpPut("counter", strconv.Itoa(nodeCountBase))).
Commit()
if err != nil {
log.Fatal(err)
}
success := false
for success == false {
nodeCount := getCounter(ctx, cli)
resp, err = keyValue.Txn(ctx).
If(clientv3.Compare(clientv3.Value("counter"), "=", strconv.Itoa(nodeCount))).
Then(clientv3.OpPut("counter", strconv.Itoa(nodeCount-1))).
Commit()
if err != nil {
log.Fatal(err)
}
success = resp.Succeeded
}
log.Println("Waiting for all pods to complete...")
if getCounter(ctx, cli) == 0 {
return
}
responseChan := cli.Watch(ctx, "counter")
for response := range responseChan {
for _, event := range response.Events {
if string(event.Kv.Value) == "0" {
return
}
}
}
return
}