Skip to content

Conversation

@TrueBurn
Copy link

@TrueBurn TrueBurn commented Dec 13, 2025

Summary

Fixes #16171

Occurred changes and/or fixed issues

When viewing an HPA (Horizontal Pod Autoscaler) with custom metrics (Pods, External, or Object types), the dashboard throws a TypeError and displays nothing. Users must resort to using kubectl describe hpa to see the metrics.

Root Cause:
The mappedMetrics computed property in the HPA detail view attempts to access nested properties (metricValue.metric.name and metric.resource.name) without null/undefined checks. When the API returns a metric with a type field but missing the corresponding nested property, accessing .name throws:
TypeError: Cannot read properties of undefined (reading 'name')

Changes Made:

  • Added optional chaining to metricValue?.metric?.name at line 55 (custom metrics)
  • Added optional chaining to metric.resource?.name at line 57 (resource metrics)
  • Added test suite "with malformed metrics" to validate graceful handling
  • Tests ensure no TypeError is thrown and null values are handled correctly

Technical notes summary

The fix uses optional chaining (?.) to safely access nested properties, following the existing pattern already used at line 72 in the same file (metricValue?.metric?.name ?? null). This is a minimal, defensive coding approach that:

  • Maintains backward compatibility with valid metrics
  • Gracefully handles malformed or incomplete metric data
  • Follows established patterns in the codebase
  • Has no performance impact (optional chaining is syntactic sugar)

Areas or cases that should be tested

Manual testing:

  1. View an HPA with custom Pods metrics
  2. View an HPA with External metrics (e.g., from Prometheus)
  3. View an HPA with Object metrics
  4. View an HPA with standard Resource metrics (CPU/Memory)
  5. View an HPA with mixed metric types
  6. Verify browser console shows no errors
  7. Verify metrics display correctly (or show null gracefully if malformed)

Automated testing:

  • Unit tests added for malformed metrics scenarios
  • Existing tests verify valid metrics still work correctly

Areas which could experience regressions

  • HPA detail page metric rendering (/shell/detail/autoscaling.horizontalpodautoscaler/index.vue)
  • HPA list view display
  • Other components that consume HPA metric data (unlikely, as this is view-only)

Screenshot/Video

Not applicable - this is a JavaScript error fix with no UI changes. The UI behavior remains the same for valid metrics and now gracefully handles invalid data.

Checklist

  • The PR is linked to an issue and the linked issue has a Milestone, or no issue is needed
  • The PR has a Milestone
  • The PR template has been filled out
  • The PR has been self reviewed
  • The PR has a reviewer assigned
  • The PR has automated tests or clear instructions for manual tests and the linked issue has appropriate QA labels, or tests are not needed
  • The PR has reviewed with UX and tested in light and dark mode, or there are no UX changes
  • The PR has been reviewed in terms of Accessibility
  • The PR has considered, and if applicable tested with, the three Global Roles Admin, Standard User and User Base

…nal chaining to safely access metric properties when rendering HPAs with custom metrics (Pods, External, Object types). This prevents TypeError when metricValue or nested properties are undefined due to malformed or incomplete metric data. Changes: - Added optional chaining for metricValue?.metric?.name (line 55) - Added optional chaining for metric.resource?.name (line 57) - Added test coverage for malformed metrics scenarios

Signed-off-by: TrueBurn <[email protected]>
@richard-cox
Copy link
Member

@TrueBurn Thanks for creating this PR. Can you confirm that it is your own work and not from AI? I also added back the checklist, can you fill in the best you can?

@richard-cox richard-cox changed the title Fix: Handle undefined metrics in HPA detail view (#46712) Fix: Handle undefined metrics in HPA detail view Dec 14, 2025
// See for examples: https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#appendix-horizontal-pod-autoscaler-status-conditions
if (metricType !== 'resource') {
currentMatch = findBy(currentMetrics, `${ metricType }.metric.name`, metricValue.metric.name);
currentMatch = findBy(currentMetrics, `${ metricType }.metric.name`, metricValue?.metric?.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment only.

if the value metricValue.metric.name doesn't exist there's no reason to search for it. however given that i don't think currentMetrics scales very high and this is more legible i think we could keep as is

@TrueBurn
Copy link
Author

TrueBurn commented Dec 15, 2025

@TrueBurn Thanks for creating this PR. Can you confirm that it is your own work and not from AI? I also added back the checklist, can you fill in the best you can?

Hi @richard-cox

I wrote the code but had AI do the documentation for me. This was just a irritation while using prometheus-adapter but we are taking KEDA to PROD next year and would like ot get this sorted before then so that my ops team can use Rancher Dashboard to view the HPA current values as we don't allow them to use kubectl.

I will go through the checklist and update accordingly.

@richard-cox richard-cox added this to the v2.14.0 milestone Dec 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]hpa cannot display detailed information

2 participants