-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (75 loc) · 1.95 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
99
100
101
102
103
package main
import (
"encoding/json"
"fmt"
"net/http"
"os"
log "github.com/sirupsen/logrus"
)
type Providers struct {
Kubernetes Kubernetes `json:"kubernetes"`
}
type Kubernetes struct {
Backends map[string]interface{} `json:"backends"`
Frontends Frontends `json:"frontends"`
}
type Frontends map[string]interface{}
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
func countFrontends(client HTTPClient) (int, error) {
var providers Providers
req, err := http.NewRequest(http.MethodGet, "http://localhost:8080/api/providers", nil)
if err != nil {
return 0, err
}
resp, err := client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&providers)
if err != nil {
return 0, err
}
return len(providers.Kubernetes.Frontends), nil
}
func main() {
// start with Service Unavailable
code := 503
curFrontends := 0
prevFrontends := 0
serverInitialized := false
logLevelString, ok := os.LookupEnv("LOG_LEVEL")
if !ok {
logLevelString = "info"
}
logLevel, err := log.ParseLevel(logLevelString)
if err != nil {
logLevel = log.DebugLevel
}
log.SetLevel(logLevel)
log.Info("startup probe server started")
client := &http.Client{}
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if !serverInitialized {
if curFrontends, err = countFrontends(client); err != nil {
log.Error(err)
}
log.Debug(fmt.Sprintf("frontends: %v (previous: %v)", curFrontends, prevFrontends))
if curFrontends != 0 && curFrontends == prevFrontends {
code = 200
serverInitialized = true
}
}
prevFrontends = curFrontends
log.Debug(fmt.Sprintf("serverInitialized: %v", serverInitialized))
w.WriteHeader(code)
_, err = w.Write([]byte(http.StatusText(code)))
if err != nil {
log.Error(err)
}
log.Info(fmt.Sprintf("%v %v %v", r.URL, code, r.UserAgent()))
})
log.Fatal(http.ListenAndServe(":8083", nil))
}