mirror of
https://github.com/tailscale/tailscale.git
synced 2025-02-21 12:28:39 +00:00

cmd/{containerboot,k8s-operator},kube: add preshutdown hook for egress PG proxies This change is part of work towards minimizing downtime during update rollouts of egress ProxyGroup replicas. This change: - updates the containerboot health check logic to return Pod IP in headers, if set - always runs the health check for egress PG proxies - updates ClusterIP Services created for PG egress endpoints to include the health check endpoint - implements preshutdown endpoint in proxies. The preshutdown endpoint logic waits till, for all currently configured egress services, the ClusterIP Service health check endpoint is no longer returned by the shutting-down Pod (by looking at the new Pod IP header). - ensures that kubelet is configured to call the preshutdown endpoint This reduces the possibility that, as replicas are terminated during an update, a replica gets terminated to which cluster traffic is still being routed via the ClusterIP Service because kube proxy has not yet updated routig rules. This is not a perfect check as in practice, it only checks that the kube proxy on the node on which the proxy runs has updated rules. However, overall this might be good enough. The preshutdown logic is disabled if users have configured a custom health check port via TS_LOCAL_ADDR_PORT env var. This change throws a warnign if so and in future setting of that env var for operator proxies might be disallowed (as users shouldn't need to configure this for a Pod directly). This is backwards compatible with earlier proxy versions. Updates tailscale/tailscale#14326 Signed-off-by: Irbe Krumina <irbe@tailscale.com>
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
//go:build linux
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"tailscale.com/kube/kubetypes"
|
|
)
|
|
|
|
// healthz is a simple health check server, if enabled it returns 200 OK if
|
|
// this tailscale node currently has at least one tailnet IP address else
|
|
// returns 503.
|
|
type healthz struct {
|
|
sync.Mutex
|
|
hasAddrs bool
|
|
podIPv4 string
|
|
}
|
|
|
|
func (h *healthz) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
h.Lock()
|
|
defer h.Unlock()
|
|
|
|
if h.hasAddrs {
|
|
w.Header().Add(kubetypes.PodIPv4Header, h.podIPv4)
|
|
if _, err := w.Write([]byte("ok")); err != nil {
|
|
http.Error(w, fmt.Sprintf("error writing status: %v", err), http.StatusInternalServerError)
|
|
}
|
|
} else {
|
|
http.Error(w, "node currently has no tailscale IPs", http.StatusServiceUnavailable)
|
|
}
|
|
}
|
|
|
|
func (h *healthz) update(healthy bool) {
|
|
h.Lock()
|
|
defer h.Unlock()
|
|
|
|
if h.hasAddrs != healthy {
|
|
log.Println("Setting healthy", healthy)
|
|
}
|
|
h.hasAddrs = healthy
|
|
}
|
|
|
|
// healthHandlers registers a simple health handler at /healthz.
|
|
// A containerized tailscale instance is considered healthy if
|
|
// it has at least one tailnet IP address.
|
|
func healthHandlers(mux *http.ServeMux, podIPv4 string) *healthz {
|
|
h := &healthz{podIPv4: podIPv4}
|
|
mux.Handle("GET /healthz", h)
|
|
return h
|
|
}
|