DevilKing's blog

冷灯看剑,剑上几分功名?炉香无需计苍生,纵一穿烟逝,万丈云埋,孤阳还照古陵

0%

Configmap Reload

repo链接

It watches mounted volume dirs and notifies the target process that the config map has been changed. It currently only supports sending an HTTP request, but in future it is expected to support sending OS (e.g. SIGHUP) once Kubernetes supports pod PID namespaces.

主要是利用fsnotify的方式

比较好的基于重试的写法

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
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)
}
}

successfulReloadWebhook := false

for retries := *webhookRetries; retries != 0; retries-- {
log.Printf("performing webhook request (%d/%d)", retries, *webhookRetries)
resp, err := http.DefaultClient.Do(req)
if err != nil {
setFailureMetrics(h.String(), "client_request_do")
log.Println("error:", err)
time.Sleep(time.Second * 10)
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)
time.Sleep(time.Second * 10)
continue
}

setSuccessMetrics(h.String(), begun)
log.Println("successfully triggered reload")
successfulReloadWebhook = true
break
}

if !successfulReloadWebhook {
setFailureMetrics(h.String(), "retries_exhausted")
log.Println("error:", "Webhook reload retries exhausted")
}