From 03ffa381397e3a71839dae387a6ccb6be90740f1 Mon Sep 17 00:00:00 2001 From: Kevin Franklin Kim Date: Mon, 14 Mar 2022 14:09:28 +0100 Subject: [PATCH] refactor: rename probe to healthzer --- closer.go | 14 +++++++++++++ example/healthz/main.go | 14 +++++++++---- healthzer.go | 14 +++++++++++++ probetype.go => healthztype.go | 6 ++---- option.go | 4 ++-- server.go | 38 +++++++++++++++++----------------- servicehttphealthz.go | 18 ++++++++-------- 7 files changed, 70 insertions(+), 38 deletions(-) rename probetype.go => healthztype.go (93%) diff --git a/closer.go b/closer.go index e7bab9d..9de714d 100644 --- a/closer.go +++ b/closer.go @@ -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() diff --git a/example/healthz/main.go b/example/healthz/main.go index 240f0fc..08de676 100644 --- a/example/healthz/main.go +++ b/example/healthz/main.go @@ -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() diff --git a/healthzer.go b/healthzer.go index 75e471e..3156353 100644 --- a/healthzer.go +++ b/healthzer.go @@ -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 diff --git a/probetype.go b/healthztype.go similarity index 93% rename from probetype.go rename to healthztype.go index 400a0b8..848edc0 100644 --- a/probetype.go +++ b/healthztype.go @@ -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 diff --git a/option.go b/option.go index 4126375..fd81998 100644 --- a/option.go +++ b/option.go @@ -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) } } diff --git a/server.go b/server.go index 6da472d..0cb848d 100644 --- a/server.go +++ b/server.go @@ -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 diff --git a/servicehttphealthz.go b/servicehttphealthz.go index 6de167a..6d28722 100644 --- a/servicehttphealthz.go +++ b/servicehttphealthz.go @@ -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, ) }