Skip to content

fix: Don't sync HPA spec from autoscaling policy for excluded Tortoises - #474

Draft
AdityaK011 wants to merge 1 commit into
mainfrom
fix-hpa-spec-sync-respect-exclusion
Draft

AdityaK011 wants to merge 1 commit into
mainfrom
fix-hpa-spec-sync-respect-exclusion

Conversation

@AdityaK011

Copy link
Copy Markdown
Collaborator

What this PR does / why we need it:

Namespace exclusion, global disable mode and ScaleOps management are meant to stop Tortoise from changing a workload's HPA. One write path still ignores them.

UpdateHPASpecFromTortoiseAutoscalingPolicy runs on every reconcile and only checks updateMode: Off. For an excluded Tortoise it still:

  • rewrites spec.metrics to match status.autoscalingPolicy, removing any container resource metric the policy doesn't mark Horizontal and adding missing ones at 70%
  • calls disableHPA on a user-provided HPA when no policy is Horizontal, which clears the metrics and sets minReplicas = maxReplicas = current replicas
  • deletes a Tortoise-created HPA in the same situation

We saw the first case in one of our clusters. A team added istio-proxy metrics to an HPA in an excluded namespace, and within about 80 seconds Tortoise removed them because its policy has istio-proxy set to Off. The Tortoise's own EffectiveModeOverridden condition said NamespaceExclusion at the time.

This PR adds the same IsChangeApplicationDisabled gate that UpdateHPAFromTortoiseRecommendation and both webhooks already use.

It also fixes hpaEdited in syncHPAMetricsWithTortoiseAutoscalingPolicy. It was set in the branch that keeps a metric, so any HPA with a kept container metric counted as edited. That meant every reconcile sent an HPA Update and emitted an HPAUpdated event ("because the autoscaling policy is changed in the tortoise") even when nothing had changed. Now it's only set when a metric is added or removed, including the existing removal of Resource-type metrics.

Which issue(s) this PR fixes:

No issue filed. Happy to open one if you'd prefer to discuss it there first.

Special notes for your reviewer:

This reverses one part of #465, which removed this gate on purpose so the HPA spec would keep syncing in excluded namespaces for data collection and to avoid "metric not found" errors. cc @AVSBharadwaj, it'd be good to hear whether there's a case I'm missing.

My reasoning for adding it back:

  • fix: Prevent panic and allow HPA sync for excluded namespaces #465 also made the recommender tolerate missing HPA metrics (calculateBestNewSize and updateHPATargetUtilizationRecommendations now skip instead of erroring), so an excluded Tortoise whose HPA lacks a metric no longer fails the reconcile. The whole suite passes with the gate, including the envtest controller tests.
  • The sync isn't dry-run. Removing a metric, adding one with a 70% target, or disabling the HPA all change how the workload scales, which is what exclusion is supposed to prevent.
  • Once a namespace is taken off the exclusion list, the next reconcile syncs the metrics as before.

The trade-off is that an excluded Tortoise whose HPA is missing a Horizontal metric won't compute a target-utilization recommendation for that metric until it's un-excluded.

Tests: TestService_UpdateHPASpecFromTortoiseAutoscalingPolicy_SkipAndNoop covers excluded namespace (metrics kept, HPA not disabled), global disable mode, the no-op case (HPA not written when it already matches), and normal removal. The first four fail on main and pass with this change. I ran go test for all packages except e2e against envtest 1.31.0. make test itself fails locally because controller-gen v0.16.1 doesn't build with Go 1.26, which doesn't affect this change.

🤖 Generated with Claude Code

UpdateHPASpecFromTortoiseAutoscalingPolicy runs on every reconcile but only
checked updateMode Off, so Tortoises in excluded namespaces, under global
disable mode, or managed by ScaleOps still had their HPA metrics rewritten
to match the autoscaling policy. The same path could also disable a
user-provided HPA (metrics cleared, min=max=current replicas) or delete a
Tortoise-created one.

Gate it with IsChangeApplicationDisabled like the other HPA write paths.

Also only mark the HPA as edited when a metric is actually added or
removed. The keep branch set hpaEdited, so every reconcile issued an HPA
Update and an HPAUpdated event even when nothing changed.
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