Skip to content

Commit 581b1f8

Browse files
committed
named checks if verbose is used
Signed-off-by: Imran Pochi <[email protected]>
1 parent 7eb8d62 commit 581b1f8

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

cmd/agent/app/server.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package app
1818

1919
import (
20+
"bytes"
2021
"context"
2122
"crypto/tls"
2223
"fmt"
@@ -26,6 +27,7 @@ import (
2627
"runtime"
2728
runpprof "runtime/pprof"
2829
"strconv"
30+
"strings"
2931
"time"
3032

3133
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -107,14 +109,38 @@ func (a *Agent) runHealthServer(o *options.GrpcProxyAgentOptions, cs agent.Readi
107109
livenessHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
108110
fmt.Fprintf(w, "ok")
109111
})
112+
113+
checks := []agent.HealthChecker{agent.Ping, agent.NewServerConnected(cs)}
110114
readinessHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
111-
if cs.Ready() {
112-
w.WriteHeader(http.StatusOK)
113-
fmt.Fprintf(w, "ok")
114-
} else {
115+
var failedChecks []string
116+
var individualCheckOutput bytes.Buffer
117+
for _, check := range checks {
118+
if err := check.Check(r); err != nil {
119+
fmt.Fprintf(&individualCheckOutput, "[-]%s failed: %v\n", check.Name(), err)
120+
failedChecks = append(failedChecks, check.Name())
121+
} else {
122+
fmt.Fprintf(&individualCheckOutput, "[+]%s ok\n", check.Name())
123+
}
124+
}
125+
126+
// Always be verbose if the check has failed
127+
if len(failedChecks) > 0 {
128+
klog.V(0).Infoln("%s check failed: \n%v", strings.Join(failedChecks, ","), individualCheckOutput.String())
115129
w.WriteHeader(http.StatusServiceUnavailable)
116-
fmt.Fprintf(w, "not ready")
130+
fmt.Fprintf(w, individualCheckOutput.String())
131+
return
117132
}
133+
134+
if _, found := r.URL.Query()["verbose"]; !found {
135+
w.WriteHeader(http.StatusOK)
136+
fmt.Fprint(w, "ok")
137+
return
138+
}
139+
140+
fmt.Fprintf(&individualCheckOutput, "check passed\n")
141+
142+
w.WriteHeader(http.StatusOK)
143+
fmt.Fprint(w, individualCheckOutput.String())
118144
})
119145

120146
muxHandler := http.NewServeMux()

pkg/agent/readiness.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ limitations under the license.
1616

1717
package agent
1818

19+
import (
20+
"fmt"
21+
"net/http"
22+
)
23+
1924
// ReadinessManager supports checking if the agent is ready.
2025
type ReadinessManager interface {
2126
// Ready returns true the proxy server is ready.
@@ -28,3 +33,46 @@ func (cs *ClientSet) Ready() bool {
2833
// Returns true if the agent is connected to at least one server.
2934
return cs.HealthyClientsCount() > 0
3035
}
36+
37+
// HealthChecker represents an entity capable of performing health checks.
38+
type HealthChecker interface {
39+
Name() string
40+
Check(req *http.Request) error
41+
}
42+
43+
// ping implements the simplest possible healthz checker.
44+
type ping struct{}
45+
46+
var Ping HealthChecker = &ping{}
47+
48+
func (p *ping) Name() string {
49+
return "ping"
50+
}
51+
52+
func (p *ping) Check(_ *http.Request) error {
53+
return nil
54+
}
55+
56+
type serverConnected struct {
57+
rm ReadinessManager
58+
}
59+
60+
var ServerConnected HealthChecker = &serverConnected{}
61+
62+
func NewServerConnected(cs ReadinessManager) HealthChecker {
63+
return &serverConnected{
64+
rm: cs,
65+
}
66+
}
67+
68+
func (s *serverConnected) Name() string {
69+
return "server-connected"
70+
}
71+
72+
func (s *serverConnected) Check(_ *http.Request) error {
73+
if s.rm.Ready() {
74+
return nil
75+
}
76+
77+
return fmt.Errorf("no servers connected")
78+
}

0 commit comments

Comments
 (0)