Skip to content

Commit

Permalink
api: fix Volume list endpoint with prefix and * ns
Browse files Browse the repository at this point in the history
When using a prefix value and the * wildcard for namespace, the endpoint
would not take the prefix value into consideration due to the order in
which the checks were executed but also the logic for retrieving volumes
from the state store.

This commit changes the order to check for a prefix first and wraps the
result iterator of the state store query in a filter to apply the
prefix.
  • Loading branch information
lgfa29 committed Mar 3, 2022
1 parent d725970 commit dfda28a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
8 changes: 5 additions & 3 deletions nomad/csi_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ func (v *CSIVolume) List(args *structs.CSIVolumeListRequest, reply *structs.CSIV
iter, err = snap.CSIVolumesByNodeID(ws, prefix, args.NodeID)
} else if args.PluginID != "" {
iter, err = snap.CSIVolumesByPluginID(ws, ns, prefix, args.PluginID)
} else if ns == structs.AllNamespacesSentinel {
iter, err = snap.CSIVolumes(ws)
} else {
} else if prefix != "" {
iter, err = snap.CSIVolumesByIDPrefix(ws, ns, prefix)
} else if ns != structs.AllNamespacesSentinel {
iter, err = snap.CSIVolumesByNamespace(ws, ns, prefix)
} else {
iter, err = snap.CSIVolumes(ws)
}

if err != nil {
Expand Down
31 changes: 30 additions & 1 deletion nomad/state/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2265,8 +2265,13 @@ func (s *StateStore) CSIVolumesByPluginID(ws memdb.WatchSet, namespace, prefix,
}

// CSIVolumesByIDPrefix supports search. Caller should snapshot if it wants to
// also denormalize the plugins.
// also denormalize the plugins. If using a prefix, the results will not use an
// index.
func (s *StateStore) CSIVolumesByIDPrefix(ws memdb.WatchSet, namespace, volumeID string) (memdb.ResultIterator, error) {
if namespace == structs.AllNamespacesSentinel {
return s.csiVolumeByIDPrefixAllNamespaces(ws, volumeID)
}

txn := s.db.ReadTxn()

iter, err := txn.Get("csi_volumes", "id_prefix", namespace, volumeID)
Expand All @@ -2279,6 +2284,30 @@ func (s *StateStore) CSIVolumesByIDPrefix(ws memdb.WatchSet, namespace, volumeID
return iter, nil
}

func (s *StateStore) csiVolumeByIDPrefixAllNamespaces(ws memdb.WatchSet, prefix string) (memdb.ResultIterator, error) {
txn := s.db.ReadTxn()

// Walk the entire csi_volumes table
iter, err := txn.Get("csi_volumes", "id")

if err != nil {
return nil, err
}

ws.Add(iter.WatchCh())

// Filter the iterator by ID prefix
f := func(raw interface{}) bool {
v, ok := raw.(*structs.CSIVolume)
if !ok {
return false
}
return !strings.HasPrefix(v.ID, prefix)
}
wrap := memdb.NewFilterIterator(iter, f)
return wrap, nil
}

// CSIVolumesByNodeID looks up CSIVolumes in use on a node. Caller should
// snapshot if it wants to also denormalize the plugins.
func (s *StateStore) CSIVolumesByNodeID(ws memdb.WatchSet, prefix, nodeID string) (memdb.ResultIterator, error) {
Expand Down

0 comments on commit dfda28a

Please sign in to comment.