Skip to content

Count added nodes when scaling a Kubernetes cluster down - #14837

Open
bhouse-nexthop wants to merge 1 commit into
apache:4.22from
bhouse-nexthop:cks-scale-size-added-nodes
Open

bhouse-nexthop wants to merge 1 commit into
apache:4.22from
bhouse-nexthop:cks-scale-size-added-nodes

Conversation

@bhouse-nexthop

@bhouse-nexthop bhouse-nexthop commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Description

A CKS cluster's size includes nodes added with addNodesToKubernetesCluster, but scale-down only counted CKS-created workers. With added nodes present, scale-down removed too few nodes or none ("No nodes to remove"), still saved the new size, and a later scale-up waited until timeout for a Ready count that could not match.

Change Why
Scale-down by size removes CKS-created workers until created + added = requested size was ignoring added nodes
Every scale first re-counts the cluster's VMs and corrects stored worker, control and etcd counts if they drifted, recomputing cores and memory from each VM's own offering clusters damaged by the old behaviour are fixed by the next scale; API checks use the same count
A size equal to the stored size counts as unchanged; any other size on a drifted cluster is rejected until corrected the UI always sends size; on a drifted cluster "3 → 4" could mean add or remove, so don't guess
Clusters whose control node flags were never set (before 4.16) keep their stored counts, and their control VMs are never picked for removal those VMs can't be told apart from workers
Reject a size, or autoscaling minsize, below the number of added nodes (API check plus a worker check) scaling never removes added nodes
Scale nodeids only takes CKS-created workers; reject added, control and etcd nodes, and removing every worker they were skipped or removed without updating the counts
UI: no per-node delete button on added, control or etcd nodes; the VM list reports iscontrolnode the button called scale nodeids
size, minsize and nodeids docs say what counts and what is accepted

Scale-up was already right when the stored size matches the nodes: it creates size - nodeCount VMs and waits for control + size Ready nodes. With the count re-derived first, that is now always the real total.

Drifted cluster (e.g. stored size 5, 6 real workers): any scale that keeps size 5 (or passes 6, or only changes the offering) corrects it to 6. Asking for another size first is rejected with a message saying to correct it. After that, requests are exact.

Fixes #14836

Types of changes

  • Breaking change (fix or feature that would cause existing functionality to change)
  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Enhancement (improves an existing feature and functionality)
  • Cleanup (Code refactoring and cleanup, that may add test cases)
  • Build/CI
  • Test (unit or integration test code)

Feature/Enhancement Scale or Bug Severity

Bug Severity

  • BLOCKER
  • Critical
  • Major
  • Minor
  • Trivial

How Has This Been Tested?

Test Result
Unit tests: scale-down with added nodes, drift repair (worker/control/etcd counts, cores, memory), stored size passed back, ambiguous size on a drifted cluster, legacy unflagged control nodes, size/minsize below added nodes, nodeids with added/control/etcd/unknown nodes or all workers pass
kubernetes-service module unit tests 105 pass
Lab on the first revision (see comment below) pass; drift repair, nodeids and minsize checks still to be re-run

How did you try to break this feature and the system with this change?

@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 82.19178% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 18.34%. Comparing base (759bb81) to head (e3187d1).

Files with missing lines Patch % Lines
...er/actionworkers/KubernetesClusterScaleWorker.java 86.13% 6 Missing and 8 partials ⚠️
...bernetes/cluster/KubernetesClusterManagerImpl.java 78.57% 6 Missing and 3 partials ⚠️
...udstack/api/response/KubernetesUserVmResponse.java 0.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               4.22   #14837      +/-   ##
============================================
+ Coverage     18.32%   18.34%   +0.02%     
- Complexity    16648    16699      +51     
============================================
  Files          5939     5939              
  Lines        537799   537922     +123     
  Branches      65935    65960      +25     
============================================
+ Hits          98546    98697     +151     
+ Misses       428043   427999      -44     
- Partials      11210    11226      +16     
Flag Coverage Δ
uitests 4.08% <ø> (ø)
unittests 19.44% <82.19%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

Lab test on a 4.22-based build (8e994c8, this PR plus the related CKS PRs): KVM, isolated network, cluster with 1 control, 4 CKS-created workers and 2 nodes added with addNodesToKubernetesCluster (size 6).

step result
scale size=5 the newest CKS-created worker removed; kubectl shows 6 nodes; size 5, cluster Running; no "No nodes to remove"
scale size=6 succeeds, 7 nodes Ready
scale size=1 rejected in 2 s: cannot be scaled to size 1, it has 2 nodes added with addNodesToKubernetesCluster which scaling does not remove. Use removeNodesFromKubernetesCluster to remove them; cluster stays Running, nothing changed
scale size=2 all 4 CKS-created workers removed, both added nodes kept; 3 nodes Ready, cores/memory match
scale nodeids=<added node> rejected in 3 s: nodeids can not include nodes added with addNodesToKubernetesCluster, use removeNodesFromKubernetesCluster to remove them
UI the shipped bundle hides the actions column for nodes with isexternalnode (checked in the built JS, not clicked through)

@bhouse-nexthop
bhouse-nexthop force-pushed the cks-scale-size-added-nodes branch from 060a029 to 53c65e6 Compare November 24, 2026 11:51
bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 24, 2026
Follow-up to "Count added nodes when scaling a Kubernetes cluster
down", for its amended upstream version (apache#14837).

- Before scaling, recount the cluster's worker, control and etcd nodes
  from its VMs (validateAndSyncWorkerNodeCount). If the stored counts
  have drifted, correct them together with the cores and memory. A
  size passed back unchanged only repairs the stored count; any other
  size on a drifted cluster is refused with the size to scale to
  first.
- nodeids may only name CKS-created worker nodes. Control and etcd
  nodes are rejected in the manager and again in the worker.
- Autoscaling minsize cannot be lower than the number of added nodes.
- Clusters created before the control node flag existed: their first
  VMs are the control nodes and are never picked for scale-down.
- New iscontrolnode on the cluster VM response. The UI hides the
  delete-node button for control, etcd and added nodes.

Bundle adaptations: our scale-down still prefers worker VMs that are
down, but sorts only the removable workers, after the unflagged
control VMs are skipped by creation order. The recount runs after the
check that the cluster is still in the state the request was accepted
in, so it never corrects a cluster another operation has taken.

Signed-off-by: Brad House <bhouse@nexthop.ai>
bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 24, 2026
Follow-up to "Count added nodes when scaling a Kubernetes cluster
down", for its amended upstream version (apache#14837).

- Before scaling, recount the cluster's worker, control and etcd nodes
  from its VMs (validateAndSyncWorkerNodeCount). If the stored counts
  have drifted, correct them together with the cores and memory. A
  size passed back unchanged only repairs the stored count; any other
  size on a drifted cluster is refused with the size to scale to
  first.
- nodeids may only name CKS-created worker nodes. Control and etcd
  nodes are rejected in the manager and again in the worker.
- Autoscaling minsize cannot be lower than the number of added nodes.
- Clusters created before the control node flag existed: their first
  VMs are the control nodes and are never picked for scale-down.
- New iscontrolnode on the cluster VM response. The UI hides the
  delete-node button for control, etcd and added nodes.

Bundle adaptations: our scale-down still prefers worker VMs that are
down, but sorts only the removable workers, after the unflagged
control VMs are skipped by creation order. The recount runs after the
check that the cluster is still in the state the request was accepted
in, so it never corrects a cluster another operation has taken.

Signed-off-by: Brad House <bhouse@nexthop.ai>
@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

Lab re-run of the updated change on a 4.22-based build (94c33ae): KVM, isolated network. Cluster with 1 control and 4 CKS-created workers, plus 2 nodes added with addNodesToKubernetesCluster (size 6).

step result
scale size=5 one CKS-created worker removed (the newest), 6 nodes Ready, size 5
scale size=6 Running, 7 nodes Ready
scale size=1 rejected: cannot be scaled to size 1, it has 2 nodes added with addNodesToKubernetesCluster …; nothing changed
scale nodeids=<control VM> rejected: nodeids can only include worker nodes, control and etcd nodes can not be removed by scaling; nothing removed, Running
scale nodeids=<etcd VM> (external etcd cluster) rejected with the same message; nothing removed, Running
scale nodeids=<added node> rejected: nodeids can not include nodes added with addNodesToKubernetesCluster …; nothing removed, Running
scale nodeids=<CKS worker> removed, size 6 → 5
scale autoscalingenabled=true minsize=1 maxsize=5 rejected: cannot be scaled to minsize 1, it has 2 nodes added …; autoscaling stays off
listKubernetesClusters VM entries carry iscontrolnode (true only on the control VM), plus isexternalnode/isetcdnode
UI shipped bundle hides the actions column when isexternalnode, iscontrolnode or isetcdnode is set (checked in the built JS)

Drift repair. Drift was simulated in the database the way the old no-op scale-down left it: the stored size lowered, and cores/memory lowered by the same number of workers.

step stored size / cores / memory result
drift by 1, cluster stopped, scale size=<stored> with only a service offering change 4 / 10 / 23552 → 5 / 12 / 24576 no VM created or destroyed; size, cores and memory match the real VMs (added nodes keep their offering); log records … 4 worker nodes but has … 5, correcting it
start 5 / 12 / 24576 all 6 nodes Ready
drift by 2, scale size=<stored+1> 3 / 8 / 16384 rejected: records size 3 but has 5 worker nodes. Scale it to size 5 first to correct this, then to the size wanted
scale size=<real> (5) → 5 / 12 / 24576 accepted in 2 s, no VM change, count corrected

The cluster size includes nodes added with addNodesToKubernetesCluster,
but scale-down only counted CKS-created workers. With added nodes
present, a scale-down removed too few nodes or none at all
("No nodes to remove"), still recorded the new size, and later
scale-ups waited forever for a Ready count that could not match.

- Scale-down by size removes CKS-created workers until created + added
  workers equals the requested size.
- Every scale first checks the stored worker, control and etcd counts
  against the cluster's VMs. If they drifted it corrects them and
  recomputes cores and memory from each VM's offering, so clusters
  damaged by the old behaviour are fixed by the next scale.
  The API checks use the same count from the VMs. A size equal to the
  stored one (the UI always sends it) is treated as unchanged; any
  other size on a drifted cluster is rejected until it is corrected,
  as the caller's intent is ambiguous. Clusters whose control node
  flags were never set (created before 4.16) keep the stored counts.
- Reject a size or autoscaling minsize below the number of added
  nodes; scaling never removes them.
- Scale nodeids only takes CKS-created workers. Added, control and
  etcd nodes were skipped or removed without updating the counts.
  removeNodesFromKubernetesCluster removes added nodes.
- UI: no per-node delete button for added, control or etcd nodes;
  the VM list now reports iscontrolnode.
- Document that size and minsize count added nodes and which
  nodeids are valid.
- Unit tests for each case.

Signed-off-by: Brad House <bhouse@nexthop.ai>
@bhouse-nexthop
bhouse-nexthop force-pushed the cks-scale-size-added-nodes branch from 53c65e6 to e3187d1 Compare November 24, 2026 13:29
bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 24, 2026
Follow-up to "Repair CKS size drift; reject control and etcd nodes in
scale", for the next amendment of apache#14837.

When the stored node counts have drifted, the scale worker no longer
adds the missing nodes' share to the stored cores and memory. It
recomputes them from the cluster's VMs, each by its own service
offering, using the VM's details for a custom offering. Stored totals
that were already wrong are therefore corrected, not carried forward.
If the offering of any VM can't be read, the counts are still
corrected but the cores and memory are left as they are.

Signed-off-by: Brad House <bhouse@nexthop.ai>
bhouse-nexthop added a commit to bhouse-nexthop/cloudstack that referenced this pull request Nov 24, 2026
Follow-up to "Repair CKS size drift; reject control and etcd nodes in
scale", for the next amendment of apache#14837.

When the stored node counts have drifted, the scale worker no longer
adds the missing nodes' share to the stored cores and memory. It
recomputes them from the cluster's VMs, each by its own service
offering, using the VM's details for a custom offering. Stored totals
that were already wrong are therefore corrected, not carried forward.
If the offering of any VM can't be read, the counts are still
corrected but the cores and memory are left as they are.

Signed-off-by: Brad House <bhouse@nexthop.ai>
@bhouse-nexthop

Copy link
Copy Markdown
Collaborator Author

Follow-up on the drift repair, now that it recomputes capacity from the VMs. Tested on a main-based build (2583bb7, which has the same change as the 4.22 line): KVM, cluster with 1 control and 2 workers on a 2 vCPU/4096 MB offering, plus 2 added nodes, one on that offering and one on a custom offering at 2 vCPU/3072 MB. "Real" is the sum of cpunumber/memory over the cluster's VMs from listVirtualMachines.

step stored size, cores/memory real result
after add-node 4, 10/19456 10/19456 the custom-offering node counts at its own 2/3072
no drift, size 4 → 5 → 4 12/23552, then 10/19456 same each step moves exactly one worker (2/4096)
size only drifted by 1, cluster stopped, scale size=<stored> with an offering change (2/5120) 3 → 4, 10/19456 → 10/22528 10/22528 matches; no VM created or destroyed; added nodes keep their own offerings
size only drifted by 2, stopped, scale size=<stored> back to 2/4096 2 → 4, → 10/19456 10/19456 matches
size and capacity drifted together (size −1, 8/15360), stopped, scale size=<stored> to 2/5120 3 → 4, → 10/22528 10/22528 matches
running, size only drifted by 2, scale size=<real> 2 → 4, 10/22528 10/22528 matches, no VM change
no drift after the repairs, size 4 → 5 → 4 12/27648, then 10/22528 same each step moves exactly one worker (2/5120)

Each repair logs records 1 control, 0 etcd and N worker nodes but has 1, 0 and 4, correcting it. All 5 nodes were Ready at the end.

This branch has not been deployed

No deployments
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.

1 participant