Repository navigation
Commit 6f8e047
Require HPA coverage before treating the evidence as conclusive (#1755)
Closes #1749.
`HPAEvidenceAvailable` is not a caveat — it is a safety gate.
Rightsizing withholds a reduction when `Fit == FitOversized &&
!HPAEvidenceAvailable`, because shrinking a request an autoscaler is
scaling on is the one mistake that matters here. A false positive
therefore does not merely mislead; it lets the reduction through.
A synced, non-nil HPA informer answers an empty list for every namespace
it does not watch, and it answers **without an error**. Both rightsizing
paths read that as "this workload has no autoscaler" and set the flag.
Radar falls back to namespace-scoped informers whenever cluster-wide
list is denied, so this is reachable in ordinary multi-tenant operation
rather than a corner case.
## What changed
Availability now follows the informer's own coverage, decided **per
workload** — a cluster-wide scan can span a watched and an unwatched
namespace at once, so a single answer for the whole scan would be wrong
either way.
Coverage is consulted via `KindCoversNamespace`, deliberately rather
than asking whether the informer is cluster-wide. A multi-namespace
informer is authoritative for every namespace it actually watches, and
testing cluster-wide-ness would have suppressed valid recommendations in
all of them.
## Full access is unchanged by construction
`NewResourceCache` normalises an unrestricted cache to `Enabled: true`
with an empty namespace, for which `KindCoversNamespace` returns true
for every namespace. Both new gates are no-ops there. The reverse — that
this cannot newly suppress a valid recommendation — holds for the same
reason: coverage is derived from the same `resourceScopeNamespaces` used
to build the informer, so a namespace that is watched is a namespace
that is covered.
## Testing
Go tests pin both directions: full access marks every workload
available, and an HPA informer scoped to one namespace marks only that
namespace's workloads — driven through `enrichScanHPA` with a real
utilization-based HPA, so target matching and the `hpaManaged` flag are
exercised rather than just the marking helper.
`go build ./...`, `go vet`, `go test ./internal/prometheus/` (including
`-race`), and the separate `pkg/k8score` module tests pass.
**Live, against GKE with real Prometheus and kube-state-metrics.**
Binaries from this branch and from `main` were run against the same
cluster under two identities.
*Full access — the case that must not change:*
| | `main` | this branch |
|---|---|---|
| `Deployment staging/koala-backend` | `hpaEvidenceAvailable=true`, same
fit and reason | identical |
| Cluster scan, 26 workloads / 52 rows | `hpaEvidenceAvailable` true on
52 | true on 52 |
| Decision fields across 52 rows | — | identical across repeat runs |
Repeat scans differed only in measured values (observed, sample counts),
and those differences moved between runs and workloads — live drift, not
behaviour.
*HPA readable in `staging` only — the condition the bug needs.* A
throwaway ServiceAccount was granted everything cluster-wide except
`horizontalpodautoscalers`, plus a Role granting HPA reads in `staging`
alone, which drives Radar to a namespace-scoped HPA informer (`Using
namespace-scoped informers for namespace(s): ["staging"]`).
| `Deployment dev/finops` (HPAs **not** readable in `dev`) | `main` |
this branch |
|---|---|---|
| `hpaEvidenceAvailable` | `true` | **`false`** |
| `recommendationReason` | *none* | `hpa_evidence_unavailable` |
| Recommended request | **cpu `100m`, memory `256Mi`** | *withheld* |
`main` suggests shrinking a workload whose autoscaler it could never
have seen. In the same run, `Deployment staging/koala-backend` — where
the informer does watch the namespace — is **identical on both builds**,
recommendations included, so the gate suppresses only what it cannot
see.
The ServiceAccount, Role, RoleBinding, ClusterRole, ClusterRoleBinding
and namespace were deleted afterwards and verified gone.
## Follow-up
Both paths still reject HPA evidence while *any* deferred informer is
warming or has failed, even when the HPA informer itself is fully synced
and cluster-wide — a false negative that suppresses valid
recommendations. It predates this change and fixing it here would alter
full-access behaviour, which this PR deliberately does not do.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_0125QDX24TNuod7FKLwTDhvW
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Changes the safety gate that blocks downsize recommendations when HPA
inventory may be incomplete; behavior is intentionally stricter for
namespace-scoped informers but full-access paths are designed to stay
equivalent.
>
> **Overview**
> Fixes a safety bug where **namespace-scoped HPA informers** return an
empty list (with no error) for namespaces they do not watch, and
rightsizing treated that as proof there was no autoscaler—allowing
**CPU/memory reduction** recommendations that should be blocked when HPA
evidence is unknown.
>
> Both the **single-workload** path (`loadHPAManagedResources`) and the
**cluster scan** path (`markScanHPAAvailable`) now call
**`KindCoversNamespace`** per workload namespace before treating an HPA
list as authoritative. Workloads outside the informer’s scope no longer
get `hpaEvidenceAvailable` / `hpaAvailable`, so oversized fits stay
withheld with **`hpa_evidence_unavailable`** instead of a shrink.
Unrestricted caches are unchanged because full coverage still marks
every namespace as readable.
>
> Tests cover scoped vs full access, the marking helper, and end-to-end
**`enrichScanHPA`** with a real utilization-based HPA.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
f64b5fd. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>1 parent 672f967 commit 6f8e047
3 files changed
Lines changed: 152 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
336 | 337 | | |
337 | 338 | | |
338 | 339 | | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
339 | 347 | | |
340 | 348 | | |
341 | 349 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
723 | 724 | | |
724 | 725 | | |
725 | 726 | | |
726 | | - | |
| 727 | + | |
727 | 728 | | |
728 | 729 | | |
729 | 730 | | |
730 | 731 | | |
731 | 732 | | |
732 | 733 | | |
733 | 734 | | |
734 | | - | |
| 735 | + | |
735 | 736 | | |
736 | 737 | | |
737 | 738 | | |
| |||
751 | 752 | | |
752 | 753 | | |
753 | 754 | | |
754 | | - | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
755 | 760 | | |
756 | | - | |
757 | | - | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
758 | 766 | | |
| 767 | + | |
759 | 768 | | |
760 | 769 | | |
761 | 770 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
338 | 339 | | |
339 | 340 | | |
340 | 341 | | |
341 | | - | |
| 342 | + | |
342 | 343 | | |
343 | 344 | | |
344 | 345 | | |
| |||
487 | 488 | | |
488 | 489 | | |
489 | 490 | | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
0 commit comments