-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
98 lines (81 loc) · 2.09 KB
/
main.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
package main
import (
"context"
"os"
"os/exec"
"path"
"text/template"
"time"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"go.etcd.io/etcd/clientv3"
)
type Version struct {
PilotVersion string
ProdVersion string
}
func main() {
viper.AutomaticEnv()
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{viper.GetString("ETCD_URLS")},
DialTimeout: 5 * time.Second,
})
if err != nil {
logrus.Fatal("Could not connect to etcd")
panic(err)
}
defer cli.Close()
var versions Version
versionsChan := make(chan Version)
go versions.watchUpdatedVersions(cli, versionsChan)
for {
select {
case versions := <-versionsChan:
generateAlertFile(versions)
updatePrometheus()
}
}
}
func generateAlertFile(v Version) {
logrus.Info("Generate alert file")
tmpl, err := template.ParseFiles("/etc/prometheus/alert-rules.yml.tmpl")
if err != nil {
panic(err)
}
f, err := os.Create("/etc/prometheus/comparative-alerts.yml")
if err != nil {
panic(err)
}
err = tmpl.Execute(f, v)
if err != nil {
panic(err)
}
}
func (v Version) watchUpdatedVersions(cli *clientv3.Client, versionsChan chan Version) {
watchChan := cli.Watch(context.TODO(), "/versions", clientv3.WithPrefix())
for {
rspProd, err := cli.Get(context.TODO(), "/versions/"+viper.GetString("REGISTRY_SERVICE")+"/prod_version", clientv3.WithPrefix())
rspPilot, err := cli.Get(context.TODO(), "/versions/"+viper.GetString("REGISTRY_SERVICE")+"/pilot_version", clientv3.WithPrefix())
if err != nil {
panic(err)
}
if len(rspProd.Kvs) == 0 || len(rspPilot.Kvs) == 0 {
logrus.Warn("Pilot or Prod version not found")
} else {
prodPath := string(rspProd.Kvs[len(rspProd.Kvs)-1].Key)
pilotPath := string(rspPilot.Kvs[len(rspPilot.Kvs)-1].Key)
v.ProdVersion = path.Base(prodPath)
v.PilotVersion = path.Base(pilotPath)
versionsChan <- v
}
<-watchChan
}
}
func updatePrometheus() {
logrus.Info("Updating prometheus alert files")
cmd := exec.Command("wget", "--post-data=''", "http://localhost:9090/-/reload", "-O", "-")
err := cmd.Run()
if err != nil {
logrus.Warn(err)
}
}