Skip to content

feat: Implement scale-to-zero support for BudAIScaler - #4

Merged
dittops merged 1 commit into
mainfrom
feat/scale-to-zero
Jan 26, 2026
Merged

dittops merged 1 commit into
mainfrom
feat/scale-to-zero

Conversation

@dittops

@dittops dittops commented Jan 26, 2026

Copy link
Copy Markdown
Member

Summary

  • Add ScaleToZeroConfig to CRD for configuring scale-to-zero behavior with grace period and activation scale
  • Allow maxReplicas: 0 to completely disable workloads and free resources
  • Track ZeroDemandSince in status for grace period calculation
  • Update algorithms (BudScaler, KPA) to respect scale-to-zero configuration
  • Handle zero-pod scenarios in metrics collector

Features

  • Demand-based scale-to-zero: Configure scaleToZeroConfig with grace period before scaling to 0
  • Manual disable: Set maxReplicas: 0 to immediately scale to 0
  • Activation scale: Configure how many replicas to wake up to from zero

Example Usage

apiVersion: scaler.bud.studio/v1alpha1
kind: BudAIScaler
spec:
  minReplicas: 0
  maxReplicas: 10
  scaleToZeroConfig:
    enabled: true
    activationScale: 2
    gracePeriod: 5m

Test plan

  • Unit tests pass
  • Tested maxReplicas: 0 in k3d cluster - deployment scales to 0
  • Tested scale back up with minReplicas: 1

🤖 Generated with Claude Code

The CRD now allows maxReplicas=0, but the controller had a redundant
validation that rejected it. Removed to allow complete workload disable.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @dittops, 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 introduces comprehensive scale-to-zero functionality for the BudAIScaler. It allows users to configure demand-based scaling down to zero with a grace period, manually disable workloads by setting maxReplicas to zero, and define an activation scale for waking up from a zero-replica state. These changes enhance resource efficiency and provide greater control over workload lifecycle.

Highlights

  • Scale-to-Zero Configuration: Introduced ScaleToZeroConfig in the CRD to allow detailed configuration of scale-to-zero behavior, including grace periods and activation scale.
  • maxReplicas: 0 Support: Enabled the ability to set maxReplicas to 0, allowing complete disabling of workloads and freeing up resources.
  • Zero Demand Tracking: Added ZeroDemandSince to the scaler's status to track when demand dropped to zero, facilitating grace period calculations.
  • Algorithm Updates: Modified BudScaler and KPA algorithms to properly respect and utilize the new scale-to-zero configuration.
  • Metrics Handling: Ensured that the metrics collector correctly handles scenarios where the number of pods is zero.

🧠 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.

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. ↩

@dittops
dittops merged commit 113db3d into main Jan 26, 2026
5 checks passed

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request implements scale-to-zero support for BudAIScaler, which is a valuable feature for resource optimization. The changes include updates to the CRD, status tracking, and scaling algorithms to handle zero-replica states. The modification in budaiscaler_controller.go correctly removes the validation that prevented maxReplicas: 0, enabling workloads to be manually disabled. However, my review identified a critical side-effect of this change: a potential division-by-zero panic in the cost metrics collection logic when a workload is scaled to zero. This must be addressed before merging.

I am having trouble creating individual review comments. Click here to see my feedback.

pkg/controller/budaiscaler/budaiscaler_controller.go (206-208)

critical

While removing this validation is necessary to support maxReplicas: 0, it introduces a critical risk of a division-by-zero panic.

The collectCostMetrics function in pkg/controller/budaiscaler/autoscaler.go calculates perReplicaCost by dividing by currentReplicas. When a workload is scaled to zero as a result of this change, currentReplicas will be 0, causing the controller to panic.

This affects both the logging statement and the returned CostMetrics struct. You should add a check to handle the currentReplicas == 0 case.

Recommendation:
In pkg/controller/budaiscaler/autoscaler.go, update collectCostMetrics to safely handle zero replicas:

// in collectCostMetrics function
var perReplicaCost float64
if currentReplicas > 0 {
	perReplicaCost = hourlyCost / float64(currentReplicas)
}

klog.V(4).InfoS("Cost metrics calculated",
	"provider", provider,
	"replicas", currentReplicas,
	"hourlyCost", hourlyCost,
	"budgetPerHour", budgetPerHour,
	"perReplicaCost", perReplicaCost)

return &types.CostMetrics{
	CurrentCostPerHour:    hourlyCost,
	BudgetPerHour:         budgetPerHour,
	PerReplicaCostPerHour: perReplicaCost,
}

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.

1 participant