@@ -21,6 +21,7 @@ import (
2121 "context"
2222 "fmt"
2323 "slices"
24+ "strings"
2425 "time"
2526
2627 "github.com/go-logr/logr"
@@ -170,6 +171,16 @@ func (r *WorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
170171
171172 finishedCond := apimeta .FindStatusCondition (wl .Status .Conditions , kueue .WorkloadFinished )
172173 if finishedCond != nil && finishedCond .Status == metav1 .ConditionTrue {
174+ // Unset quota reservation for finished workloads
175+ if workload .HasQuotaReservation (& wl ) {
176+ log .V (2 ).Info ("Unsetting quota reservation for finished workload" )
177+ if err := workload .PatchAdmissionStatus (ctx , r .client , & wl , r .clock , func () (* kueue.Workload , bool , error ) {
178+ return & wl , workload .UnsetQuotaReservationWithCondition (& wl , kueue .WorkloadFinished , "Workload finished" , r .clock .Now ()), nil
179+ }); err != nil {
180+ return ctrl.Result {}, fmt .Errorf ("failed to unset quota reservation for finished workload: %w" , err )
181+ }
182+ }
183+
173184 if ! features .Enabled (features .ObjectRetentionPolicies ) || r .workloadRetention == nil || r .workloadRetention .afterFinished == nil {
174185 return ctrl.Result {}, nil
175186 }
@@ -226,6 +237,7 @@ func (r *WorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
226237 requeuedCond := apimeta .FindStatusCondition (wl .Status .Conditions , kueue .WorkloadRequeued )
227238
228239 var conditionsCleared bool
240+ //if requeuedCond != nil && requeuedCond.Status == metav1.ConditionFalse && requeuedCond.Reason == kueue.WorkloadInadmissible {
229241 if quotaReservedCond != nil && quotaReservedCond .Status == metav1 .ConditionFalse {
230242 apimeta .RemoveStatusCondition (& wl .Status .Conditions , kueue .WorkloadQuotaReserved )
231243 conditionsCleared = true
@@ -321,6 +333,18 @@ func (r *WorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
321333 if updated {
322334 if evicted {
323335 if err := workload .Evict (ctx , r .client , r .recorder , wlOrig , reason , message , underlyingCause , r .clock , workload .WithCustomPrepare (func () (* kueue.Workload , error ) {
336+ // Unset quota reservation for deactivated workloads
337+ if reason == kueue .WorkloadDeactivated || strings .HasPrefix (reason , kueue .WorkloadDeactivated ) {
338+ if workload .HasQuotaReservation (& wl ) {
339+ quotaReason := reason
340+ if underlyingCause != "" {
341+ quotaReason = workload .ReasonWithCause (reason , string (underlyingCause ))
342+ }
343+ workload .UnsetQuotaReservationWithCondition (& wl , quotaReason , message , r .clock .Now ())
344+ // Set the WorkloadRequeued condition to false for deactivated workloads
345+ workload .SetRequeuedCondition (& wl , quotaReason , message , false )
346+ }
347+ }
324348 return & wl , nil
325349 })); err != nil {
326350 if ! apierrors .IsNotFound (err ) {
0 commit comments