From 78a435f7cd9c7c6396fb3d06509cbeb430d1480c Mon Sep 17 00:00:00 2001 From: racicLuka Date: Fri, 14 Aug 2026 15:01:25 +0200 Subject: [PATCH] feat: add cache TTL configurations for kubelet stats and Linux agent CMK-36615 --- .../templates/metrics-cache/deployment.yaml | 2 ++ charts/cmk-rustik/values.yaml | 8 +++++ metrics-cache/src/cli_args.rs | 35 ++++++++++++++++--- metrics-cache/src/state.rs | 2 ++ 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/charts/cmk-rustik/templates/metrics-cache/deployment.yaml b/charts/cmk-rustik/templates/metrics-cache/deployment.yaml index 30d7970..472dc4a 100644 --- a/charts/cmk-rustik/templates/metrics-cache/deployment.yaml +++ b/charts/cmk-rustik/templates/metrics-cache/deployment.yaml @@ -53,6 +53,8 @@ spec: - --writer-allowlist={{ .Release.Namespace }}:{{ include "rustik.fullname" . }}-metrics-fetcher - --cluster-name={{ required "clusterName is required (there is no safe default: it names every generated host)" .Values.clusterName }} - --cluster-host-name={{ required "clusterHostName is required (it must exactly match the Checkmk host representing this cluster)" .Values.clusterHostName }} + - --kubelet-stats-cache-ttl={{ .Values.metricsCache.kubeletStatsCacheTtl }} + - --linux-agent-cache-ttl={{ .Values.metricsCache.linuxAgentCacheTtl }} {{- if .Values.hostLabels.importAllAnnotations }} {{- if .Values.hostLabels.importKeyPattern }} {{ fail "hostLabels.importKeyPattern should not be set while hostLabels.importAllAnnotations is true" }} diff --git a/charts/cmk-rustik/values.yaml b/charts/cmk-rustik/values.yaml index 3f9a093..e3fbb3d 100644 --- a/charts/cmk-rustik/values.yaml +++ b/charts/cmk-rustik/values.yaml @@ -95,6 +95,14 @@ metricsCache: # Inherits top-level logLevel by default. logLevel: "" + # kubeletStatsCacheTtl is how long (seconds) kubelet stats entries are + # persisted in the cache before expiring. + kubeletStatsCacheTtl: 120 + + # linuxAgentCacheTtl is how long (seconds) Linux agent entries are + # persisted in the cache before expiring. + linuxAgentCacheTtl: 120 + # extraVolumes is volume definitions to add to the Pod in the Deployment. extraVolumes: [] # extraVolumeMounts is volume mounts to mount in the metrics-cache container. diff --git a/metrics-cache/src/cli_args.rs b/metrics-cache/src/cli_args.rs index 800b99d..033624a 100644 --- a/metrics-cache/src/cli_args.rs +++ b/metrics-cache/src/cli_args.rs @@ -78,14 +78,21 @@ pub struct CliArgs { )] pub writer_allowlist: Vec, - /// How long (seconds) entries are persisted in the cache + /// How long (seconds) kubelet stats entries are persisted in the cache #[arg( - short = 't', - long = "cache-ttl", + long = "kubelet-stats-cache-ttl", value_parser = parse_duration_secs, default_value = "120" )] - pub cache_ttl: Duration, + pub kubelet_stats_cache_ttl: Duration, + + /// How long (seconds) Linux agent entries are persisted in the cache + #[arg( + long = "linux-agent-cache-ttl", + value_parser = parse_duration_secs, + default_value = "120" + )] + pub linux_agent_cache_ttl: Duration, /// How verbose to log #[arg( @@ -398,6 +405,26 @@ mod tests { } } + #[test] + fn cache_ttls_have_expected_defaults() { + let args = parse(&[]).expect("minimal args should parse"); + assert_eq!(args.kubelet_stats_cache_ttl, Duration::from_secs(120)); + assert_eq!(args.linux_agent_cache_ttl, Duration::from_secs(120)); + } + + #[test] + fn cache_ttls_parse_from_flags() { + let args = parse(&[ + "--kubelet-stats-cache-ttl", + "30", + "--linux-agent-cache-ttl", + "60", + ]) + .expect("cache TTL flags should parse"); + assert_eq!(args.kubelet_stats_cache_ttl, Duration::from_secs(30)); + assert_eq!(args.linux_agent_cache_ttl, Duration::from_secs(60)); + } + #[test] fn push_interval_parse_correctly() { for interval in ["1", "80"] { diff --git a/metrics-cache/src/state.rs b/metrics-cache/src/state.rs index ee2aa4b..3df8034 100644 --- a/metrics-cache/src/state.rs +++ b/metrics-cache/src/state.rs @@ -47,9 +47,11 @@ impl AppState { reader_allowlist: args.reader_allowlist.clone(), writer_allowlist: args.writer_allowlist.clone(), kubelet_stats_summary_cache: Cache::builder() + .time_to_live(args.kubelet_stats_cache_ttl) .max_capacity(MAX_SUPPORTED_KUBERNETES_NODES) .build(), system_agent_cache: Cache::builder() + .time_to_live(args.linux_agent_cache_ttl) .max_capacity(MAX_SUPPORTED_KUBERNETES_NODES) .build(), host_settings: host_settings.into(),