Skip to content

filter irrelevant pods in pod controller #696

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
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pkg/epp/controller/pod_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import (
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/gateway-api-inference-extension/pkg/epp/datastore"
logutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
podutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/pod"
Expand Down Expand Up @@ -63,8 +65,28 @@ func (c *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
}

func (c *PodReconciler) SetupWithManager(mgr ctrl.Manager) error {
filter := predicate.Funcs{
CreateFunc: func(ce event.CreateEvent) bool {
pod := ce.Object.(*corev1.Pod)
return c.Datastore.PoolLabelsMatch(pod.GetLabels())
},
UpdateFunc: func(ue event.UpdateEvent) bool {
oldPod := ue.ObjectOld.(*corev1.Pod)
newPod := ue.ObjectNew.(*corev1.Pod)
return c.Datastore.PoolLabelsMatch(oldPod.GetLabels()) || c.Datastore.PoolLabelsMatch(newPod.GetLabels())
},
DeleteFunc: func(de event.DeleteEvent) bool {
pod := de.Object.(*corev1.Pod)
return c.Datastore.PoolLabelsMatch(pod.GetLabels())
},
GenericFunc: func(ge event.GenericEvent) bool {
pod := ge.Object.(*corev1.Pod)
return c.Datastore.PoolLabelsMatch(pod.GetLabels())
},
}
return ctrl.NewControllerManagedBy(mgr).
For(&corev1.Pod{}).
WithEventFilter(filter).
Complete(c)
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/epp/datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ func (ds *datastore) PoolHasSynced() bool {
func (ds *datastore) PoolLabelsMatch(podLabels map[string]string) bool {
ds.poolAndModelsMu.RLock()
defer ds.poolAndModelsMu.RUnlock()
if ds.pool == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's good to have without any relation to the predicate.
nit: you can use the internal ds function (same logic, just to be consistent with GetPool func)

if !ds.PoolHasSynced() {
	return false
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will cause deadlock if we use ds.PoolHasSynced because it also need to acquire poolAndModelsMu.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure? :)
Go mutex behavior around reentrant locks can be a bit nuanced.
see this example here https://github.com/kubernetes-sigs/gateway-api-inference-extension/blob/main/pkg/epp/datastore/datastore.go#L112-L117.
you can give it a try in your fork.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's safe not to acquire RWMutex twice in one function.
https://pkg.go.dev/sync#RWMutex

If any goroutine calls RWMutex.Lock while the lock is already held by one or more readers, concurrent calls to RWMutex.RLock will block until the writer has acquired (and released) the lock, to ensure that the lock eventually becomes available to the writer. Note that this prohibits recursive read-locking.

https://stackoverflow.com/questions/30547916/goroutine-blocks-when-calling-rwmutex-rlock-twice-after-an-rwmutex-unlock

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's definitely safe to keep current check.
was just a nice to have comment :)

return false
}
poolSelector := selectorFromInferencePoolSelector(ds.pool.Spec.Selector)
podSet := labels.Set(podLabels)
return poolSelector.Matches(podSet)
Expand Down