Add healthz check to ensure logging is not blocked
Kubernetes-commit: b7b4b84afe4405cde976ceeeccb62acecac1c4f0
This commit is contained in:
parent
52b58255fb
commit
6c34ac4aa5
|
@ -255,7 +255,7 @@ func NewConfig(codecs serializer.CodecFactory) *Config {
|
|||
HandlerChainWaitGroup: new(utilwaitgroup.SafeWaitGroup),
|
||||
LegacyAPIGroupPrefixes: sets.NewString(DefaultLegacyAPIPrefix),
|
||||
DisabledPostStartHooks: sets.NewString(),
|
||||
HealthzChecks: []healthz.HealthzChecker{healthz.PingHealthz},
|
||||
HealthzChecks: []healthz.HealthzChecker{healthz.PingHealthz, healthz.LogHealthz},
|
||||
EnableIndex: true,
|
||||
EnableDiscovery: true,
|
||||
EnableProfiling: true,
|
||||
|
|
|
@ -101,6 +101,7 @@ func TestNewWithDelegate(t *testing.T) {
|
|||
"/foo",
|
||||
"/healthz",
|
||||
"/healthz/delegate-health",
|
||||
"/healthz/log",
|
||||
"/healthz/ping",
|
||||
"/healthz/poststarthook/delegate-post-start-hook",
|
||||
"/healthz/poststarthook/generic-apiserver-start-informers",
|
||||
|
@ -111,6 +112,7 @@ func TestNewWithDelegate(t *testing.T) {
|
|||
]
|
||||
}`, t)
|
||||
checkPath(server.URL+"/healthz", http.StatusInternalServerError, `[+]ping ok
|
||||
[+]log ok
|
||||
[-]wrapping-health failed: reason withheld
|
||||
[-]delegate-health failed: reason withheld
|
||||
[+]poststarthook/generic-apiserver-start-informers ok
|
||||
|
|
|
@ -22,8 +22,12 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
)
|
||||
|
||||
// HealthzChecker is a named healthz checker.
|
||||
|
@ -56,6 +60,34 @@ func (ping) Check(_ *http.Request) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// LogHealthz returns true if logging is not blocked
|
||||
var LogHealthz HealthzChecker = &log{}
|
||||
|
||||
type log struct {
|
||||
startOnce sync.Once
|
||||
lastVerified atomic.Value
|
||||
}
|
||||
|
||||
func (l *log) Name() string {
|
||||
return "log"
|
||||
}
|
||||
|
||||
func (l *log) Check(_ *http.Request) error {
|
||||
l.startOnce.Do(func() {
|
||||
l.lastVerified.Store(time.Now())
|
||||
go wait.Forever(func() {
|
||||
glog.Flush()
|
||||
l.lastVerified.Store(time.Now())
|
||||
}, time.Minute)
|
||||
})
|
||||
|
||||
lastVerified := l.lastVerified.Load().(time.Time)
|
||||
if time.Since(lastVerified) < (2 * time.Minute) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("logging blocked")
|
||||
}
|
||||
|
||||
// NamedCheck returns a healthz checker for the given name and function.
|
||||
func NamedCheck(name string, check func(r *http.Request) error) HealthzChecker {
|
||||
return &healthzCheck{name, check}
|
||||
|
|
Loading…
Reference in New Issue