summaryrefslogtreecommitdiff
path: root/libpod/in_memory_state.go
diff options
context:
space:
mode:
authorMatthew Heon <matthew.heon@pm.me>2020-08-04 15:51:13 -0400
committerMatthew Heon <matthew.heon@pm.me>2020-08-10 10:15:51 -0400
commit569854d6348e1cd74e8d654c88720e5517898f1a (patch)
tree8eaf3233da1ce1a4d1c3d39dc502a08e60307e2d /libpod/in_memory_state.go
parentda00482ef29e0a04a690f1538391e38b0b951dd0 (diff)
downloadpodman-569854d6348e1cd74e8d654c88720e5517898f1a.tar.gz
podman-569854d6348e1cd74e8d654c88720e5517898f1a.tar.bz2
podman-569854d6348e1cd74e8d654c88720e5517898f1a.zip
Unconditionally retrieve pod names via API
The ListContainers API previously had a Pod parameter, which determined if pod name was returned (but, notably, not Pod ID, which was returned unconditionally). This was fairly confusing, so we decided to deprecate/remove the parameter and return it unconditionally. To do this without serious performance implications, we need to avoid expensive JSON decodes of pod configuration in the DB. The way our Bolt tables are structured, retrieving name given ID is actually quite cheap, but we did not expose this via the Libpod API. Add a new GetName API to do this. Fixes #7214 Signed-off-by: Matthew Heon <matthew.heon@pm.me>
Diffstat (limited to 'libpod/in_memory_state.go')
-rw-r--r--libpod/in_memory_state.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/libpod/in_memory_state.go b/libpod/in_memory_state.go
index 2ac05e88d..0de25a6ef 100644
--- a/libpod/in_memory_state.go
+++ b/libpod/in_memory_state.go
@@ -106,6 +106,36 @@ func (s *InMemoryState) SetNamespace(ns string) error {
return nil
}
+// GetName retrieves the name associated with a given ID.
+// Works with both Container and Pod IDs.
+func (s *InMemoryState) GetName(id string) (string, error) {
+ if id == "" {
+ return "", define.ErrEmptyID
+ }
+
+ var idIndex *truncindex.TruncIndex
+ if s.namespace != "" {
+ nsIndex, ok := s.namespaceIndexes[s.namespace]
+ if !ok {
+ // We have no containers in the namespace
+ // Return false
+ return "", define.ErrNoSuchCtr
+ }
+ idIndex = nsIndex.idIndex
+ } else {
+ idIndex = s.idIndex
+ }
+
+ fullID, err := idIndex.Get(id)
+ if err != nil {
+ if err == truncindex.ErrNotExist {
+ return "", define.ErrNoSuchCtr
+ }
+ return "", errors.Wrapf(err, "error performing truncindex lookup for ID %s", id)
+ }
+ return fullID, nil
+}
+
// Container retrieves a container from its full ID
func (s *InMemoryState) Container(id string) (*Container, error) {
if id == "" {