Skip to content

fix: autoscaler use target's namespace if specified#797

Open
dblate wants to merge 1 commit intovolcano-sh:mainfrom
dblate:fix-autoscaler
Open

fix: autoscaler use target's namespace if specified#797
dblate wants to merge 1 commit intovolcano-sh:mainfrom
dblate:fix-autoscaler

Conversation

@dblate
Copy link
Copy Markdown

@dblate dblate commented Mar 4, 2026

What type of PR is this?

/kind bug

What this PR does / why we need it: When the namespace of the modelserving pod differs from that of the autoscaler, the namespace corresponding to the modelserving pod is used first to ensure the pod is correctly located.

Which issue(s) this PR fixes:
Fixes autoscaler, use target's namespace if specified

Special notes for your reviewer:

Does this PR introduce a user-facing change?: NONE


Signed-off-by: yuhui06 <173983476@qq.com>
Copilot AI review requested due to automatic review settings March 4, 2026 07:13
@volcano-sh-bot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign lizhencheng9527 for approval. For more information see the Kubernetes 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

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a bug in the autoscaler's metric collection logic. It ensures that the autoscaler correctly identifies and uses the namespace of the target modelserving pod, rather than solely relying on the autoscaling policy binding's namespace. This change is crucial for scenarios where the target pod resides in a different namespace than the autoscaler itself, preventing incorrect pod location and ensuring accurate metric collection.

Highlights

  • Namespace Resolution: Modified the MetricCollector initialization to prioritize the target's specified namespace for metric collection. If the target's namespace is not explicitly set, it defaults to the autoscaling policy binding's namespace.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • pkg/autoscaler/autoscaler/metric_collector.go
    • Updated the logic for determining the namespace used by the MetricCollector to prioritize the target's namespace.
Activity
  • No activity has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates autoscaler metric collection to correctly locate model-serving pods when the autoscaler’s namespace differs from the target workload’s namespace by preferring the target’s explicitly specified namespace.

Changes:

  • Derive the metric-collection namespace from target.TargetRef.Namespace when set.
  • Fall back to binding.Namespace when the target reference doesn’t specify a namespace.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request modifies pkg/autoscaler/autoscaler/metric_collector.go to correctly determine the namespace for metric collection, prioritizing the namespace specified in the target.TargetRef and falling back to the AutoscalingPolicyBinding's namespace. While this change aligns with the expectation that targets can exist in different namespaces, it introduces a significant security risk related to cross-namespace access control. Specifically, the NewMetricCollector function now accepts a namespace from the target reference without verifying it against the binding's namespace, potentially allowing unauthorized users to scrape metrics from pods in namespaces they should not have access to. It is recommended to add a validation check to ensure the target namespace matches the binding's namespace or implement a robust authorization mechanism for cross-namespace references.

Comment on lines +53 to +56
namespace := target.TargetRef.Namespace
if namespace == "" {
namespace = binding.Namespace
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

security-high high

The introduction of target.TargetRef.Namespace as a source for the collection namespace without validation creates a Broken Access Control vulnerability. An attacker who can create an AutoscalingPolicyBinding in one namespace can now target and collect metrics from pods in any other namespace across the cluster. Since the autoscaler controller typically operates with high privileges, this allows unauthorized cross-namespace data access. You should ensure that the target namespace is either empty or explicitly matches the namespace of the binding.

Copy link
Copy Markdown
Member

@hzxuzhonghu hzxuzhonghu left a comment

Choose a reason for hiding this comment

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

The target cannot be set a namespace != binding, i think we can validate it in the webhook, and not change this palce

@dblate
Copy link
Copy Markdown
Author

dblate commented Mar 4, 2026

The target cannot be set a namespace != binding, i think we can validate it in the webhook, and not change this palce

If the target cannot be set a namespace != binding, I have a scenario and would like to ask for advice on the practical approach:

  • The modelserving controller and autoscaler controller are deployed in namespace A.

  • The modelserving CRD is deployed in namespace B.

  • I want to deploy the autoscaler CRD to monitor and scale the modelserving related pods in namespace B.

  • Should I also deploy the autoscaler CRD in namespace B? Is this supported(autoscaler CRD deployed in a different namespace from it's controller)? I tried it and it seems that it cannot monitor metrics.

@hzxuzhonghu
Copy link
Copy Markdown
Member

The controller can be deployed in any namespace, it has nothing to do with the api object.

if modelserving in namespace A, you have to create AutoscalingPolicy and Binding in namespace A

@LiZhenCheng9527
Copy link
Copy Markdown
Contributor

@dblate Please update this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants