-
Notifications
You must be signed in to change notification settings - Fork 1.2k
✨ WithLowPriorityWhenUnchanged: Set Priority for all add methods #3290
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
Open
alvaroaleman
wants to merge
2
commits into
kubernetes-sigs:main
Choose a base branch
from
alvaroaleman:addafter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+196
−58
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ package handler | |
import ( | ||
"context" | ||
"reflect" | ||
"time" | ||
|
||
"k8s.io/client-go/util/workqueue" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
@@ -126,20 +127,14 @@ func (h TypedFuncs[object, request]) Create(ctx context.Context, e event.TypedCr | |
h.CreateFunc(ctx, e, q) | ||
return | ||
} | ||
wq := workqueueWithCustomAddFunc[request]{ | ||
TypedRateLimitingInterface: q, | ||
|
||
wq := workqueueWithDefaultPriority[request]{ | ||
// We already know that we have a priority queue, that event.Object implements | ||
// client.Object and that its not nil | ||
addFunc: func(item request, q workqueue.TypedRateLimitingInterface[request]) { | ||
var priority int | ||
if e.IsInInitialList { | ||
priority = LowPriority | ||
} | ||
q.(priorityqueue.PriorityQueue[request]).AddWithOpts( | ||
priorityqueue.AddOpts{Priority: priority}, | ||
item, | ||
) | ||
}, | ||
PriorityQueue: q.(priorityqueue.PriorityQueue[request]), | ||
} | ||
if e.IsInInitialList { | ||
wq.priority = LowPriority | ||
} | ||
h.CreateFunc(ctx, e, wq) | ||
} | ||
|
@@ -160,20 +155,13 @@ func (h TypedFuncs[object, request]) Update(ctx context.Context, e event.TypedUp | |
return | ||
} | ||
|
||
wq := workqueueWithCustomAddFunc[request]{ | ||
TypedRateLimitingInterface: q, | ||
wq := workqueueWithDefaultPriority[request]{ | ||
// We already know that we have a priority queue, that event.ObjectOld and ObjectNew implement | ||
// client.Object and that they are not nil | ||
addFunc: func(item request, q workqueue.TypedRateLimitingInterface[request]) { | ||
var priority int | ||
if any(e.ObjectOld).(client.Object).GetResourceVersion() == any(e.ObjectNew).(client.Object).GetResourceVersion() { | ||
priority = LowPriority | ||
} | ||
q.(priorityqueue.PriorityQueue[request]).AddWithOpts( | ||
priorityqueue.AddOpts{Priority: priority}, | ||
item, | ||
) | ||
}, | ||
PriorityQueue: q.(priorityqueue.PriorityQueue[request]), | ||
} | ||
if any(e.ObjectOld).(client.Object).GetResourceVersion() == any(e.ObjectNew).(client.Object).GetResourceVersion() { | ||
wq.priority = LowPriority | ||
} | ||
h.UpdateFunc(ctx, e, wq) | ||
} | ||
|
@@ -201,13 +189,28 @@ func WithLowPriorityWhenUnchanged[object client.Object, request comparable](u Ty | |
} | ||
} | ||
|
||
type workqueueWithCustomAddFunc[request comparable] struct { | ||
workqueue.TypedRateLimitingInterface[request] | ||
addFunc func(item request, q workqueue.TypedRateLimitingInterface[request]) | ||
type workqueueWithDefaultPriority[request comparable] struct { | ||
priorityqueue.PriorityQueue[request] | ||
priority int | ||
} | ||
|
||
func (w workqueueWithDefaultPriority[request]) Add(item request) { | ||
w.PriorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: &w.priority}, item) | ||
} | ||
|
||
func (w workqueueWithCustomAddFunc[request]) Add(item request) { | ||
w.addFunc(item, w.TypedRateLimitingInterface) | ||
func (w workqueueWithDefaultPriority[request]) AddAfter(item request, after time.Duration) { | ||
w.PriorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: &w.priority, After: after}, item) | ||
} | ||
|
||
func (w workqueueWithDefaultPriority[request]) AddRateLimited(item request) { | ||
w.PriorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: &w.priority, RateLimited: true}, item) | ||
} | ||
|
||
func (w workqueueWithDefaultPriority[request]) AddWithOpts(o priorityqueue.AddOpts, items ...request) { | ||
if o.Priority == nil { | ||
o.Priority = &w.priority | ||
} | ||
w.PriorityQueue.AddWithOpts(o, items...) | ||
} | ||
|
||
// addToQueueCreate adds the reconcile.Request to the priorityqueue in the handler | ||
|
@@ -223,7 +226,7 @@ func addToQueueCreate[T client.Object, request comparable](q workqueue.TypedRate | |
if evt.IsInInitialList { | ||
priority = LowPriority | ||
} | ||
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: priority}, item) | ||
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: &priority}, item) | ||
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. Similar here. Wondering if we should just play it safe var priority *int
if evt.IsInInitialList {
priority = ptr.To(LowPriority)
}
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: priority}, item) (same in l.245) |
||
} | ||
|
||
// addToQueueUpdate adds the reconcile.Request to the priorityqueue in the handler | ||
|
@@ -239,5 +242,5 @@ func addToQueueUpdate[T client.Object, request comparable](q workqueue.TypedRate | |
if evt.ObjectOld.GetResourceVersion() == evt.ObjectNew.GetResourceVersion() { | ||
priority = LowPriority | ||
} | ||
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: priority}, item) | ||
priorityQueue.AddWithOpts(priorityqueue.AddOpts{Priority: &priority}, item) | ||
} |
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.
I'm wondering if we should also make this a pointer
So that we only call AddWithOpts with a priority if it is actually set (and not &0 if unset)
I know that currently for our priority queue implementation it doesn't matter (as far as I can tell), still wondering if it would be better