Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable readyz/healthz endpoints #2431

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cns/healthserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/zap"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/metrics"
)

func Start(log *zap.Logger, addr string) {
func Start(log *zap.Logger, addr string, readyz, healthz http.Handler) {
e := echo.New()
e.HideBanner = true
e.GET("/healthz", echo.WrapHandler(http.StripPrefix("/healthz", &healthz.Handler{})))
e.GET("/healthz", echo.WrapHandler(http.StripPrefix("/healthz", healthz)))
e.GET("/readyz", echo.WrapHandler(http.StripPrefix("/readyz", readyz)))
e.GET("/metrics", echo.WrapHandler(promhttp.HandlerFor(metrics.Registry, promhttp.HandlerOpts{
ErrorHandling: promhttp.HTTPErrorOnError,
})))
Expand Down
31 changes: 21 additions & 10 deletions cns/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import (
"github.com/avast/retry-go/v4"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -579,9 +580,24 @@ func main() {
}
}

// start the health server
z, _ := zap.NewProduction()
go healthserver.Start(z, cnsconfig.MetricsBindAddress)
// configure zap logger
zconfig := zap.NewProductionConfig()
zconfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
z, _ := zconfig.Build()

// start the healthz/readyz/metrics server
readyCh := make(chan interface{})
readyChecker := healthz.CheckHandler{
Checker: healthz.Checker(func(*http.Request) error {
select {
default:
return errors.New("not ready")
case <-readyCh:
}
return nil
}),
}
go healthserver.Start(z, cnsconfig.MetricsBindAddress, &healthz.Handler{}, readyChecker)

nmaConfig, err := nmagent.NewConfig(cnsconfig.WireserverIP)
if err != nil {
Expand Down Expand Up @@ -953,6 +969,8 @@ func main() {
}
}

// mark the service as "ready"
close(readyCh)
// block until process exiting
<-rootCtx.Done()

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

// adding some routes to the root service mux
mux := httpRestServiceImplementation.Listener.GetMux()
mux.Handle("/readyz", http.StripPrefix("/readyz", &healthz.Handler{}))
if cnsconfig.EnablePprof {
httpRestServiceImplementation.RegisterPProfEndpoints()
}

// start the pool Monitor before the Reconciler, since it needs to be ready to receive an
// NodeNetworkConfig update by the time the Reconciler tries to send it.
go func() {
Expand Down
Loading