Skip to content

Commit 13d42ec

Browse files
Merge pull request #19009 from JacobTanenbaum/BZ1550007
Differentiate liveness and readiness probes for router pods
2 parents f377a40 + 978d2bc commit 13d42ec

File tree

6 files changed

+91
-17
lines changed

6 files changed

+91
-17
lines changed

pkg/cmd/infra/router/template.go

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

246247
var reloadCallbacks []func()
247248

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

317324
kubeconfig, _, err := o.Config.KubeConfig()
@@ -355,7 +362,8 @@ func (o *TemplateRouterOptions) Run() error {
355362
Resource: "routers",
356363
Name: o.RouterName,
357364
},
358-
Checks: []healthz.HealthzChecker{check},
365+
LiveChecks: liveChecks,
366+
ReadyChecks: []healthz.HealthzChecker{checkBackend, checkSync},
359367
}
360368
if certFile := util.Env("ROUTER_METRICS_TLS_CERT_FILE", ""); len(certFile) > 0 {
361369
certificate, err := tls.LoadX509KeyPair(certFile, util.Env("ROUTER_METRICS_TLS_KEY_FILE", ""))
@@ -417,6 +425,7 @@ func (o *TemplateRouterOptions) Run() error {
417425
if err != nil {
418426
return err
419427
}
428+
ptrTemplatePlugin = templatePlugin
420429

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

pkg/oc/admin/router/router.go

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

240-
// Default stats and healthz port.
241-
defaultStatsPort = 1936
242-
defaultHealthzPort = defaultStatsPort
240+
// Default stats port.
241+
defaultStatsPort = 1936
243242
)
244243

245244
// NewCmdRouter implements the OpenShift CLI router command.
@@ -428,21 +427,21 @@ func generateSecretsConfig(cfg *RouterConfig, namespace string, defaultCert []by
428427
return secrets, volumes, mounts, nil
429428
}
430429

431-
func generateProbeConfigForRouter(cfg *RouterConfig, ports []kapi.ContainerPort) *kapi.Probe {
430+
func generateProbeConfigForRouter(path string, cfg *RouterConfig, ports []kapi.ContainerPort) *kapi.Probe {
432431
var probe *kapi.Probe
433432

434433
if cfg.Type == "haproxy-router" {
435434
probe = &kapi.Probe{}
436-
healthzPort := defaultHealthzPort
435+
probePort := defaultStatsPort
437436
if cfg.StatsPort > 0 {
438-
healthzPort = cfg.StatsPort
437+
probePort = cfg.StatsPort
439438
}
440439

441440
probe.Handler.HTTPGet = &kapi.HTTPGetAction{
442-
Path: "/healthz",
441+
Path: path,
443442
Port: intstr.IntOrString{
444443
Type: intstr.Int,
445-
IntVal: int32(healthzPort),
444+
IntVal: int32(probePort),
446445
},
447446
}
448447

@@ -458,15 +457,15 @@ func generateProbeConfigForRouter(cfg *RouterConfig, ports []kapi.ContainerPort)
458457
}
459458

460459
func generateLivenessProbeConfig(cfg *RouterConfig, ports []kapi.ContainerPort) *kapi.Probe {
461-
probe := generateProbeConfigForRouter(cfg, ports)
460+
probe := generateProbeConfigForRouter("/healthz", cfg, ports)
462461
if probe != nil {
463462
probe.InitialDelaySeconds = 10
464463
}
465464
return probe
466465
}
467466

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

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()

vendor/k8s.io/kubernetes/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go

Lines changed: 11 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/k8s.io/kubernetes/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)