-
Notifications
You must be signed in to change notification settings - Fork 456
[#5310] workload controller delete refactor #7585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
07e9f65
6728e2b
71b8511
636383f
b9466d5
5d3187f
0b08d3d
9a97939
d7bedee
02c34b8
b231d05
e05dd5e
122318e
93e6b50
5dc4f0a
4f9e8e4
26d469b
071dd51
db6df11
5909cb3
a416c2e
adca57b
ab74ebe
3e01953
e0a733c
02af80c
aa299d3
ab990ab
9a4452a
33ceb56
c7ef18f
4d627e6
72f0e69
ecc5785
1b6910c
b4973f7
7c1f588
7b78828
361f523
1de8dba
54091d1
87d985b
5062544
7491d35
430f1db
17a7ec3
fddf1b7
b177dcc
cfe148a
9bba991
a24556e
5891828
d0338b2
d81e75b
7c4de98
cbc270b
56bdbcc
b4edcb9
ab8b367
4f8172d
76a89e0
8e4abe2
fc3c1c4
4ce6906
6121826
0423898
8c973f5
83effda
8970bdb
825c4e3
5225f29
f483b4f
0827c45
d96094c
cedc241
ef12e72
4aa6d05
c7d7ef3
1cef4e8
c20b6ac
5a83c45
07e442e
f134700
a1fb5ae
05ba58b
b205f5d
08319fa
933a228
490d40d
ff7236f
a806a48
0e7a10d
bd6e6fb
e3fe657
a77639e
1edfeaf
2544a73
daaa5c2
2c0904f
62db2e8
50c1fb0
c759957
f8df1e4
e1a8d18
fcf5ea3
e8c47b8
22e249f
25f46f8
1e9c2cd
89f6f27
c00e728
ae76730
ec1b73d
15a6257
3b81164
e2ecd6a
84b835c
71cd4e1
7d2f0ff
f513d78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -42,6 +42,7 @@ import ( | |||||
| "sigs.k8s.io/controller-runtime/pkg/builder" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/controller" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/event" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/handler" | ||||||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||||||
|
|
@@ -158,18 +159,39 @@ func (r *WorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c | |||||
| return ctrl.Result{}, client.IgnoreNotFound(err) | ||||||
| } | ||||||
|
|
||||||
| log := ctrl.LoggerFrom(ctx) | ||||||
| status := workload.Status(&wl) | ||||||
| log := ctrl.LoggerFrom(ctx).WithValues("workload", klog.KObj(&wl), "queue", wl.Spec.QueueName, "status", status) | ||||||
| log.V(2).Info("Reconcile Workload") | ||||||
|
|
||||||
| if len(wl.OwnerReferences) == 0 && !wl.DeletionTimestamp.IsZero() { | ||||||
| // manual deletion triggered by the user | ||||||
| err := workload.RemoveFinalizer(ctx, r.client, &wl) | ||||||
| return ctrl.Result{}, client.IgnoreNotFound(err) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The old code wrapped |
||||||
| if !wl.DeletionTimestamp.IsZero() { | ||||||
| log = log.WithValues("deletionTimestamp", wl.DeletionTimestamp) | ||||||
| log.Info("Attemtping to finalize workload.") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| switch { | ||||||
| case controllerutil.ContainsFinalizer(&wl, kueue.ResourceInUseFinalizerName): | ||||||
| { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Braces are not necessary in the |
||||||
| log.Info("Manual deletion by a user detected.") | ||||||
| if len(wl.OwnerReferences) == 0 { | ||||||
| return ctrl.Result{}, r.finalize(ctx, &wl, log) | ||||||
| } else { | ||||||
| log.Info("Uable to finalize: workload still has owners. Proceeding with reconcile.", "owners", wl.OwnerReferences) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
| } | ||||||
| case controllerutil.ContainsFinalizer(&wl, kueue.SafeDeleteFinalizerName): | ||||||
| { | ||||||
| return ctrl.Result{}, r.finalize(ctx, &wl, log) | ||||||
| } | ||||||
| default: | ||||||
| { | ||||||
| log.Info("Unknown finalizer(s) present. Proceeding with reconcile.") | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| finishedCond := apimeta.FindStatusCondition(wl.Status.Conditions, kueue.WorkloadFinished) | ||||||
| if finishedCond != nil && finishedCond.Status == metav1.ConditionTrue { | ||||||
| if !features.Enabled(features.ObjectRetentionPolicies) || r.workloadRetention == nil || r.workloadRetention.afterFinished == nil { | ||||||
| log.Info("Unable to determine workload retention scheme.") | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logs in this function seem pretty verbose, shouldn't the level of those logs be higher? Here you are using |
||||||
| return ctrl.Result{}, nil | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -472,6 +494,38 @@ func (r *WorkloadReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c | |||||
| return ctrl.Result{}, nil | ||||||
| } | ||||||
|
|
||||||
| func (r *WorkloadReconciler) finalize(ctx context.Context, wl *kueue.Workload, log logr.Logger) error { | ||||||
| log.V(2).Info("Finalizing workload.") | ||||||
| defer r.notifyWatchers(wl, nil) | ||||||
|
|
||||||
| if workload.HasQuotaReservation(wl) { | ||||||
| var err error = nil | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need
Suggested change
|
||||||
| r.queues.QueueAssociatedInadmissibleWorkloadsAfter(ctx, wl, func() { | ||||||
| err = r.cache.DeleteWorkload(log, wl) | ||||||
| }) | ||||||
| if err != nil { | ||||||
| log.V(2).Error(err, "Failed to delete workload from cache.") | ||||||
| return err | ||||||
| } | ||||||
| } else { | ||||||
| r.queues.QueueAssociatedInadmissibleWorkloadsAfter(ctx, wl, func() { | ||||||
| if err := r.cache.DeleteWorkload(log, wl); err != nil { | ||||||
| log.V(2).Info("Failed to delete workload from cache.", "Error", err, "Note", "this may be intended behavior") | ||||||
| } | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| r.queues.DeleteWorkload(wl) | ||||||
|
|
||||||
| controllerutil.RemoveFinalizer(wl, kueue.ResourceInUseFinalizerName) | ||||||
| controllerutil.RemoveFinalizer(wl, kueue.SafeDeleteFinalizerName) | ||||||
| if err := r.client.Update(ctx, wl); err != nil { | ||||||
| return err | ||||||
| } | ||||||
| r.recorder.Eventf(wl, corev1.EventTypeNormal, "Finalized", "Workload %s has been finalized", workload.Key(wl)) | ||||||
| return nil | ||||||
| } | ||||||
|
|
||||||
| // isDisabledRequeuedByClusterQueueStopped returns true if the workload is unset requeued by cluster queue stopped. | ||||||
| func isDisabledRequeuedByClusterQueueStopped(w *kueue.Workload) bool { | ||||||
| return isDisabledRequeuedByReason(w, kueue.WorkloadEvictedByClusterQueueStopped) | ||||||
|
|
@@ -757,36 +811,12 @@ func (r *WorkloadReconciler) Create(e event.TypedCreateEvent[*kueue.Workload]) b | |||||
| } | ||||||
|
|
||||||
| func (r *WorkloadReconciler) Delete(e event.TypedDeleteEvent[*kueue.Workload]) bool { | ||||||
| defer r.notifyWatchers(e.Object, nil) | ||||||
| status := "unknown" | ||||||
| if !e.DeleteStateUnknown { | ||||||
| status = workload.Status(e.Object) | ||||||
| } | ||||||
| log := r.log.WithValues("workload", klog.KObj(e.Object), "queue", e.Object.Spec.QueueName, "status", status) | ||||||
| log.V(2).Info("Workload delete event") | ||||||
| ctx := ctrl.LoggerInto(context.Background(), log) | ||||||
|
|
||||||
| // When assigning a clusterQueue to a workload, we assume it in the cache. If | ||||||
| // the state is unknown, the workload could have been assumed, and we need | ||||||
| // to clear it from the cache. | ||||||
| if workload.HasQuotaReservation(e.Object) || e.DeleteStateUnknown { | ||||||
| // trigger the move of associated inadmissibleWorkloads if required. | ||||||
| r.queues.QueueAssociatedInadmissibleWorkloadsAfter(ctx, e.Object, func() { | ||||||
| // Delete the workload from cache while holding the queues lock | ||||||
| // to guarantee that requeued workloads are taken into account before | ||||||
| // the next scheduling cycle. | ||||||
| if err := r.cache.DeleteWorkload(log, e.Object); err != nil { | ||||||
| if !e.DeleteStateUnknown { | ||||||
| log.Error(err, "Failed to delete workload from cache") | ||||||
| } | ||||||
| } | ||||||
| }) | ||||||
| log := r.log.WithValues("workload", klog.KObj(e.Object), "queue", e.Object.Spec.QueueName, "status", workload.Status(e.Object)) | ||||||
| if e.DeleteStateUnknown { | ||||||
| log.V(2).Info("Workload delete event; delete status unknown") | ||||||
| } else { | ||||||
| log.V(2).Info("Workload delete event") | ||||||
| } | ||||||
|
|
||||||
| // Even if the state is unknown, the last cached state tells us whether the | ||||||
| // workload was in the queues and should be cleared from them. | ||||||
| r.queues.DeleteWorkload(e.Object) | ||||||
|
|
||||||
| return true | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks incorrectly formatted (misaligned =)