Repository: kubernetes-sigs/scheduler-plugins
Pull request #908 (2025-05-19 07:01:30)

nrt: log: return updated generation value for cache
<!--  Thanks for sending a pull request!  Here are some tips for you:

1. If this is your first time, please read our contributor guidelines: https://git.k8s.io/community/contributors/guide/first-contribution.md#your-first-contribution and developer guide https://git.k8s.io/community/contributors/devel/development.md#development-guide
2. Please label this pull request according to what type of issue you are addressing, especially if this is a release targeted pull request. For reference on required PR/issue labels, read here:
https://git.k8s.io/community/contributors/devel/sig-release/release.md#issuepr-kind-label
3. Ensure you have added or ran the appropriate tests for your PR: https://git.k8s.io/community/contributors/devel/sig-testing/testing.md
4. If you want *faster* PR reviews, read how: https://git.k8s.io/community/contributors/guide/pull-requests.md#best-practices-for-faster-reviews
5. If the PR is unfinished, see how to mark it: https://git.k8s.io/community/contributors/guide/pull-requests.md#marking-unfinished-pull-requests
-->

#### What type of PR is this?
/kind bug
A bug in the logs

<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind feature

Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
-->

#### What this PR does / why we need it:

The logs show misaligned generation value when tackling the same node in overreserve.go and filter.go logs where the former is always up to date while the latter is never updated (generation=0):

```
1 overreserve.go:310] "NodeTopology podset fingerprint mismatch" logger="nrtcache.resync" generation=3 node="node-1"
...
1 filter.go:202] "invalid topology data" logger="FilterWithNominatedPods.Filter.NodeResourceTopologyMatch" pod="default/test-6f798bf799-tj7bc" pod="default/test-6f798bf799-tj7bc" podUID="f221ce7c-09c9-4462-a67a-95f5fdec3fb7" node="node-1" generation=0
```

The only cache implementation that defines and uses "generation" is OverReserve thus this commit updates the method GetCachedNRTCopy() to return updated generation value in the returned CachedNRTInfo.


#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

#### Special notes for your reviewer:

#### Does this PR introduce a user-facing change?
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".

For more information on release notes see: https://git.k8s.io/community/contributors/guide/release-notes.md
-->
```release-note
NONE
```


k8s-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/scheduler-plugins/pull/908#" title="Author self-approved">shajmakh</a>*
**Once this PR has been reviewed and has the lgtm label**, please assign [ffromani](https://github.com/ffromani) for approval. For more information see [the Code Review Process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process).

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fscheduler-plugins).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubernetes-sigs/scheduler-plugins/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["ffromani"]} -->
k8s-ci-robot: Hi @shajmakh. Thanks for your PR.

I'm waiting for a [kubernetes-sigs](https://github.com/orgs/kubernetes-sigs/people) member to verify that this patch is reasonable to test. If it is, they should reply with `/ok-to-test` on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should [join the org](https://git.k8s.io/community/community-membership.md#member) to skip this step.

Once the patch is verified, the new status will be reflected by the `ok-to-test` label.

I understand the commands that are listed [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fscheduler-plugins).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.
</details>

netlify[bot]: ### <span aria-hidden="true">👷</span> Deploy Preview for *kubernetes-sigs-scheduler-plugins* processing.


|  Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | fb7555047eb8ae05862bb28da5330d7c8b5cba49 |
|<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/projects/kubernetes-sigs-scheduler-plugins/deploys/682ad74d4d2e9400089f9112 |
shajmakh: /cc @ffromani 
shajmakh: /ok-to-test
k8s-ci-robot: @shajmakh: Cannot trigger testing until a trusted user reviews the PR and leaves an `/ok-to-test` message.

<details>

In response to [this](https://github.com/kubernetes-sigs/scheduler-plugins/pull/908#issuecomment-2889865537):

>/ok-to-test


Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.
</details>
ffromani: /ok-to-test

Patch:
diff --git a/pkg/noderesourcetopology/cache/overreserve.go b/pkg/noderesourcetopology/cache/overreserve.go
index 9cd5008c..b6848777 100644
--- a/pkg/noderesourcetopology/cache/overreserve.go
+++ b/pkg/noderesourcetopology/cache/overreserve.go
@@ -101,17 +101,16 @@ func NewOverReserve(ctx context.Context, lh logr.Logger, cfg *apiconfig.NodeReso
 func (ov *OverReserve) GetCachedNRTCopy(ctx context.Context, nodeName string, pod *corev1.Pod) (*topologyv1alpha2.NodeResourceTopology, CachedNRTInfo) {
 	ov.lock.Lock()
 	defer ov.lock.Unlock()
+	info := CachedNRTInfo{Generation: ov.generation}
 	if ov.nodesWithForeignPods.IsSet(nodeName) {
-		return nil, CachedNRTInfo{}
+		return nil, info
 	}
 
-	info := CachedNRTInfo{Fresh: true}
+	info.Fresh = true
 	nrt := ov.nrts.GetNRTCopyByNodeName(nodeName)
 	if nrt == nil {
 		return nil, info
 	}
-
-	info.Generation = ov.generation
 	nodeAssumedResources, ok := ov.assumedResources[nodeName]
 	if !ok {
 		return nrt, info
@@ -341,7 +340,7 @@ func (ov *OverReserve) Resync() {
 }
 
 // FlushNodes drops all the cached information about a given node, resetting its state clean.
-func (ov *OverReserve) FlushNodes(lh logr.Logger, nrts ...*topologyv1alpha2.NodeResourceTopology) {
+func (ov *OverReserve) FlushNodes(lh logr.Logger, nrts ...*topologyv1alpha2.NodeResourceTopology) uint64 {
 	ov.lock.Lock()
 	defer ov.lock.Unlock()
 
@@ -355,12 +354,14 @@ func (ov *OverReserve) FlushNodes(lh logr.Logger, nrts ...*topologyv1alpha2.Node
 	}
 
 	if len(nrts) == 0 {
-		return
+		return ov.generation
 	}
 
 	// increase only if we mutated the internal state
 	ov.generation += 1
 	lh.V(2).Info("generation", "new", ov.generation)
+	return ov.generation
+
 }
 
 // to be used only in tests


Test patch:
diff --git a/pkg/noderesourcetopology/cache/overreserve_test.go b/pkg/noderesourcetopology/cache/overreserve_test.go
index 33214b44..b8b15a03 100644
--- a/pkg/noderesourcetopology/cache/overreserve_test.go
+++ b/pkg/noderesourcetopology/cache/overreserve_test.go
@@ -30,6 +30,7 @@ import (
 	"k8s.io/apimachinery/pkg/api/equality"
 	"k8s.io/apimachinery/pkg/api/resource"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/runtime"
 	podlisterv1 "k8s.io/client-go/listers/core/v1"
 	"k8s.io/klog/v2"
 
@@ -434,17 +435,35 @@ func TestFlush(t *testing.T) {
 
 	lh := klog.Background()
 
-	nrtCache.FlushNodes(lh, expectedNodeTopology.DeepCopy())
+	expectedGen := nrtCache.generation + 1
+	gen1 := nrtCache.FlushNodes(lh, expectedNodeTopology.DeepCopy())
+	if gen1 != expectedGen {
+		t.Fatalf("generation is expected to increase once after flushing a dirty node\ngot %d expected %d", gen1, expectedGen)
+	}
 
 	dirtyNodes := nrtCache.GetDesyncedNodes(lh)
 	if dirtyNodes.Len() != 0 {
 		t.Errorf("dirty nodes after flush: %v", dirtyNodes)
 	}
 
-	nrtObj, _ := nrtCache.GetCachedNRTCopy(context.Background(), "node1", testPod)
+	nrtObj, nrtInfo := nrtCache.GetCachedNRTCopy(context.Background(), "node1", testPod)
 	if !reflect.DeepEqual(nrtObj, expectedNodeTopology) {
 		t.Fatalf("unexpected object from cache\ngot: %s\nexpected: %s\n", dumpNRT(nrtObj), dumpNRT(nodeTopologies[0]))
 	}
+
+	expectedNrtInfo := CachedNRTInfo{
+		Fresh:      true,
+		Generation: expectedGen,
+	}
+	if !reflect.DeepEqual(nrtInfo, expectedNrtInfo) {
+		t.Fatalf("unexpected NRT info from cache\ngot: %+v\nexpected: %+v\n", nrtInfo, expectedNrtInfo)
+	}
+
+	// flush again without dirty nodes
+	gen2 := nrtCache.FlushNodes(lh)
+	if gen2 != expectedGen {
+		t.Fatalf("generation shouldn't change with no dirty nodes\ngot %d expected %d", gen2, expectedGen)
+	}
 }
 
 func TestResyncNoPodFingerprint(t *testing.T) {
@@ -971,3 +990,49 @@ func TestMakeNodeToPodDataMap(t *testing.T) {
 		})
 	}
 }
+
+func TestOverresevedGetCachedNRTCopyWithForeignPods(t *testing.T) {
+	testNodeName := "worker-node-G"
+	nrt := makeTestNRT(testNodeName)
+	pod := &corev1.Pod{} // API placeholder
+	fakePodLister := &fakePodLister{}
+
+	objs := []runtime.Object{nrt}
+	fakeClient, err := tu.NewFakeClient(objs...)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	ctx := context.Background()
+	lh := klog.Background()
+	nrtCache, err := NewOverReserve(ctx, lh, nil, fakeClient, fakePodLister, podprovider.IsPodRelevantAlways)
+	if err != nil {
+		t.Fatalf("unexpected error creating cache: %v", err)
+	}
+
+	expectedNrtInfo := CachedNRTInfo{
+		Generation: 0,
+		Fresh:      true,
+	}
+	_, gotInfo := nrtCache.GetCachedNRTCopy(ctx, testNodeName, pod)
+	if !reflect.DeepEqual(gotInfo, expectedNrtInfo) {
+		t.Errorf("mismatched nrt info from cache, got %+v expected %+v", gotInfo, expectedNrtInfo)
+	}
+
+	// pointless, but will force a generation increase
+	gen := nrtCache.FlushNodes(lh, nrt)
+	if gen == 0 {
+		t.Fatalf("FlushNodes didn't increase the generation")
+	}
+
+	// expected to mark cached data as stale (!fresh)
+	nrtCache.NodeHasForeignPods(testNodeName, pod)
+
+	_, gotInfo = nrtCache.GetCachedNRTCopy(ctx, testNodeName, pod)
+	if gotInfo.Generation != gen {
+		t.Errorf("mismatched generation, got %v expected %v", gotInfo.Generation, gen)
+	}
+	if gotInfo.Fresh {
+		t.Errorf("cached data reported fresh when node has foreign pods")
+	}
+}