Files
tailscale/kube/health/probe.go
chaosinthecrd b5fa6773ce cmd/containerboot,kube/{certs,health,localclient}: serve /livez and /readyz
Proxies only serve /healthz, which reports whether the node has a tailnet IP,
so it can't back a liveness or readiness probe. When TS_ENABLE_HEALTH_CHECK is
set, containerboot now also serves /livez, which fails only when tailscaled
stops answering LocalAPI, and /readyz, which passes once the node has tailnet
IPs and, with TS_SERVE_CONFIG set, has applied the serve config and has a cert
for each HTTPS-only Tailscale Service it advertises. Certs are checked through
the read-only cert-status endpoint so the probe never triggers issuance. An
expired or unreadable cert counts as present, as failing readiness on it would
stop a StatefulSet rollout before it reaches the replica that renews certs.

Updates tailscale/corp#38175

Signed-off-by: chaosinthecrd <[email protected]>
2026-11-19 13:17:44 +00:00

113 lines
2.5 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
//go:build !plan9
package health
import (
"context"
"errors"
"fmt"
"net/http"
"slices"
"sync"
"tailscale.com/types/logger"
)
// Probe is a Kubernetes probe server. It serves 200 only once every check
// registered with it passes, otherwise 503 with the reason each failing check
// gave.
//
// A nil Probe is valid and ignores registrations, so callers don't need to
// check whether health checks are enabled.
type Probe struct {
name string
logf logger.Logf
mu sync.Mutex // protects following
checks []check
}
type check struct {
name string
fn func(context.Context) error
}
func (p *Probe) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p.mu.Lock()
checks := slices.Clone(p.checks)
p.mu.Unlock()
var errs []error
for _, c := range checks {
if err := c.fn(r.Context()); err != nil {
errs = append(errs, fmt.Errorf("%s: %w", c.name, err))
}
}
if err := errors.Join(errs...); err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
if _, err := w.Write([]byte("ok")); err != nil {
http.Error(w, fmt.Sprintf("error writing status: %v", err), http.StatusInternalServerError)
}
}
// AddCheck registers a check that is run on every request. fn returns nil if
// the check passes, or the reason it doesn't. fn must be cheap and safe for
// concurrent use.
func (p *Probe) AddCheck(name string, fn func(context.Context) error) {
if p == nil {
return
}
p.mu.Lock()
defer p.mu.Unlock()
p.checks = append(p.checks, check{name: name, fn: fn})
}
// AddCondition registers a check whose state is set by its owner with
// [Condition.Set]. It starts out failing, reporting reason.
func (p *Probe) AddCondition(name, reason string) *Condition {
if p == nil {
return nil
}
c := &Condition{name: p.name + ": " + name, reason: reason, logf: p.logf}
p.AddCheck(name, c.check)
return c
}
// Condition is a check that its owner sets as its state changes.
// A nil Condition is valid and ignores Set.
type Condition struct {
name string
reason string
logf logger.Logf
mu sync.Mutex // protects following
pass bool
}
// Set records whether the condition holds.
func (c *Condition) Set(pass bool) {
if c == nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
if c.pass != pass {
c.logf("%s passing: %v", c.name, pass)
}
c.pass = pass
}
func (c *Condition) check(context.Context) error {
c.mu.Lock()
defer c.mu.Unlock()
if !c.pass {
return errors.New(c.reason)
}
return nil
}