fix(base-cluster/monitoring): only watch and create metrics for latest apiVersion#2037
fix(base-cluster/monitoring): only watch and create metrics for latest apiVersion#2037
Conversation
📝 WalkthroughWalkthroughThe Helm template for kube-state-metrics configuration was refactored to dynamically resolve Flux custom-resource configurations by iterating through CRD kinds and performing live lookups to extract API versions, replacing previously hardcoded entries. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request updates the kube-state-metrics configuration to dynamically resolve Flux CRD versions using the Helm lookup function and introduces regex-based pluralization for resource names. Review feedback identifies a significant issue where the use of lookup causes the configuration to be omitted during static manifest generation or in GitOps environments without cluster access. Additionally, the regex logic for pluralization is flagged as fragile, with a recommendation to explicitly define plural names in the source data for better maintainability.
There was a problem hiding this comment.
Pull request overview
Updates the kube-state-metrics customResourceState configuration in the base-cluster Helm chart to pin Flux custom resource scraping to a single CRD version, aiming to avoid deprecated API usage alerts in the cluster.
Changes:
- Uses Helm
lookupto fetch each Flux CRD and derive a specific version for thegroupVersionKindconfig. - Updates the generated Flux custom resource metrics entries to include the resolved
groupVersionKind.version.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
charts/base-cluster/templates/monitoring/kube-prometheus-stack/_kube-state-metrics-config.yaml (1)
114-139:⚠️ Potential issue | 🟠 MajorStatic ImagePolicy entry lacks
versionfield - inconsistent with PR objective.The static
ImagePolicyentry for thelatest_versionmetric does not include aversioningroupVersionKind(lines 115-118), while the dynamic entries now include the storage version. This means:
- Dynamic ImagePolicy entry (from loop): watches only the latest/storage API version
- This static entry: watches all API versions
This defeats the PR objective of "only watch and create metrics for the latest apiVersion" for the
latest_versionmetric, and deprecatedAPIAlerts may still be generated for this resource.Consider applying the same CRD lookup pattern here to include the version:
Proposed fix
+ {{- $imagePolicyCrd := lookup "apiextensions.k8s.io/v1" "CustomResourceDefinition" "" "imagepolicies.image.toolkit.fluxcd.io" -}} + {{- if $imagePolicyCrd -}} + {{- $imagePolicyVersion := "" -}} + {{- range $version := $imagePolicyCrd.spec.versions -}} + {{- if $version.storage -}} + {{- $imagePolicyVersion = $version.name -}} + {{- end -}} + {{- end -}} {{- $resources = append $resources (dict "groupVersionKind" (dict "group" "image.toolkit.fluxcd.io" "kind" "ImagePolicy" + "version" $imagePolicyVersion ) "metricNamePrefix" "image_policy" "metrics" (list ... ) ) }} + {{- end -}}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@charts/base-cluster/templates/monitoring/kube-prometheus-stack/_kube-state-metrics-config.yaml` around lines 114 - 139, The static ImagePolicy resource dict (the dict with "groupVersionKind" having group "image.toolkit.fluxcd.io" and kind "ImagePolicy", metricNamePrefix "image_policy" and metric "latest_version") is missing a "version" field and therefore watches all API versions; update this dict to set "version" to the storage/latest API version using the same CRD lookup pattern used for dynamic entries (i.e., compute the CRD's storage/version value and inject it into groupVersionKind.version), leaving the metric definition and labelsFromPath unchanged so the static entry matches the dynamic entries' behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In
`@charts/base-cluster/templates/monitoring/kube-prometheus-stack/_kube-state-metrics-config.yaml`:
- Around line 114-139: The static ImagePolicy resource dict (the dict with
"groupVersionKind" having group "image.toolkit.fluxcd.io" and kind
"ImagePolicy", metricNamePrefix "image_policy" and metric "latest_version") is
missing a "version" field and therefore watches all API versions; update this
dict to set "version" to the storage/latest API version using the same CRD
lookup pattern used for dynamic entries (i.e., compute the CRD's storage/version
value and inject it into groupVersionKind.version), leaving the metric
definition and labelsFromPath unchanged so the static entry matches the dynamic
entries' behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 7eb00fa5-e6d5-4b0a-bfb4-d28a629ab3e4
📒 Files selected for processing (1)
charts/base-cluster/templates/monitoring/kube-prometheus-stack/_kube-state-metrics-config.yaml
…t apiVersion That way there are also no deprecatedAPIAlerts
5815322 to
d532d0d
Compare
🤖 I have created a release *beep* *boop* --- ## [11.1.2](base-cluster-v11.1.1...base-cluster-v11.1.2) (2026-04-01) ### Bug Fixes * **base-cluster/monitoring:** only watch and create metrics for latest apiVersion ([#2037](#2037)) ([b233d68](b233d68)) ### Miscellaneous Chores * **base-cluster/dependencies:** update docker.io/fluxcd/flux-cli docker tag to v2.8.3 ([#2017](#2017)) ([a4ecf9b](a4ecf9b)) * **base-cluster/dependencies:** update docker.io/vladgh/gpg docker tag to v1.3.9 ([#2022](#2022)) ([59f892e](59f892e)) * **base-cluster/dependencies:** update helm release alloy to v1.6.2 ([#2011](#2011)) ([1f593af](1f593af)) * **base-cluster/dependencies:** update helm release kube-prometheus-stack to v82.10.5 ([#2026](#2026)) ([ccfe7b1](ccfe7b1)) * **base-cluster/dependencies:** update helm release traefik to v39.0.7 ([#2013](#2013)) ([bdca6cc](bdca6cc)) * **base-cluster/dependencies:** update helm release trivy-operator to v0.32.1 ([#1990](#1990)) ([cab5a0d](cab5a0d)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Monitoring now only watches and creates metrics for the latest apiVersion. * **Chores** * Bumped base-cluster chart to v11.1.2 and updated release metadata and manifest entry. * Updated container image tags and Helm dependency versions (flux-cli, gpg, alloy, kube-prometheus-stack, traefik, trivy-operator). * **Documentation** * Updated chart version badge and source link in the README and added a changelog entry for v11.1.2. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
That way there are also no deprecatedAPIAlerts
Summary by CodeRabbit
Release Notes