...
1 package entrypoint
2
3 import (
4 "context"
5 "net"
6 "net/http"
7 "net/http/httputil"
8 "net/http/pprof"
9 "net/url"
10
11 _ "k8s.io/client-go/plugin/pkg/client/auth"
12 _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
13
14 "github.com/datawire/ambassador/v2/pkg/acp"
15 "github.com/datawire/ambassador/v2/pkg/debug"
16 "github.com/datawire/dlib/dhttp"
17 )
18
19 func handleCheckAlive(w http.ResponseWriter, r *http.Request, ambwatch *acp.AmbassadorWatcher) {
20
21 ambwatch.FetchEnvoyReady(r.Context())
22
23
24 ok := ambwatch.IsAlive()
25
26 if ok {
27 _, _ = w.Write([]byte("Ambassador is alive and well\n"))
28 } else {
29 http.Error(w, "Ambassador is not alive\n", http.StatusServiceUnavailable)
30 }
31 }
32
33 func handleCheckReady(w http.ResponseWriter, r *http.Request, ambwatch *acp.AmbassadorWatcher) {
34
35
36
37
38
39 ambwatch.FetchEnvoyReady(r.Context())
40
41 ok := ambwatch.IsReady()
42
43 if ok {
44 _, _ = w.Write([]byte("Ambassador is ready and waiting\n"))
45 } else {
46 http.Error(w, "Ambassador is not ready\n", http.StatusServiceUnavailable)
47 }
48 }
49
50 func healthCheckHandler(ctx context.Context, ambwatch *acp.AmbassadorWatcher) error {
51 dbg := debug.FromContext(ctx)
52
53
54
55 sm := http.NewServeMux()
56
57
58
59
60 livenessTimer := dbg.Timer("check_alive")
61 sm.HandleFunc("/ambassador/v0/check_alive",
62 livenessTimer.TimedHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
63 handleCheckAlive(w, r, ambwatch)
64 }))
65
66 readinessTimer := dbg.Timer("check_ready")
67 sm.HandleFunc("/ambassador/v0/check_ready",
68 readinessTimer.TimedHandlerFunc(func(w http.ResponseWriter, r *http.Request) {
69 handleCheckReady(w, r, ambwatch)
70 }))
71
72
73 sm.Handle("/debug", dbg)
74
75
76 sm.HandleFunc("/debug/pprof/", pprof.Index)
77
78
79
80
81 diagdOrigin, _ := url.Parse("http://127.0.0.1:8004/")
82
83
84
85
86
87 reverseProxy := &httputil.ReverseProxy{
88 Director: func(req *http.Request) {
89 req.URL.Scheme = diagdOrigin.Scheme
90 req.URL.Host = diagdOrigin.Host
91
92
93 if acp.HostPortIsLocal(req.RemoteAddr) {
94 req.Header.Set("X-Ambassador-Diag-IP", "127.0.0.1")
95 }
96 },
97 }
98
99
100
101 sm.HandleFunc("/", reverseProxy.ServeHTTP)
102
103
104
105
106
107
108
109
110
111
112
113 listener, err := net.Listen("tcp4", ":8877")
114 if err != nil {
115 return err
116 }
117
118 s := &dhttp.ServerConfig{
119 Handler: sm,
120 }
121
122 return s.Serve(ctx, listener)
123 }
124
View as plain text