Skip to content

Commit 64b0657

Browse files
authored
Select a node's publications by nodeID instead of the node-name label
Node startup cleanup builds its desired publication set from the TridentVolumePublication CRs it lists for the node. The CRD client selected them with the trident.netapp.io/nodeName label, but publications written by older releases have no such label until the controller adds it, and the controller does so asynchronously after the node has already started. Any volume missing from the desired set is treated as stale, so its pod mounts were unmounted, the volume was force-unstaged, and its tracking file was deleted. Kubelet still considers those volumes staged, so new pods then fail NodePublishVolume. List the publications and keep those whose spec nodeID matches the node. That field is written for every publication and equals the label value wherever the label exists, so the result no longer depends on whether the controller has labeled the publication yet.
1 parent a1ab098 commit 64b0657

2 files changed

Lines changed: 163 additions & 4 deletions

File tree

‎frontend/csi/tridentcontroller/crd/client.go‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,22 +337,25 @@ func (c *Client) StartNodeCleanupWatch(ctx context.Context, nodeName string) (<-
337337
return events, stopFn, nil
338338
}
339339

340-
// GetDesiredPublications lists TridentVolumePublication CRs labeled for the node.
340+
// GetDesiredPublications returns the TridentVolumePublication CRs for the node
341341
func (c *Client) GetDesiredPublications(
342342
ctx context.Context, nodeName string,
343343
) (map[string]*models.VolumePublicationExternal, error) {
344344
if err := c.ensureTridentClient(); err != nil {
345345
return nil, err
346346
}
347347
publications, err := c.tridentClient.TridentV1().TridentVolumePublications(c.tridentNamespace).List(
348-
ctx, metav1.ListOptions{LabelSelector: fmt.Sprintf("%s=%s", config.TridentNodeNameLabel, nodeName)},
348+
ctx, metav1.ListOptions{},
349349
)
350350
if err != nil {
351-
return nil, err
351+
return nil, fmt.Errorf("list volume publications: %w", err)
352352
}
353353

354-
desiredPublicationState := make(map[string]*models.VolumePublicationExternal, len(publications.Items))
354+
desiredPublicationState := make(map[string]*models.VolumePublicationExternal)
355355
for _, pubCR := range publications.Items {
356+
if pubCR.NodeID != nodeName {
357+
continue
358+
}
356359
pub, pubErr := pubCR.Persistent()
357360
if pubErr != nil {
358361
return nil, pubErr
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Copyright 2026 NetApp, Inc. All Rights Reserved.
2+
3+
package crd
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"sort"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
k8sruntime "k8s.io/apimachinery/pkg/runtime"
14+
k8stesting "k8s.io/client-go/testing"
15+
16+
"github.com/netapp/trident/config"
17+
tridentv1 "github.com/netapp/trident/persistent_store/crd/apis/netapp/v1"
18+
tridentfake "github.com/netapp/trident/persistent_store/crd/client/clientset/versioned/fake"
19+
"github.com/netapp/trident/utils/models"
20+
)
21+
22+
// testPublicationCR builds a TridentVolumePublication the way the controller persists it. When
23+
// labeled is false the node-name label is removed, as on publications that predate the label.
24+
func testPublicationCR(t *testing.T, namespace, volume, node string, labeled bool) *tridentv1.TridentVolumePublication {
25+
t.Helper()
26+
27+
cr, err := tridentv1.NewTridentVolumePublication(&models.VolumePublication{
28+
Name: models.GenerateVolumePublishName(volume, node),
29+
VolumeName: volume,
30+
NodeName: node,
31+
})
32+
require.NoError(t, err)
33+
cr.Namespace = namespace
34+
if !labeled {
35+
delete(cr.Labels, config.TridentNodeNameLabel)
36+
}
37+
return cr
38+
}
39+
40+
func TestClient_GetDesiredPublications(t *testing.T) {
41+
const (
42+
ns = "trident"
43+
node = "node-a"
44+
)
45+
46+
type publication struct {
47+
volume string
48+
node string
49+
labeled bool
50+
}
51+
52+
tests := []struct {
53+
name string
54+
queryNode string // defaults to node
55+
publications []publication
56+
wantVolumeNames []string
57+
}{
58+
{
59+
name: "no publications",
60+
publications: nil,
61+
wantVolumeNames: []string{},
62+
},
63+
{
64+
name: "this node, labeled",
65+
publications: []publication{{"vol1", node, true}},
66+
wantVolumeNames: []string{"vol1"},
67+
},
68+
{
69+
name: "this node, unlabeled",
70+
publications: []publication{{"vol1", node, false}},
71+
wantVolumeNames: []string{"vol1"},
72+
},
73+
{
74+
name: "other node, labeled",
75+
publications: []publication{{"vol1", "node-b", true}},
76+
wantVolumeNames: []string{},
77+
},
78+
{
79+
name: "other node, unlabeled",
80+
publications: []publication{{"vol1", "node-b", false}},
81+
wantVolumeNames: []string{},
82+
},
83+
{
84+
name: "mix of labeled and unlabeled across nodes",
85+
publications: []publication{
86+
{"vol1", node, true},
87+
{"vol2", node, false},
88+
{"vol3", "node-b", true},
89+
{"vol4", "node-b", false},
90+
{"vol5", node, false},
91+
},
92+
wantVolumeNames: []string{"vol1", "vol2", "vol5"},
93+
},
94+
{
95+
name: "same volume published to this node and another node",
96+
publications: []publication{
97+
{"vol1", node, false},
98+
{"vol1", "node-b", false},
99+
},
100+
wantVolumeNames: []string{"vol1"},
101+
},
102+
{
103+
name: "node name that is a dotted suffix of another node name",
104+
queryNode: "b",
105+
publications: []publication{
106+
{"vol1", "b", false},
107+
{"vol2", "a.b", false},
108+
},
109+
wantVolumeNames: []string{"vol1"},
110+
},
111+
}
112+
113+
for _, tc := range tests {
114+
t.Run(tc.name, func(t *testing.T) {
115+
objects := make([]k8sruntime.Object, 0, len(tc.publications))
116+
for _, p := range tc.publications {
117+
objects = append(objects, testPublicationCR(t, ns, p.volume, p.node, p.labeled))
118+
}
119+
c := &Client{tridentClient: tridentfake.NewSimpleClientset(objects...), tridentNamespace: ns}
120+
121+
queryNode := node
122+
if tc.queryNode != "" {
123+
queryNode = tc.queryNode
124+
}
125+
desired, err := c.GetDesiredPublications(context.Background(), queryNode)
126+
require.NoError(t, err)
127+
128+
gotVolumeNames := make([]string, 0, len(desired))
129+
for volumeName, pub := range desired {
130+
assert.Equal(t, volumeName, pub.VolumeName)
131+
assert.Equal(t, queryNode, pub.NodeName)
132+
gotVolumeNames = append(gotVolumeNames, volumeName)
133+
}
134+
sort.Strings(gotVolumeNames)
135+
assert.Equal(t, tc.wantVolumeNames, gotVolumeNames)
136+
})
137+
}
138+
}
139+
140+
func TestClient_GetDesiredPublications_ListError(t *testing.T) {
141+
const ns = "trident"
142+
143+
tridentClient := tridentfake.NewSimpleClientset()
144+
tridentClient.PrependReactor("list", "tridentvolumepublications",
145+
func(k8stesting.Action) (bool, k8sruntime.Object, error) {
146+
return true, nil, fmt.Errorf("apiserver unavailable")
147+
})
148+
c := &Client{tridentClient: tridentClient, tridentNamespace: ns}
149+
150+
desired, err := c.GetDesiredPublications(context.Background(), "node-a")
151+
152+
assert.Error(t, err)
153+
assert.ErrorContains(t, err, "list volume publications")
154+
assert.ErrorContains(t, err, "apiserver unavailable")
155+
assert.Nil(t, desired)
156+
}

0 commit comments

Comments
 (0)