mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 22:10:05 +00:00
TUN-8737: update metrics server port selection
## Summary Update how metrics server binds to a listener by using a known set of ports whenever the default address is used with the fallback to a random port in case all address are already in use. The default address changes at compile time in order to bind to a different default address when the final deliverable is a docker image. Refactor ReadyServer tests. Closes TUN-8737
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/facebookgo/grace/gracenet"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/rs/zerolog"
|
||||
@@ -21,6 +22,34 @@ const (
|
||||
defaultShutdownTimeout = time.Second * 15
|
||||
)
|
||||
|
||||
// This variable is set at compile time to allow the default local address to change.
|
||||
var Runtime = "host"
|
||||
|
||||
func GetMetricsDefaultAddress(runtimeType string) string {
|
||||
// When issuing the diagnostic command we may have to reach a server that is
|
||||
// running in a virtual enviroment and in that case we must bind to 0.0.0.0
|
||||
// otherwise the server won't be reachable.
|
||||
switch runtimeType {
|
||||
case "virtual":
|
||||
return "0.0.0.0:0"
|
||||
default:
|
||||
return "localhost:0"
|
||||
}
|
||||
}
|
||||
|
||||
// GetMetricsKnownAddresses returns the addresses used by the metrics server to bind at
|
||||
// startup time to allow a semi-deterministic approach to know where the server is listening at.
|
||||
// The ports were selected because at the time we are in 2024 and they do not collide with any
|
||||
// know/registered port according https://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers.
|
||||
func GetMetricsKnownAddresses(runtimeType string) [5]string {
|
||||
switch Runtime {
|
||||
case "virtual":
|
||||
return [5]string{"0.0.0.0:20241", "0.0.0.0:20242", "0.0.0.0:20243", "0.0.0.0:20244", "0.0.0.0:20245"}
|
||||
default:
|
||||
return [5]string{"localhost:20241", "localhost:20242", "localhost:20243", "localhost:20244", "localhost:20245"}
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
ReadyServer *ReadyServer
|
||||
QuickTunnelHostname string
|
||||
@@ -65,6 +94,42 @@ func newMetricsHandler(
|
||||
return router
|
||||
}
|
||||
|
||||
// CreateMetricsListener will create a new [net.Listener] by using an
|
||||
// known set of ports when the default address is passed with the fallback
|
||||
// of choosing a random port when none is available.
|
||||
//
|
||||
// In case the provided address is not the default one then it will be used
|
||||
// as is.
|
||||
func CreateMetricsListener(listeners *gracenet.Net, laddr string) (net.Listener, error) {
|
||||
if laddr == GetMetricsDefaultAddress(Runtime) {
|
||||
// On the presence of the default address select
|
||||
// a port from the known set of addresses iteratively.
|
||||
addresses := GetMetricsKnownAddresses(Runtime)
|
||||
for _, address := range addresses {
|
||||
listener, err := listeners.Listen("tcp", address)
|
||||
if err == nil {
|
||||
return listener, nil
|
||||
}
|
||||
}
|
||||
|
||||
// When no port is available then bind to a random one
|
||||
listener, err := listeners.Listen("tcp", laddr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to listen to default metrics address: %w", err)
|
||||
}
|
||||
|
||||
return listener, nil
|
||||
}
|
||||
|
||||
// Explicitly got a local address then bind to it
|
||||
listener, err := listeners.Listen("tcp", laddr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to bind to address (%s): %w", laddr, err)
|
||||
}
|
||||
|
||||
return listener, nil
|
||||
}
|
||||
|
||||
func ServeMetrics(
|
||||
l net.Listener,
|
||||
ctx context.Context,
|
||||
|
Reference in New Issue
Block a user