From 3568ba559c80bdc1cff4ab2a797f7edaa25c51b4 Mon Sep 17 00:00:00 2001 From: Marcel <156897072+mblos@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:03:24 +0100 Subject: [PATCH] fix: host oversubscription check fires false alarms after capacity refactoring Signed-off-by: Marcel <156897072+mblos@users.noreply.github.com> --- .../host_oversubscription_controller.go | 20 ++++++++++++--- .../host_oversubscription_controller_test.go | 25 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/internal/scheduling/reservations/commitments/host_oversubscription_controller.go b/internal/scheduling/reservations/commitments/host_oversubscription_controller.go index 34d7667f2..afc3e66bb 100644 --- a/internal/scheduling/reservations/commitments/host_oversubscription_controller.go +++ b/internal/scheduling/reservations/commitments/host_oversubscription_controller.go @@ -269,13 +269,25 @@ func (r *HostOversubscriptionController) SetupWithManager(mgr ctrl.Manager, mcl // the host is over-subscribed. Returns nil if capacity data is unavailable, empty map if no violation. func computeViolations(allReservations []v1alpha1.Reservation, hv hv1.Hypervisor) map[hv1.ResourceName]resource.Quantity { rep := capacity.NewHostUsageReport(hv, allReservations) - if rep.Oversubscribed == nil { + if rep.Total == nil { return nil // no capacity data } + // Only count types that represent real demand on this host's capacity. + // ReservationTypeFailoverHost is excluded: it blocks Spec.Resources = full host capacity + // for evacuation and is policed by a dedicated filter — counting it here would make every + // host with running VMs appear oversubscribed. + reserved := rep.ReservedTotal( + v1alpha1.ReservationTypeCommittedResource, + v1alpha1.ReservationTypeFailover, + v1alpha1.ReservationTypeInFlight, + ) violations := make(map[hv1.ResourceName]resource.Quantity) - for rn, excess := range rep.Oversubscribed { - if excess.Sign() > 0 { - violations[rn] = excess + for rn, total := range rep.Total { + demand := rep.Used[rn].DeepCopy() + demand.Add(reserved[rn]) + if demand.Cmp(total) > 0 { + demand.Sub(total) + violations[rn] = demand } } return violations diff --git a/internal/scheduling/reservations/commitments/host_oversubscription_controller_test.go b/internal/scheduling/reservations/commitments/host_oversubscription_controller_test.go index 12bc8fc7b..54b0125e1 100644 --- a/internal/scheduling/reservations/commitments/host_oversubscription_controller_test.go +++ b/internal/scheduling/reservations/commitments/host_oversubscription_controller_test.go @@ -101,6 +101,31 @@ func TestComputeViolations(t *testing.T) { Status: v1alpha1.ReservationStatus{Host: "host-1"}, }}, }, + { + // Regression: after the pkg/capacity refactor (31b2fe82), FailoverHost reservations + // were counted at full Spec.Resources (= entire host) instead of zero. + // With VMs running: Available = Total − Used − Total = −Used → false oversubscription. + name: "failover host reservation with running VMs does not cause false oversubscription", + hv: hv1.Hypervisor{ + ObjectMeta: metav1.ObjectMeta{Name: "host-1"}, + Status: hv1.HypervisorStatus{ + EffectiveCapacity: map[hv1.ResourceName]resource.Quantity{ + hv1.ResourceMemory: testGiB(512), + }, + Allocation: map[hv1.ResourceName]resource.Quantity{ + hv1.ResourceMemory: testGiB(256), + }, + }, + }, + slots: []v1alpha1.Reservation{{ + Spec: v1alpha1.ReservationSpec{ + Type: v1alpha1.ReservationTypeFailoverHost, + TargetHost: "host-1", + Resources: map[hv1.ResourceName]resource.Quantity{hv1.ResourceMemory: testGiB(512)}, + }, + Status: v1alpha1.ReservationStatus{Host: "host-1"}, + }}, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {