refactor: rename probe to healthzer

This commit is contained in:
Kevin Franklin Kim 2022-03-14 14:09:28 +01:00
parent 264917cc87
commit 03ffa38139
7 changed files with 70 additions and 38 deletions

View File

@ -2,6 +2,20 @@ package keel
import "context"
type closer struct {
handle func(context.Context) error
}
func NewCloserFn(handle func(context.Context) error) closer {
return closer{
handle: handle,
}
}
func (h healther) Close(ctx context.Context) error {
return h.handle(ctx)
}
// Closer interface
type Closer interface {
Close()

View File

@ -1,6 +1,7 @@
package main
import (
"context"
"net/http"
"os"
"time"
@ -22,10 +23,15 @@ func main() {
l := svr.Logger()
// Add probe handlers
svr.AddHealthzProbes(handler.New(l, "any"))
svr.AddStartupProbes(handler.New(l, "startup"))
svr.AddLivenessProbes(handler.New(l, "liveness"))
svr.AddReadinessProbes(handler.New(l, "readiness"))
svr.AddAlwaysHealthzers(handler.New(l, "always"))
svr.AddStartupHealthzers(handler.New(l, "startup"))
svr.AddLivenessHealthzers(handler.New(l, "liveness"))
svr.AddReadinessHealthzers(handler.New(l, "readiness"))
svr.AddAlwaysHealthzers(keel.NewHealthzerFn(func(ctx context.Context) error {
l.Info("healther fn")
return nil
}))
// create demo service
svs := http.NewServeMux()

View File

@ -2,6 +2,20 @@ package keel
import "context"
type healther struct {
handle func(context.Context) error
}
func NewHealthzerFn(handle func(context.Context) error) healther {
return healther{
handle: handle,
}
}
func (h healther) Healthz(ctx context.Context) error {
return h.handle(ctx)
}
// BoolHealthzer interface
type BoolHealthzer interface {
Healthz() bool

View File

@ -4,11 +4,9 @@ package keel
// https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
type HealthzType string
const healthzServiceName = "healthz"
const (
// HealthzTypeAny will run on any checks
HealthzTypeAny HealthzType = "any"
// HealthzTypeAlways will run on any checks
HealthzTypeAlways HealthzType = "always"
// HealthzTypeStartup will run on /healthz/startup checks
// > The kubelet uses startup probes to know when a container application has started. If such a probe is configured,
// > it disables liveness and readiness checks until it succeeds, making sure those probes don't interfere with the

View File

@ -142,10 +142,10 @@ func WithHTTPPrometheusService(enabled bool) Option {
func WithHTTPHealthzService(enabled bool) Option {
return func(inst *Server) {
if config.GetBool(inst.Config(), "service.probes.enabled", enabled)() {
if config.GetBool(inst.Config(), "service.healthz.enabled", enabled)() {
service := NewDefaultServiceHTTPProbes(inst.probes)
inst.initServices = append(inst.initServices, service)
inst.AddHealthzProbes(service)
inst.AddAlwaysHealthzers(service)
inst.AddCloser(service)
}
}

View File

@ -151,7 +151,7 @@ func NewServer(opts ...Option) *Server {
}
// add probe
inst.AddHealthzProbes(inst)
inst.AddAlwaysHealthzers(inst)
// start init services
inst.startService(inst.initServices...)
@ -192,7 +192,7 @@ func (s *Server) AddService(service Service) {
}
}
s.services = append(s.services, service)
s.AddHealthzProbes(service)
s.AddAlwaysHealthzers(service)
s.AddCloser(service)
}
@ -236,8 +236,8 @@ func (s *Server) AddClosers(closers ...interface{}) {
}
}
// AddProbe adds a probe to be called on healthz checks
func (s *Server) AddProbe(typ HealthzType, probe interface{}) {
// AddHealthzer adds a probe to be called on healthz checks
func (s *Server) AddHealthzer(typ HealthzType, probe interface{}) {
switch probe.(type) {
case BoolHealthzer,
BoolHealthzerWithContext,
@ -251,31 +251,31 @@ func (s *Server) AddProbe(typ HealthzType, probe interface{}) {
}
}
// AddProbes adds the given probes to be called on healthz checks
func (s *Server) AddProbes(typ HealthzType, probes ...interface{}) {
// AddHealthzers adds the given probes to be called on healthz checks
func (s *Server) AddHealthzers(typ HealthzType, probes ...interface{}) {
for _, probe := range probes {
s.AddProbe(typ, probe)
s.AddHealthzer(typ, probe)
}
}
// AddHealthzProbes adds the startup probes to be called on healthz checks
func (s *Server) AddHealthzProbes(probes ...interface{}) {
s.AddProbes(HealthzTypeAny, probes...)
// AddAlwaysHealthzers adds the probes to be called on any healthz checks
func (s *Server) AddAlwaysHealthzers(probes ...interface{}) {
s.AddHealthzers(HealthzTypeAlways, probes...)
}
// AddStartupProbes adds the startup probes to be called on healthz checks
func (s *Server) AddStartupProbes(probes ...interface{}) {
s.AddProbes(HealthzTypeStartup, probes...)
// AddStartupHealthzers adds the startup probes to be called on healthz checks
func (s *Server) AddStartupHealthzers(probes ...interface{}) {
s.AddHealthzers(HealthzTypeStartup, probes...)
}
// AddLivenessProbes adds the liveness probes to be called on healthz checks
func (s *Server) AddLivenessProbes(probes ...interface{}) {
s.AddProbes(HealthzTypeLiveness, probes...)
// AddLivenessHealthzers adds the liveness probes to be called on healthz checks
func (s *Server) AddLivenessHealthzers(probes ...interface{}) {
s.AddHealthzers(HealthzTypeLiveness, probes...)
}
// AddReadinessProbes adds the readiness probes to be called on healthz checks
func (s *Server) AddReadinessProbes(probes ...interface{}) {
s.AddProbes(HealthzTypeReadiness, probes...)
// AddReadinessHealthzers adds the readiness probes to be called on healthz checks
func (s *Server) AddReadinessHealthzers(probes ...interface{}) {
s.AddHealthzers(HealthzTypeReadiness, probes...)
}
// IsCanceled returns true if the internal errgroup has been canceled

View File

@ -11,9 +11,9 @@ import (
)
const (
DefaultServiceHTTPProbesName = healthzServiceName
DefaultServiceHTTPProbesAddr = ":9400"
DefaultServiceHTTPProbesPath = "/healthz"
DefaultServiceHTTPHealthzName = "healthz"
DefaultServiceHTTPHealthzAddr = ":9400"
DefaultServiceHTTPHealthzPath = "/healthz"
)
func NewServiceHTTPHealthz(l *zap.Logger, name, addr, path string, probes map[HealthzType][]interface{}) *ServiceHTTP {
@ -66,7 +66,7 @@ func NewServiceHTTPHealthz(l *zap.Logger, name, addr, path string, probes map[He
handler.HandleFunc(path+"/"+HealthzTypeLiveness.String(), func(w http.ResponseWriter, r *http.Request) {
var ps []interface{}
if p, ok := probes[HealthzTypeAny]; ok {
if p, ok := probes[HealthzTypeAlways]; ok {
ps = append(ps, p...)
}
if p, ok := probes[HealthzTypeLiveness]; ok {
@ -87,7 +87,7 @@ func NewServiceHTTPHealthz(l *zap.Logger, name, addr, path string, probes map[He
handler.HandleFunc(path+"/"+HealthzTypeReadiness.String(), func(w http.ResponseWriter, r *http.Request) {
var ps []interface{}
if p, ok := probes[HealthzTypeAny]; ok {
if p, ok := probes[HealthzTypeAlways]; ok {
ps = append(ps, p...)
}
if p, ok := probes[HealthzTypeReadiness]; ok {
@ -108,7 +108,7 @@ func NewServiceHTTPHealthz(l *zap.Logger, name, addr, path string, probes map[He
handler.HandleFunc(path+"/"+HealthzTypeStartup.String(), func(w http.ResponseWriter, r *http.Request) {
var ps []interface{}
if p, ok := probes[HealthzTypeAny]; ok {
if p, ok := probes[HealthzTypeAlways]; ok {
ps = append(ps, p...)
}
if p, ok := probes[HealthzTypeStartup]; ok {
@ -132,9 +132,9 @@ func NewServiceHTTPHealthz(l *zap.Logger, name, addr, path string, probes map[He
func NewDefaultServiceHTTPProbes(probes map[HealthzType][]interface{}) *ServiceHTTP {
return NewServiceHTTPHealthz(
log.Logger(),
DefaultServiceHTTPProbesName,
DefaultServiceHTTPProbesAddr,
DefaultServiceHTTPProbesPath,
DefaultServiceHTTPHealthzName,
DefaultServiceHTTPHealthzAddr,
DefaultServiceHTTPHealthzPath,
probes,
)
}