Skip to content

OCPBUGS-84872: fix O(n²) metrics collection in Collect() - #101

Open
aswinsuryan wants to merge 1 commit into
openshift:mainfrom
aswinsuryan:OCPBUGS-84872
Open

OCPBUGS-84872: fix O(n²) metrics collection in Collect()#101
aswinsuryan wants to merge 1 commit into
openshift:mainfrom
aswinsuryan:OCPBUGS-84872

Conversation

@aswinsuryan

@aswinsuryan aswinsuryan commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Replace the nested loop over all ingresses per route with a direct lister lookup using the owner reference name. Add a managedCache to avoid redundant ingressManaged calls for ingresses shared by many routes, reducing complexity from O(routes x ingresses) to O(routes).

At 7400 routes x 7500 ingresses this caused the metrics endpoint to exceed the default 10s Prometheus scrape timeout on the leader pod.

Summary by CodeRabbit

  • Bug Fixes
    • Improved ingress metrics collection for routes sharing the same ingress.
    • Collection now continues when individual ingress checks encounter errors, while affected metrics are safely skipped.
    • Improved labeling accuracy for managed and unmanaged routes, including cleanup of outdated route labels.
    • Reduced duplicate processing to provide more consistent and efficient metrics across namespaces.

@openshift-merge-bot

Copy link
Copy Markdown
Contributor

Pipeline controller notification
This repo is configured to use the pipeline controller. Second-stage tests will be triggered either automatically or after lgtm label is added, depending on the repository configuration. The pipeline controller will automatically detect which contexts are required and will utilize /test Prow commands to trigger the second stage.

For optional jobs, comment /test ? to see a list of all defined jobs. To trigger manually all jobs from second stage use /pipeline required command.

This repository is configured in: LGTM mode

@openshift-ci-robot openshift-ci-robot added jira/severity-moderate Referenced Jira bug's severity is moderate for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type. jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. labels Aug 7, 2026
@openshift-ci-robot

Copy link
Copy Markdown
Contributor

@aswinsuryan: This pull request references Jira Issue OCPBUGS-84872, which is invalid:

  • expected the bug to target the "5.0.0" version, but no target version was set

Comment /jira refresh to re-evaluate validity if changes to the Jira bug are made, or edit the title of this pull request to link to a different bug.

The bug has been updated to refer to the pull request using the external bug tracker.

Details

In response to this:

Replace the nested loop over all ingresses per route with a direct lister lookup using the owner reference name. Add a managedCache to avoid redundant ingressManaged calls for ingresses shared by many routes, reducing complexity from O(routes x ingresses) to O(routes).

At 7400 routes x 7500 ingresses this caused the metrics endpoint to exceed the default 10s Prometheus scrape timeout on the leader pod.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 1baf6f96-40dd-4771-b4af-98d3329d5a67

📥 Commits

Reviewing files that changed from the base of the PR and between b71f47b and f6e7f0d.

📒 Files selected for processing (1)
  • pkg/route/ingress/metrics.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/route/ingress/metrics.go

Walkthrough

Ingress metrics collection now caches namespace-qualified ingress management results, performs direct namespace-scoped lookups, and skips only routes with management errors. Stale route metric labels are removed before skipped updates.

Changes

Ingress metrics collection

Layer / File(s) Summary
Management lookup handling
pkg/route/ingress/metrics.go
Collect classifies Kubernetes lookup errors, caches results by namespace and owner name, treats only NotFound errors as unmanaged, and skips affected routes for other errors. Stale metric labels are deleted before skipped updates.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: jcmoraisjr, rikatz

🚥 Pre-merge checks | ✅ 15
✅ Passed checks (15 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the performance fix to metrics collection, which is the main change in the pull request.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Stable And Deterministic Test Names ✅ Passed The PR changes only pkg/route/ingress/metrics.go and adds no test declarations; existing test titles and table names are static literals.
Test Structure And Quality ✅ Passed The commit changes only pkg/route/ingress/metrics.go and adds or modifies no Ginkgo test code, so the listed test-quality checks are not applicable.
Microshift Test Compatibility ✅ Passed The pull request changes only pkg/route/ingress/metrics.go and adds no Ginkgo e2e tests, so MicroShift test compatibility is not applicable.
Single Node Openshift (Sno) Test Compatibility ✅ Passed The pull request changes only pkg/route/ingress/metrics.go and adds no Ginkgo e2e tests, so the SNO compatibility check is not applicable.
Topology-Aware Scheduling Compatibility ✅ Passed The commit changes only pkg/route/ingress/metrics.go, and its hunks modify Prometheus metrics and lister error handling; no manifests or scheduling constraints are introduced.
Ote Binary Stdout Contract ✅ Passed The PR changes only metrics.go; it adds no stdout writes or OTE suite code, and the repository contains only a route-controller-manager main.
Ipv6 And Disconnected Network Test Compatibility ✅ Passed The pull request changes only pkg/route/ingress/metrics.go and adds no Ginkgo e2e tests, IPv4 assumptions, or external connectivity requirements.
No-Weak-Crypto ✅ Passed The PR changes only pkg/route/ingress/metrics.go; added code uses Kubernetes errors, listers, caching, and metrics, with no weak crypto, custom crypto, or secret comparisons found.
Container-Privileges ✅ Passed The PR changes only pkg/route/ingress/metrics.go; no privileged, host namespace, SYS_ADMIN, allowPrivilegeEscalation, or root settings appear in the diff or manifests.
No-Sensitive-Data-In-Logs ✅ Passed The only new log sink is utilruntime.HandleError(err) for lister/cache errors; NotFound is excluded, and no passwords, tokens, hosts, PII, or object fields are logged.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@openshift-ci
openshift-ci Bot requested review from jcmoraisjr and rikatz August 7, 2026 23:49
@openshift-ci

openshift-ci Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: aswinsuryan
Once this PR has been reviewed and has the lgtm label, please assign bentito for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
pkg/route/ingress/metrics.go (1)

89-98: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

Cache error outcomes for shared owners.

The cache is populated only after ingressManaged succeeds. If many routes reference an ingress whose management check errors, each route repeats the ingress lookup and management check. Cache the complete lookup outcome, including errors and not-found status, so each namespace-qualified owner is evaluated once per collection.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@pkg/route/ingress/metrics.go` around lines 89 - 98, Update the managedCache
flow in the route collection logic to cache the complete ingress lookup and
management outcome for each namespace-qualified cacheKey, including errors and
not-found results. Ensure shared owners are evaluated only once per collection,
while preserving existing error handling and managed-state behavior for
subsequent routes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/route/ingress/metrics.go`:
- Around line 91-93: Update the owner lookup in the route handling flow around
ingressManaged to distinguish apierrors.IsNotFound(err) from other
IngressNamespaceLister.Get failures. Treat only NotFound as a missing owner;
handle or propagate other errors so the route is not marked unmanaged and cached
due to the default managed value.
- Around line 94-96: In the ingress metrics collection loop, update the err
branch around ingressManaged to delete the affected route’s labels from
unmanagedRoutes before continuing, preventing stale GaugeVec entries after
collection errors. Extend TestMetrics to cover an errored collect and verify the
route metric is removed.

---

Nitpick comments:
In `@pkg/route/ingress/metrics.go`:
- Around line 89-98: Update the managedCache flow in the route collection logic
to cache the complete ingress lookup and management outcome for each
namespace-qualified cacheKey, including errors and not-found results. Ensure
shared owners are evaluated only once per collection, while preserving existing
error handling and managed-state behavior for subsequent routes.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: openshift/coderabbit/.coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 521e6564-ded1-4696-8965-7dbaed5dca80

📥 Commits

Reviewing files that changed from the base of the PR and between 59697cf and b71f47b.

📒 Files selected for processing (1)
  • pkg/route/ingress/metrics.go

Comment thread pkg/route/ingress/metrics.go Outdated
Comment thread pkg/route/ingress/metrics.go Outdated
@aswinsuryan

Copy link
Copy Markdown
Contributor Author

/jira refresh

@openshift-ci-robot

Copy link
Copy Markdown
Contributor

@aswinsuryan: This pull request references Jira Issue OCPBUGS-84872, which is invalid:

  • expected the bug to target the "5.0.0" version, but no target version was set

Comment /jira refresh to re-evaluate validity if changes to the Jira bug are made, or edit the title of this pull request to link to a different bug.

Details

In response to this:

/jira refresh

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

Replace the nested loop over all ingresses per route with a direct
lister lookup using the owner reference name. Add a managedCache to
avoid redundant ingressManaged calls for ingresses shared by many
routes, reducing complexity from O(routes x ingresses) to O(routes).

At 7400 routes x 7500 ingresses this caused the metrics endpoint to
exceed the default 10s Prometheus scrape timeout on the leader pod.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@openshift-ci

openshift-ci Bot commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

@aswinsuryan: all tests passed!

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

@gcs278

gcs278 commented Aug 12, 2026

Copy link
Copy Markdown

/assign @rfredette

@aswinsuryan

Copy link
Copy Markdown
Contributor Author

/jira refresh

@openshift-ci-robot openshift-ci-robot added the jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. label Aug 20, 2026
@openshift-ci-robot

Copy link
Copy Markdown
Contributor

@aswinsuryan: This pull request references Jira Issue OCPBUGS-84872, which is valid. The bug has been moved to the POST state.

3 validation(s) were run on this bug
  • bug is open, matching expected state (open)
  • bug target version (5.1.0) matches configured target version for branch (5.1.0)
  • bug is in the state New, which is one of the valid states (NEW, ASSIGNED, POST)

Requesting review from QA contact:
/cc @melvinjoseph86

Details

In response to this:

/jira refresh

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@openshift-ci-robot openshift-ci-robot removed the jira/invalid-bug Indicates that a referenced Jira bug is invalid for the branch this PR is targeting. label Aug 20, 2026
@openshift-ci
openshift-ci Bot requested a review from melvinjoseph86 August 20, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jira/severity-moderate Referenced Jira bug's severity is moderate for the branch this PR is targeting. jira/valid-bug Indicates that a referenced Jira bug is valid for the branch this PR is targeting. jira/valid-reference Indicates that this PR references a valid Jira ticket of any type.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants