-
Notifications
You must be signed in to change notification settings - Fork 1.2k
🐛Implement priorityqueue as default on handlers if using priorityqueue interface #3111
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
Merged
k8s-ci-robot
merged 3 commits into
kubernetes-sigs:main
from
troy0820:troy0820/priority-queue-on-enqueue-handler
Mar 10, 2025
+171
−30
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,7 +65,7 @@ type EventHandler = TypedEventHandler[client.Object, reconcile.Request] | |
// | ||
// Unless you are implementing your own TypedEventHandler, you can ignore the functions on the TypedEventHandler interface. | ||
// Most users shouldn't need to implement their own TypedEventHandler. | ||
// | ||
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. This change breaks godoc rendering of TypedEventHandler. Now only l.69 shows up |
||
|
||
// TypedEventHandler is experimental and subject to future change. | ||
type TypedEventHandler[object any, request comparable] interface { | ||
// Create is called in response to a create event - e.g. Pod Creation. | ||
|
@@ -149,33 +149,15 @@ func WithLowPriorityWhenUnchanged[object client.Object, request comparable](u Ty | |
u.Create(ctx, tce, workqueueWithCustomAddFunc[request]{ | ||
TypedRateLimitingInterface: trli, | ||
addFunc: func(item request, q workqueue.TypedRateLimitingInterface[request]) { | ||
priorityQueue, isPriorityQueue := q.(priorityqueue.PriorityQueue[request]) | ||
if !isPriorityQueue { | ||
q.Add(item) | ||
return | ||
} | ||
var priority int | ||
if isObjectUnchanged(tce) { | ||
priority = LowPriority | ||
} | ||
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: priority}, item) | ||
addToQueueCreate(q, tce, item) | ||
}, | ||
}) | ||
}, | ||
UpdateFunc: func(ctx context.Context, tue event.TypedUpdateEvent[object], trli workqueue.TypedRateLimitingInterface[request]) { | ||
u.Update(ctx, tue, workqueueWithCustomAddFunc[request]{ | ||
TypedRateLimitingInterface: trli, | ||
addFunc: func(item request, q workqueue.TypedRateLimitingInterface[request]) { | ||
priorityQueue, isPriorityQueue := q.(priorityqueue.PriorityQueue[request]) | ||
if !isPriorityQueue { | ||
q.Add(item) | ||
return | ||
} | ||
var priority int | ||
if tue.ObjectOld.GetResourceVersion() == tue.ObjectNew.GetResourceVersion() { | ||
priority = LowPriority | ||
} | ||
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: priority}, item) | ||
addToQueueUpdate(q, tue, item) | ||
}, | ||
}) | ||
}, | ||
|
@@ -199,3 +181,35 @@ func (w workqueueWithCustomAddFunc[request]) Add(item request) { | |
func isObjectUnchanged[object client.Object](e event.TypedCreateEvent[object]) bool { | ||
return e.Object.GetCreationTimestamp().Time.Before(time.Now().Add(-time.Minute)) | ||
} | ||
|
||
// addToQueueCreate adds the reconcile.Request to the priorityqueue in the handler | ||
// for Create requests if and only if the workqueue being used is of type priorityqueue.PriorityQueue[reconcile.Request] | ||
func addToQueueCreate[T client.Object, request comparable](q workqueue.TypedRateLimitingInterface[request], evt event.TypedCreateEvent[T], item request) { | ||
priorityQueue, isPriorityQueue := q.(priorityqueue.PriorityQueue[request]) | ||
if !isPriorityQueue { | ||
q.Add(item) | ||
return | ||
} | ||
|
||
var priority int | ||
if isObjectUnchanged(evt) { | ||
priority = LowPriority | ||
} | ||
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: priority}, item) | ||
} | ||
|
||
// addToQueueUpdate adds the reconcile.Request to the priorityqueue in the handler | ||
// for Update requests if and only if the workqueue being used is of type priorityqueue.PriorityQueue[reconcile.Request] | ||
func addToQueueUpdate[T client.Object, request comparable](q workqueue.TypedRateLimitingInterface[request], evt event.TypedUpdateEvent[T], item request) { | ||
priorityQueue, isPriorityQueue := q.(priorityqueue.PriorityQueue[request]) | ||
if !isPriorityQueue { | ||
q.Add(item) | ||
return | ||
} | ||
|
||
var priority int | ||
if evt.ObjectOld.GetResourceVersion() == evt.ObjectNew.GetResourceVersion() { | ||
priority = LowPriority | ||
} | ||
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: priority}, item) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could we also do the same for
Funcs
? That is the only remaining handlerThere 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.
Not sure if this is how you wanted this done but any feedback is appreciated.
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.
I think in
Funcs
we have to do the same dance as inTypedEnqueueRequestForObject
because we export the type and not a constructor, otherwise the original goal ofget this by default
won't be achievedThere 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.
Makes sense, I did what we did with
TypedEnqueueRequestForObject
because I can't add methods to theFuncs
type. So I did the same thing we did with this.