Skip to content

Commit 5c12069

Browse files
Differentiate liveness and readiness probes for router
Add a backend to the router controller "/livez" that always returns true. This differentiates the liveness and readiness probes so that a router can be alive and not ready. Bug 1550007
1 parent fdfb6d9 commit 5c12069

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed

pkg/cmd/infra/router/template.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ func (o *TemplateRouterOptions) Validate() error {
243243
// Run launches a template router using the provided options. It never exits.
244244
func (o *TemplateRouterOptions) Run() error {
245245
glog.Infof("Starting template router (%s)", version.Get())
246+
var ptrTemplatePlugin *templateplugin.TemplatePlugin
246247

247248
var reloadCallbacks []func()
248249

@@ -310,9 +311,15 @@ func (o *TemplateRouterOptions) Run() error {
310311
if err != nil {
311312
return fmt.Errorf("ROUTER_METRICS_READY_HTTP_URL must be a valid URL or empty: %v", err)
312313
}
313-
check := metrics.HTTPBackendAvailable(u)
314+
checkBackend := metrics.HTTPBackendAvailable(u)
314315
if isTrue(util.Env("ROUTER_USE_PROXY_PROTOCOL", "")) {
315-
check = metrics.ProxyProtocolHTTPBackendAvailable(u)
316+
checkBackend = metrics.ProxyProtocolHTTPBackendAvailable(u)
317+
}
318+
checkSync := metrics.HasSynced(&ptrTemplatePlugin)
319+
checkController := metrics.ControllerLive()
320+
liveChecks := []healthz.HealthzChecker{checkController}
321+
if !(isTrue(util.Env("ROUTER_BIND_PORTS_BEFORE_SYNC", ""))) {
322+
liveChecks = append(liveChecks, checkBackend)
316323
}
317324

318325
kubeconfig := o.Config.KubeConfig()
@@ -353,7 +360,8 @@ func (o *TemplateRouterOptions) Run() error {
353360
Resource: "routers",
354361
Name: o.RouterName,
355362
},
356-
Checks: []healthz.HealthzChecker{check},
363+
LiveChecks: liveChecks,
364+
ReadyChecks: []healthz.HealthzChecker{checkBackend, checkSync},
357365
}
358366
if certFile := util.Env("ROUTER_METRICS_TLS_CERT_FILE", ""); len(certFile) > 0 {
359367
certificate, err := tls.LoadX509KeyPair(certFile, util.Env("ROUTER_METRICS_TLS_KEY_FILE", ""))
@@ -411,6 +419,7 @@ func (o *TemplateRouterOptions) Run() error {
411419
if err != nil {
412420
return err
413421
}
422+
ptrTemplatePlugin = templatePlugin
414423

415424
factory := o.RouterSelection.NewFactory(routeclient, projectclient.Project().Projects(), kc)
416425
factory.RouteModifierFn = o.RouteUpdate

pkg/oc/admin/router/router.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,8 @@ const (
243243
// Default port numbers to expose and bind/listen on.
244244
defaultPorts = "80:80,443:443"
245245

246-
// Default stats and healthz port.
247-
defaultStatsPort = 1936
248-
defaultHealthzPort = defaultStatsPort
246+
// Default stats port.
247+
defaultStatsPort = 1936
249248
)
250249

251250
// NewCmdRouter implements the OpenShift CLI router command.
@@ -436,21 +435,21 @@ func generateSecretsConfig(cfg *RouterConfig, namespace string, defaultCert []by
436435
return secrets, volumes, mounts, nil
437436
}
438437

439-
func generateProbeConfigForRouter(cfg *RouterConfig, ports []kapi.ContainerPort) *kapi.Probe {
438+
func generateProbeConfigForRouter(path string, cfg *RouterConfig, ports []kapi.ContainerPort) *kapi.Probe {
440439
var probe *kapi.Probe
441440

442441
if cfg.Type == "haproxy-router" {
443442
probe = &kapi.Probe{}
444-
healthzPort := defaultHealthzPort
443+
probePort := defaultStatsPort
445444
if cfg.StatsPort > 0 {
446-
healthzPort = cfg.StatsPort
445+
probePort = cfg.StatsPort
447446
}
448447

449448
probe.Handler.HTTPGet = &kapi.HTTPGetAction{
450-
Path: "/healthz",
449+
Path: path,
451450
Port: intstr.IntOrString{
452451
Type: intstr.Int,
453-
IntVal: int32(healthzPort),
452+
IntVal: int32(probePort),
454453
},
455454
}
456455

@@ -466,15 +465,15 @@ func generateProbeConfigForRouter(cfg *RouterConfig, ports []kapi.ContainerPort)
466465
}
467466

468467
func generateLivenessProbeConfig(cfg *RouterConfig, ports []kapi.ContainerPort) *kapi.Probe {
469-
probe := generateProbeConfigForRouter(cfg, ports)
468+
probe := generateProbeConfigForRouter("/healthz", cfg, ports)
470469
if probe != nil {
471470
probe.InitialDelaySeconds = 10
472471
}
473472
return probe
474473
}
475474

476475
func generateReadinessProbeConfig(cfg *RouterConfig, ports []kapi.ContainerPort) *kapi.Probe {
477-
probe := generateProbeConfigForRouter(cfg, ports)
476+
probe := generateProbeConfigForRouter("healthz/ready", cfg, ports)
478477
if probe != nil {
479478
probe.InitialDelaySeconds = 10
480479
}

pkg/router/metrics/health.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/golang/glog"
1414

15+
templateplugin "github.com/openshift/origin/pkg/router/template"
1516
"k8s.io/apiserver/pkg/server/healthz"
1617
"k8s.io/kubernetes/pkg/probe"
1718
probehttp "k8s.io/kubernetes/pkg/probe/http"
@@ -35,6 +36,28 @@ func HTTPBackendAvailable(u *url.URL) healthz.HealthzChecker {
3536
})
3637
}
3738

39+
// HasSynced returns a healthz check that verifies the router has been synced at least
40+
// once.
41+
func HasSynced(router **templateplugin.TemplatePlugin) healthz.HealthzChecker {
42+
return healthz.NamedCheck("has-synced", func(r *http.Request) error {
43+
if router != nil {
44+
if (*router).Router.SyncedAtLeastOnce() == true {
45+
return nil
46+
} else {
47+
return fmt.Errorf("Router not synced")
48+
}
49+
}
50+
return nil
51+
})
52+
}
53+
54+
func ControllerLive() healthz.HealthzChecker {
55+
return healthz.NamedCheck("controller", func(r *http.Request) error {
56+
return nil
57+
})
58+
59+
}
60+
3861
// ProxyProtocolHTTPBackendAvailable returns a healthz check that verifies a backend supporting
3962
// the HAProxy PROXY protocol responds to a GET to the provided URL with 2xx or 3xx response.
4063
func ProxyProtocolHTTPBackendAvailable(u *url.URL) healthz.HealthzChecker {

pkg/router/metrics/metrics.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ type Listener struct {
3131
Authorizer authorizer.Authorizer
3232
Record authorizer.AttributesRecord
3333

34-
Checks []healthz.HealthzChecker
34+
LiveChecks []healthz.HealthzChecker
35+
ReadyChecks []healthz.HealthzChecker
3536
}
3637

3738
func (l Listener) handler() http.Handler {
3839
mux := http.NewServeMux()
39-
healthz.InstallHandler(mux, l.Checks...)
40+
healthz.InstallHandler(mux, l.LiveChecks...)
41+
healthz.InstallPathHandler(mux, "/healthz/ready", l.ReadyChecks...)
4042

4143
if l.Authenticator != nil {
4244
protected := http.NewServeMux()

0 commit comments

Comments
 (0)