api: support namespace wildcard in CSI volume list (#11724)

This commit is contained in:
Luiz Aoqui 2021-12-21 17:19:45 -05:00 committed by GitHub
parent 65eab35412
commit 40093f97cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 1 deletions

3
.changelog/11724.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:improvement
api: Updated the CSI volumes list API to respect wildcard namespaces
```

View File

@ -129,6 +129,8 @@ func (v *CSIVolume) List(args *structs.CSIVolumeListRequest, reply *structs.CSIV
iter, err = snap.CSIVolumesByNodeID(ws, prefix, args.NodeID) iter, err = snap.CSIVolumesByNodeID(ws, prefix, args.NodeID)
} else if args.PluginID != "" { } else if args.PluginID != "" {
iter, err = snap.CSIVolumesByPluginID(ws, ns, prefix, args.PluginID) iter, err = snap.CSIVolumesByPluginID(ws, ns, prefix, args.PluginID)
} else if ns == structs.AllNamespacesSentinel {
iter, err = snap.CSIVolumes(ws)
} else { } else {
iter, err = snap.CSIVolumesByNamespace(ws, ns, prefix) iter, err = snap.CSIVolumesByNamespace(ws, ns, prefix)
} }
@ -155,7 +157,7 @@ func (v *CSIVolume) List(args *structs.CSIVolumeListRequest, reply *structs.CSIV
// Remove by Namespace, since CSIVolumesByNodeID hasn't used // Remove by Namespace, since CSIVolumesByNodeID hasn't used
// the Namespace yet // the Namespace yet
if vol.Namespace != ns { if ns != structs.AllNamespacesSentinel && vol.Namespace != ns {
continue continue
} }

View File

@ -669,6 +669,72 @@ func TestCSIVolumeEndpoint_List(t *testing.T) {
require.Equal(t, vols[1].ID, resp.Volumes[0].ID) require.Equal(t, vols[1].ID, resp.Volumes[0].ID)
} }
func TestCSIVolumeEndpoint_ListAllNamespaces(t *testing.T) {
t.Parallel()
srv, shutdown := TestServer(t, func(c *Config) {
c.NumSchedulers = 0 // Prevent automatic dequeue
})
defer shutdown()
testutil.WaitForLeader(t, srv.RPC)
state := srv.fsm.State()
codec := rpcClient(t, srv)
// Create namespaces.
ns0 := structs.DefaultNamespace
ns1 := "namespace-1"
ns2 := "namespace-2"
err := state.UpsertNamespaces(1000, []*structs.Namespace{{Name: ns1}, {Name: ns2}})
require.NoError(t, err)
// Create volumes in multiple namespaces.
id0 := uuid.Generate()
id1 := uuid.Generate()
id2 := uuid.Generate()
vols := []*structs.CSIVolume{{
ID: id0,
Namespace: ns0,
PluginID: "minnie",
Secrets: structs.CSISecrets{"mysecret": "secretvalue"},
RequestedCapabilities: []*structs.CSIVolumeCapability{{
AccessMode: structs.CSIVolumeAccessModeMultiNodeReader,
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
}},
}, {
ID: id1,
Namespace: ns1,
PluginID: "adam",
RequestedCapabilities: []*structs.CSIVolumeCapability{{
AccessMode: structs.CSIVolumeAccessModeMultiNodeSingleWriter,
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
}},
}, {
ID: id2,
Namespace: ns2,
PluginID: "beth",
RequestedCapabilities: []*structs.CSIVolumeCapability{{
AccessMode: structs.CSIVolumeAccessModeMultiNodeSingleWriter,
AttachmentMode: structs.CSIVolumeAttachmentModeFilesystem,
}},
},
}
err = state.CSIVolumeRegister(1001, vols)
require.NoError(t, err)
// Lookup volumes in all namespaces
get := &structs.CSIVolumeListRequest{
QueryOptions: structs.QueryOptions{
Region: "global",
Namespace: "*",
},
}
var resp structs.CSIVolumeListResponse
err = msgpackrpc.CallWithCodec(codec, "CSIVolume.List", get, &resp)
require.NoError(t, err)
require.Equal(t, uint64(1001), resp.Index)
require.Len(t, resp.Volumes, len(vols))
}
func TestCSIVolumeEndpoint_Create(t *testing.T) { func TestCSIVolumeEndpoint_Create(t *testing.T) {
t.Parallel() t.Parallel()
var err error var err error