Skip to content

Commit 8d9eef1

Browse files
authored
feat: enable readyz/healthz endpoints (#2431)
Signed-off-by: Evan Baker <[email protected]>
1 parent 7dd8342 commit 8d9eef1

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

cns/healthserver/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import (
66
"github.com/labstack/echo/v4"
77
"github.com/prometheus/client_golang/prometheus/promhttp"
88
"go.uber.org/zap"
9-
"sigs.k8s.io/controller-runtime/pkg/healthz"
109
"sigs.k8s.io/controller-runtime/pkg/metrics"
1110
)
1211

13-
func Start(log *zap.Logger, addr string) {
12+
func Start(log *zap.Logger, addr string, readyz, healthz http.Handler) {
1413
e := echo.New()
1514
e.HideBanner = true
16-
e.GET("/healthz", echo.WrapHandler(http.StripPrefix("/healthz", &healthz.Handler{})))
15+
e.GET("/healthz", echo.WrapHandler(http.StripPrefix("/healthz", healthz)))
16+
e.GET("/readyz", echo.WrapHandler(http.StripPrefix("/readyz", readyz)))
1717
e.GET("/metrics", echo.WrapHandler(promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{
1818
ErrorHandling: promhttp.HTTPErrorOnError,
1919
})))

cns/service/main.go

+21-10
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import (
6060
"github.com/avast/retry-go/v4"
6161
"github.com/pkg/errors"
6262
"go.uber.org/zap"
63+
"go.uber.org/zap/zapcore"
6364
corev1 "k8s.io/api/core/v1"
6465
apierrors "k8s.io/apimachinery/pkg/api/errors"
6566
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -579,9 +580,24 @@ func main() {
579580
}
580581
}
581582

582-
// start the health server
583-
z, _ := zap.NewProduction()
584-
go healthserver.Start(z, cnsconfig.MetricsBindAddress)
583+
// configure zap logger
584+
zconfig := zap.NewProductionConfig()
585+
zconfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
586+
z, _ := zconfig.Build()
587+
588+
// start the healthz/readyz/metrics server
589+
readyCh := make(chan interface{})
590+
readyChecker := healthz.CheckHandler{
591+
Checker: healthz.Checker(func(*http.Request) error {
592+
select {
593+
default:
594+
return errors.New("not ready")
595+
case <-readyCh:
596+
}
597+
return nil
598+
}),
599+
}
600+
go healthserver.Start(z, cnsconfig.MetricsBindAddress, &healthz.Handler{}, readyChecker)
585601

586602
nmaConfig, err := nmagent.NewConfig(cnsconfig.WireserverIP)
587603
if err != nil {
@@ -953,6 +969,8 @@ func main() {
953969
}
954970
}
955971

972+
// mark the service as "ready"
973+
close(readyCh)
956974
// block until process exiting
957975
<-rootCtx.Done()
958976

@@ -1353,13 +1371,6 @@ func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cn
13531371
httpRestService.AttachSWIFTv2Middleware(&swiftV2Middleware)
13541372
}
13551373

1356-
// adding some routes to the root service mux
1357-
mux := httpRestServiceImplementation.Listener.GetMux()
1358-
mux.Handle("/readyz", http.StripPrefix("/readyz", &healthz.Handler{}))
1359-
if cnsconfig.EnablePprof {
1360-
httpRestServiceImplementation.RegisterPProfEndpoints()
1361-
}
1362-
13631374
// start the pool Monitor before the Reconciler, since it needs to be ready to receive an
13641375
// NodeNetworkConfig update by the time the Reconciler tries to send it.
13651376
go func() {

0 commit comments

Comments
 (0)