Initial revisin of healthchecker, inspired by HAProxy

This commit is contained in:
2026-04-10 17:30:44 +02:00
commit b84b3274b1
24 changed files with 4400 additions and 0 deletions

40
internal/prober/netns.go Normal file
View File

@@ -0,0 +1,40 @@
package prober
import (
"fmt"
"runtime"
"github.com/vishvananda/netns"
)
// inNetns runs fn while the current OS thread is switched into the named
// network namespace. The thread is locked for the duration so the switch is safe.
// The original netns is restored before returning.
// If nsName is empty, fn is run in the current namespace without any switching.
func inNetns(nsName string, fn func() error) error {
if nsName == "" {
return fn()
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
origNs, err := netns.Get()
if err != nil {
return fmt.Errorf("get current netns: %w", err)
}
defer origNs.Close()
defer netns.Set(origNs) //nolint:errcheck
targetNs, err := netns.GetFromName(nsName)
if err != nil {
return fmt.Errorf("get netns %q: %w", nsName, err)
}
defer targetNs.Close()
if err := netns.Set(targetNs); err != nil {
return fmt.Errorf("enter netns %q: %w", nsName, err)
}
return fn()
}