Skip to content

Commit

Permalink
Add API Server Reachability Check (#2959)
Browse files Browse the repository at this point in the history
* get api server ping code

* add emit gauge for healthz ping code

---------

Co-authored-by: Nicolas Ontiveros <[email protected]>
  • Loading branch information
niontive and Nicolas Ontiveros authored Jul 5, 2023
1 parent d82d4f5 commit 629913b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
12 changes: 6 additions & 6 deletions pkg/monitor/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,18 @@ func (mon *Monitor) Monitor(ctx context.Context) (errs []error) {
})
}

// If API is not returning 200, don't need to run the next checks
statusCode, err := mon.emitAPIServerHealthzCode(ctx)
if err != nil {
// If API Server is unreachable, don't need to run the next checks
statusCode, err := mon.getAPIServerPingCode(ctx)
if err != nil || statusCode != http.StatusOK {
errs = append(errs, err)
friendlyFuncName := steps.FriendlyName(mon.emitAPIServerHealthzCode)
friendlyFuncName := steps.FriendlyName(mon.getAPIServerPingCode)
mon.log.Printf("%s: %s", friendlyFuncName, err)
mon.emitGauge("monitor.clustererrors", 1, map[string]string{"monitor": friendlyFuncName})
}
if statusCode != http.StatusOK {
return
}

for _, f := range []func(context.Context) error{
mon.emitAPIServerHealthzCode,
mon.emitAroOperatorHeartbeat,
mon.emitAroOperatorConditions,
mon.emitNSGReconciliation,
Expand Down
18 changes: 17 additions & 1 deletion pkg/monitor/cluster/healthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
)

func (mon *Monitor) emitAPIServerHealthzCode(ctx context.Context) (int, error) {
func (mon *Monitor) emitAPIServerHealthzCode(ctx context.Context) error {
var statusCode int
err := mon.cli.Discovery().RESTClient().
Get().
Expand All @@ -21,5 +21,21 @@ func (mon *Monitor) emitAPIServerHealthzCode(ctx context.Context) (int, error) {
"code": strconv.FormatInt(int64(statusCode), 10),
})

return err
}

func (mon *Monitor) getAPIServerPingCode(ctx context.Context) (int, error) {
var statusCode int
err := mon.cli.Discovery().RESTClient().
Get().
AbsPath("/healthz/ping").
Do(ctx).
StatusCode(&statusCode).
Error()

mon.emitGauge("apiserver.healthz.ping.code", 1, map[string]string{
"code": strconv.FormatInt(int64(statusCode), 10),
})

return statusCode, err
}

0 comments on commit 629913b

Please sign in to comment.