forked from jimmidyson/configmap-reload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configmap-reload.go
212 lines (189 loc) · 5.8 KB
/
configmap-reload.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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"time"
fsnotify "github.com/fsnotify/fsnotify"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const namespace = "configmap_reload"
var (
volumeDirs volumeDirsFlag
webhook webhookFlag
webhookMethod = flag.String("webhook-method", "POST", "the HTTP method url to use to send the webhook")
webhookStatusCode = flag.Int("webhook-status-code", 200, "the HTTP status code indicating successful triggering of reload")
listenAddress = flag.String("web.listen-address", ":9533", "Address to listen on for web interface and telemetry.")
metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
lastReloadError = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "last_reload_error",
Help: "Whether the last reload resulted in an error (1 for error, 0 for success)",
}, []string{"webhook"})
requestDuration = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "last_request_duration_seconds",
Help: "Duration of last webhook request",
}, []string{"webhook"})
successReloads = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "success_reloads_total",
Help: "Total success reload calls",
}, []string{"webhook"})
requestErrorsByReason = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "request_errors_total",
Help: "Total request errors by reason",
}, []string{"webhook", "reason"})
watcherErrors = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "watcher_errors_total",
Help: "Total filesystem watcher errors",
})
requestsByStatusCode = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "requests_total",
Help: "Total requests by response status code",
}, []string{"webhook", "status_code"})
)
func init() {
prometheus.MustRegister(lastReloadError)
prometheus.MustRegister(requestDuration)
prometheus.MustRegister(successReloads)
prometheus.MustRegister(requestErrorsByReason)
prometheus.MustRegister(watcherErrors)
prometheus.MustRegister(requestsByStatusCode)
}
func main() {
flag.Var(&volumeDirs, "volume-dir", "the config map volume directory to watch for updates; may be used multiple times")
flag.Var(&webhook, "webhook-url", "the url to send a request to when the specified config map volume directory has been updated")
flag.Parse()
if len(volumeDirs) < 1 {
log.Println("Missing volume-dir")
log.Println()
flag.Usage()
os.Exit(1)
}
if len(webhook) < 1 {
log.Println("Missing webhook-url")
log.Println()
flag.Usage()
os.Exit(1)
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
defer watcher.Close()
go func() {
for {
select {
case event := <-watcher.Events:
if !isValidEvent(event) {
continue
}
log.Println("config map updated")
for _, h := range webhook {
begun := time.Now()
req, err := http.NewRequest(*webhookMethod, h.String(), nil)
if err != nil {
setFailureMetrics(h.String(), "client_request_create")
log.Println("error:", err)
continue
}
userInfo := h.User
if userInfo != nil {
if password, passwordSet := userInfo.Password(); passwordSet {
req.SetBasicAuth(userInfo.Username(), password)
}
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
setFailureMetrics(h.String(), "client_request_do")
log.Println("error:", err)
continue
}
resp.Body.Close()
requestsByStatusCode.WithLabelValues(h.String(), strconv.Itoa(resp.StatusCode)).Inc()
if resp.StatusCode != *webhookStatusCode {
setFailureMetrics(h.String(), "client_response")
log.Println("error:", "Received response code", resp.StatusCode, ", expected", *webhookStatusCode)
continue
}
setSuccessMetrict(h.String(), begun)
log.Println("successfully triggered reload")
}
case err := <-watcher.Errors:
watcherErrors.Inc()
log.Println("error:", err)
}
}
}()
for _, d := range volumeDirs {
log.Printf("Watching directory: %q", d)
err = watcher.Add(d)
if err != nil {
log.Fatal(err)
}
}
log.Fatal(serverMetrics(*listenAddress, *metricPath))
}
func setFailureMetrics(h, reason string) {
requestErrorsByReason.WithLabelValues(h, reason).Inc()
lastReloadError.WithLabelValues(h).Set(1.0)
}
func setSuccessMetrict(h string, begun time.Time) {
requestDuration.WithLabelValues(h).Set(time.Since(begun).Seconds())
successReloads.WithLabelValues(h).Inc()
lastReloadError.WithLabelValues(h).Set(0.0)
}
func isValidEvent(event fsnotify.Event) bool {
if event.Op&fsnotify.Create != fsnotify.Create {
return false
}
if filepath.Base(event.Name) != "..data" {
return false
}
return true
}
func serverMetrics(listenAddress, metricsPath string) error {
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>ConfigMap Reload Metrics</title></head>
<body>
<h1>ConfigMap Reload</h1>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</body>
</html>
`))
})
return http.ListenAndServe(listenAddress, nil)
}
type volumeDirsFlag []string
type webhookFlag []*url.URL
func (v *volumeDirsFlag) Set(value string) error {
*v = append(*v, value)
return nil
}
func (v *volumeDirsFlag) String() string {
return fmt.Sprint(*v)
}
func (v *webhookFlag) Set(value string) error {
u, err := url.Parse(value)
if err != nil {
return fmt.Errorf("invalid URL: %v", err)
}
*v = append(*v, u)
return nil
}
func (v *webhookFlag) String() string {
return fmt.Sprint(*v)
}