From 7518eba82e3e2b9641ef2cf56d76fb86322c5e12 Mon Sep 17 00:00:00 2001 From: Chris Wiggins Date: Thu, 12 Dec 2024 09:37:56 +1300 Subject: [PATCH] feat: allow setting node registration expiration via config --- CHANGELOG.md | 1 + hscontrol/app.go | 8 +++----- hscontrol/oidc.go | 5 +++-- hscontrol/types/config.go | 8 ++++++++ 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73225cca84..4cf5faf573 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -95,6 +95,7 @@ This will also affect the way you [reference users in policies](https://github.c - Fixed missing `stable-debug` container tag [#2232](https://github.com/juanfont/headscale/pr/2232) - Loosened up `server_url` and `base_domain` check. It was overly strict in some cases. [#2248](https://github.com/juanfont/headscale/pull/2248) - CLI for managing users now accepts `--identifier` in addition to `--name`, usage of `--identifier` is recommended [#2261](https://github.com/juanfont/headscale/pull/2261) +- Added option to set Node registration expiration/cleanup options via config [#2280](https://github.com/juanfont/headscale/pull/2280) ## 0.23.0 (2024-09-18) diff --git a/hscontrol/app.go b/hscontrol/app.go index 1651b8f211..2a712031f8 100644 --- a/hscontrol/app.go +++ b/hscontrol/app.go @@ -72,9 +72,6 @@ const ( updateInterval = 5 * time.Second privateKeyFileMode = 0o600 headscaleDirPerm = 0o700 - - registerCacheExpiration = time.Minute * 15 - registerCacheCleanup = time.Minute * 20 ) // Headscale represents the base app of the service. @@ -122,8 +119,8 @@ func NewHeadscale(cfg *types.Config) (*Headscale, error) { } registrationCache := zcache.New[string, types.Node]( - registerCacheExpiration, - registerCacheCleanup, + cfg.Tuning.NodeRegistrationCacheExpiration, + cfg.Tuning.NodeRegistrationCacheCleanup, ) app := Headscale{ @@ -171,6 +168,7 @@ func NewHeadscale(cfg *types.Config) (*Headscale, error) { app.nodeNotifier, app.ipAlloc, app.polMan, + &cfg.Tuning, ) if err != nil { if cfg.OIDC.OnlyStartIfOIDCIsAvailable { diff --git a/hscontrol/oidc.go b/hscontrol/oidc.go index 1db1ec079f..2c7b916aea 100644 --- a/hscontrol/oidc.go +++ b/hscontrol/oidc.go @@ -68,6 +68,7 @@ func NewAuthProviderOIDC( notif *notifier.Notifier, ipAlloc *db.IPAllocator, polMan policy.PolicyManager, + tuningCfg *types.Tuning, ) (*AuthProviderOIDC, error) { var err error // grab oidc config if it hasn't been already @@ -88,8 +89,8 @@ func NewAuthProviderOIDC( } registrationCache := zcache.New[string, key.MachinePublic]( - registerCacheExpiration, - registerCacheCleanup, + tuningCfg.NodeRegistrationCacheExpiration, + tuningCfg.NodeRegistrationCacheCleanup, ) return &AuthProviderOIDC{ diff --git a/hscontrol/types/config.go b/hscontrol/types/config.go index 3dc822ba6d..537ea72286 100644 --- a/hscontrol/types/config.go +++ b/hscontrol/types/config.go @@ -212,6 +212,10 @@ type Tuning struct { NotifierSendTimeout time.Duration BatchChangeDelay time.Duration NodeMapSessionBufferedChanSize int + + // Node registration cache expiration + NodeRegistrationCacheExpiration time.Duration + NodeRegistrationCacheCleanup time.Duration } // LoadConfig prepares and loads the Headscale configuration into Viper. @@ -291,6 +295,8 @@ func LoadConfig(path string, isFile bool) error { viper.SetDefault("tuning.notifier_send_timeout", "800ms") viper.SetDefault("tuning.batch_change_delay", "800ms") viper.SetDefault("tuning.node_mapsession_buffered_chan_size", 30) + viper.SetDefault("tuning.node_registration_cache_expiration", "15m") + viper.SetDefault("tuning.node_registration_cache_cleanup", "20m") viper.SetDefault("prefixes.allocation", string(IPAllocationStrategySequential)) @@ -935,6 +941,8 @@ func LoadServerConfig() (*Config, error) { NodeMapSessionBufferedChanSize: viper.GetInt( "tuning.node_mapsession_buffered_chan_size", ), + NodeRegistrationCacheExpiration: viper.GetDuration("tuning.node_registration_cache_expiration"), + NodeRegistrationCacheCleanup: viper.GetDuration("tuning.node_registration_cache_cleanup"), }, }, nil }