@@ -819,6 +819,18 @@ func SetEvictedCondition(w *kueue.Workload, now time.Time, reason string, messag
819819 return apimeta .SetStatusCondition (& w .Status .Conditions , condition )
820820}
821821
822+ func SetFinishedCondition (w * kueue.Workload , now time.Time , reason string , message string ) bool {
823+ condition := metav1.Condition {
824+ Type : kueue .WorkloadFinished ,
825+ Status : metav1 .ConditionTrue ,
826+ LastTransitionTime : metav1 .NewTime (now ),
827+ Reason : reason ,
828+ Message : api .TruncateConditionMessage (message ),
829+ ObservedGeneration : w .Generation ,
830+ }
831+ return apimeta .SetStatusCondition (& w .Status .Conditions , condition )
832+ }
833+
822834// PropagateResourceRequests synchronizes w.Status.ResourceRequests to
823835// with info.TotalRequests if the feature gate is enabled and returns true if w was updated
824836func PropagateResourceRequests (w * kueue.Workload , info * Info ) bool {
@@ -1301,6 +1313,58 @@ func Evict(ctx context.Context, c client.Client, recorder record.EventRecorder,
13011313 return nil
13021314}
13031315
1316+ type FinishOption func (* FinishOptions )
1317+
1318+ type FinishOptions struct {
1319+ UsePatch bool
1320+ ForceOwnership bool
1321+ }
1322+
1323+ func DefaultFinishOptions () * FinishOptions {
1324+ return & FinishOptions {
1325+ UsePatch : false ,
1326+ ForceOwnership : false ,
1327+ }
1328+ }
1329+
1330+ func WithUsePatch () FinishOption {
1331+ return func (o * FinishOptions ) {
1332+ o .UsePatch = true
1333+ }
1334+ }
1335+
1336+ func WithForceOwnership () FinishOption {
1337+ return func (o * FinishOptions ) {
1338+ o .ForceOwnership = true
1339+ }
1340+ }
1341+
1342+ func Finish (ctx context.Context , c client.Client , wl * kueue.Workload , reason , msg , managerPrefix string , clock clock.Clock , options ... FinishOption ) error {
1343+ optsFinish := DefaultFinishOptions ()
1344+ for _ , opt := range options {
1345+ opt (optsFinish )
1346+ }
1347+ usePatch := optsFinish .UsePatch
1348+ forceOwnership := optsFinish .ForceOwnership
1349+
1350+ if features .Enabled (features .WorkloadRequestUseMergePatch ) || usePatch {
1351+ return clientutil .PatchStatus (ctx , c , wl , func () (client.Object , bool , error ) {
1352+ update := SetFinishedCondition (wl , clock .Now (), reason , msg )
1353+ return wl , update , nil
1354+ })
1355+ } else {
1356+ newWl := BaseSSAWorkload (wl , false )
1357+ SetFinishedCondition (newWl , clock .Now (), reason , msg )
1358+
1359+ fieldOwner := client .FieldOwner (managerPrefix + "-" + kueue .WorkloadFinished )
1360+ applyOpts := []client.SubResourcePatchOption {fieldOwner }
1361+ if forceOwnership {
1362+ applyOpts = append (applyOpts , client .ForceOwnership )
1363+ }
1364+ return c .Status ().Patch (ctx , newWl , client .Apply , applyOpts ... )
1365+ }
1366+ }
1367+
13041368func PriorityClassName (wl * kueue.Workload ) string {
13051369 if wl .Spec .PriorityClassRef != nil {
13061370 return wl .Spec .PriorityClassRef .Name
0 commit comments