[Feature]: Use the cached client in gpuPodSpecFilter instead of the API reader
Motivation
cmd/gpu-operator/main.go builds the upgrade pod-deletion filter with gpuPodSpecFilter(ctx, mgr.GetAPIReader()). For every candidate pod with ResourceClaims, controllers.PodHasNVIDIAGPUClaim then does a live GET per claim against the API server, on every upgrade pass and for every node being upgraded.
The node-labeling controller already calls the same helper with the manager's cached client (nodeHasDRAClaimPods), so ResourceClaims are already served from the cache.
Proposal
Pass mgr.GetClient() (or a dedicated cached reader) to gpuPodSpecFilter. If freshness is a concern for a claim allocated moments ago, fall back to the API reader only on cache miss.
@CloneOfAlex
Could you provide more details about the problem you encountered and your proposed solution?
Thanks @francisguillier. Digging into this for the details turned up two things: the change I proposed would be unsafe, and the code I cited as precedent looks like it has a bug of its own. Details below.
What the code does today
cmd/gpu-operator/main.go:195 builds the upgrade pod-deletion filter with gpuPodSpecFilter(ctx, mgr.GetAPIReader()). For a running/pending pod that has no nvidia.com/gpu* / nvidia.com/mig-* extended resources, the filter calls controllers.PodHasNVIDIAGPUClaim, which does a live GET per ResourceClaim (nodelabeling_controller.go:435).
The cost is bounded. It only runs on nodes in the pod-deletion step of a driver upgrade, and only for pods that have ResourceClaims and no GPU extended resources. So the performance motivation in this issue is modest.
Why switching to mgr.GetClient() is unsafe
The manager's cache is scoped to two namespaces:
cacheOptions := cache.Options{
DefaultNamespaces: map[string]cache.Config{
operatorNamespace: {},
openshiftNamespace: {},
},
}
For a namespaced object outside those namespaces, the cached client's Get returns unable to get: <key> because of unknown namespace for the cache (controller-runtime/pkg/cache/multi_namespace_cache.go:244). PodHasNVIDIAGPUClaim fails closed on any lookup error and treats the pod as a GPU pod (nodelabeling_controller.go:436-438).
Workload pods almost always live in user namespaces. With the cached client, every user pod holding any ResourceClaim (a NIC claim, another vendor's device) would be classified as a GPU pod and evicted during a driver upgrade. The API reader is what makes this filter correct today.
Tests against current main (the second one injects the exact error the namespace-scoped cache returns):
TestPodHasNVIDIAGPUClaimWithAPIReader: user pod with a non-NVIDIA claim →false(correct)TestPodHasNVIDIAGPUClaimWithNamespaceScopedCache: same pod →true(would be evicted)
controllers/pod_claim_reader_test.go
package controllers
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
resourcev1 "k8s.io/api/resource/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
)
// A workload pod in a user namespace whose claim is allocated by some other DRA
// driver: it is not a GPU pod and must not be evicted by the driver upgrade.
func userPodWithNonNVIDIAClaim() (*corev1.Pod, *resourcev1.ResourceClaim) {
claim := &resourcev1.ResourceClaim{
ObjectMeta: metav1.ObjectMeta{Name: "nic-claim", Namespace: "ml-team"},
Status: resourcev1.ResourceClaimStatus{
Allocation: &resourcev1.AllocationResult{
Devices: resourcev1.DeviceAllocationResult{
Results: []resourcev1.DeviceRequestAllocationResult{
{Request: "nic", Driver: "nic.example.com", Pool: "p", Device: "d0"},
},
},
},
},
}
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{Name: "trainer", Namespace: "ml-team"},
Spec: corev1.PodSpec{
ResourceClaims: []corev1.PodResourceClaim{{Name: "nic", ResourceClaimName: ptr.To("nic-claim")}},
},
Status: corev1.PodStatus{Phase: corev1.PodRunning},
}
return pod, claim
}
// With the API reader (what gpuPodSpecFilter uses today), the claim resolves and
// the pod is correctly classified as non-GPU.
func TestPodHasNVIDIAGPUClaimWithAPIReader(t *testing.T) {
scheme := runtime.NewScheme()
require.NoError(t, resourcev1.AddToScheme(scheme))
pod, claim := userPodWithNonNVIDIAClaim()
reader := fake.NewClientBuilder().WithScheme(scheme).WithObjects(claim).Build()
assert.False(t, PodHasNVIDIAGPUClaim(context.Background(), reader, pod, false))
}
// The manager's cache is scoped to DefaultNamespaces {operator ns, openshift ns}
// (cmd/gpu-operator/main.go). Get for an object in any other namespace fails with
// this error (controller-runtime cache/multi_namespace_cache.go). PodHasNVIDIAGPUClaim
// treats a failed lookup as "GPU pod", so switching gpuPodSpecFilter to the cached
// client would mark every user pod with a ResourceClaim for deletion.
func TestPodHasNVIDIAGPUClaimWithNamespaceScopedCache(t *testing.T) {
scheme := runtime.NewScheme()
require.NoError(t, resourcev1.AddToScheme(scheme))
pod, claim := userPodWithNonNVIDIAClaim()
cachedClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(claim).
WithInterceptorFuncs(interceptor.Funcs{
Get: func(_ context.Context, _ client.WithWatch, key client.ObjectKey, _ client.Object, _ ...client.GetOption) error {
// Exact error returned by multiNamespaceCache.Get for an uncached namespace.
return fmt.Errorf("unable to get: %v because of unknown namespace for the cache", key)
},
}).Build()
assert.True(t, PodHasNVIDIAGPUClaim(context.Background(), cachedClient, pod, false),
"non-GPU pod is misclassified as a GPU pod and would be evicted during driver upgrade")
}
The precedent I cited looks like a bug
I pointed to nodeHasDRAClaimPods (nodelabeling_controller.go:461) as already using the cached client. It does, and that's the problem. Its doc comment says it should detect any pod on the node still holding an NVIDIA DRA claim, because unpreparing those claims requires the kubelet plugin. But:
List(..., MatchingFields{podNodeNameIndexKey: nodeName})with no namespace goes throughmultiNamespaceCache.List, which only aggregates the configured namespaces (multi_namespace_cache.go:289). Workload pods in user namespaces are invisible to it.PodHasNVIDIAGPUClaim(ctx, nlc.client, …)would also fail for those claims, but those pods never make it into the list in the first place.
So the check effectively covers only operator/OpenShift-namespace pods. The DRA kubelet-plugin deploy label could be removed while user workloads on the node still hold prepared NVIDIA claims. This is from code reading; I haven't reproduced it on a cluster. I'll open a separate bug for it so it gets tracked on its own.
Suggested resolution
- This issue: I'd close it as "won't fix as proposed". Keeping
mgr.GetAPIReader()ingpuPodSpecFilteris the correct choice given the cache scoping. - If live GETs here ever matter: the safe route is a cluster-wide cache entry for just these kinds, e.g.
ByObject: {&corev1.Pod{}: {Namespaces: {cache.AllNamespaces: {}}}, &resourcev1.ResourceClaim{}: {…same…}}. The ClusterRole already grantsget/list/watchonpodsandresourceclaimscluster-wide, so no RBAC change is needed. The cost is holding every Pod and ResourceClaim in the cluster in operator memory, which is significant on large clusters. That same change would also fix the node-labeling gap above, so it may be worth evaluating in the context of that bug rather than this one.
Sorry for the noise on the original proposal. Happy to send the node-labeling fix (or the tests above) as a PR once the approach is agreed.
@CloneOfAlex
I’m not sure I understand the deployment problem you’re trying to solve. Could you please provide:
- Issue summary: What is happening, and what behavior do you expect?
- Steps to reproduce: Include the relevant environment details and configuration.
- Proposed fix: Explain how the fix addresses the problem. Without these details, we won’t be able to assess the issue and will close it. Thanks
@francisguillier same here, there isn't a deployment problem. The change I proposed would actually be unsafe: the namespace-scoped cache can't read ResourceClaims in user namespaces, so user pods holding non-NVIDIA claims would be evicted during driver upgrades. The current mgr.GetAPIReader() is correct. Closing as won't-fix.
The related node-labeling gap (nodeHasDRAClaimPods can't see user-namespace pods) is a real bug, and I'll file it as its own issue with reproduction steps.