Merge pull request #2563 from mheon/lookup_double_match_always_returns_name

Change LookupContainer logic to match Docker
This commit is contained in:
OpenShift Merge Robot
2019-03-07 00:49:52 -08:00
committed by GitHub

View File

@ -382,6 +382,11 @@ func (s *BoltState) LookupContainer(idOrName string) (*Container, error) {
return err return err
} }
namesBucket, err := getNamesBucket(tx)
if err != nil {
return err
}
nsBucket, err := getNSBucket(tx) nsBucket, err := getNSBucket(tx)
if err != nil { if err != nil {
return err return err
@ -395,41 +400,59 @@ func (s *BoltState) LookupContainer(idOrName string) (*Container, error) {
// It might not be in our namespace, but // It might not be in our namespace, but
// getContainerFromDB() will handle that case. // getContainerFromDB() will handle that case.
id = []byte(idOrName) id = []byte(idOrName)
} else { return s.getContainerFromDB(id, ctr, ctrBucket)
// They did not give us a full container ID. }
// Search for partial ID or full name matches
// Use else-if in case the name is set to a partial ID
exists := false
err = idBucket.ForEach(func(checkID, checkName []byte) error {
// If the container isn't in our namespace, we
// can't match it
if s.namespaceBytes != nil {
ns := nsBucket.Get(checkID)
if !bytes.Equal(ns, s.namespaceBytes) {
return nil
}
}
if string(checkName) == idOrName {
if exists {
return errors.Wrapf(ErrCtrExists, "more than one result for ID or name %s", idOrName)
}
id = checkID
exists = true
} else if strings.HasPrefix(string(checkID), idOrName) {
if exists {
return errors.Wrapf(ErrCtrExists, "more than one result for ID or name %s", idOrName)
}
id = checkID
exists = true
}
return nil // Next, check if the full name was given
}) isPod := false
if err != nil { fullID := namesBucket.Get([]byte(idOrName))
return err if fullID != nil {
} else if !exists { // The name exists and maps to an ID.
return errors.Wrapf(ErrNoSuchCtr, "no container with name or ID %s found", idOrName) // However, we are not yet certain the ID is a
// container.
ctrExists = ctrBucket.Bucket(fullID)
if ctrExists != nil {
// A container bucket matching the full ID was
// found.
return s.getContainerFromDB(fullID, ctr, ctrBucket)
} }
// Don't error if we have a name match but it's not a
// container - there's a chance we have a container with
// an ID starting with those characters.
// However, so we can return a good error, note whether
// this is a pod.
isPod = true
}
// We were not given a full container ID or name.
// Search for partial ID matches.
exists := false
err = idBucket.ForEach(func(checkID, checkName []byte) error {
// If the container isn't in our namespace, we
// can't match it
if s.namespaceBytes != nil {
ns := nsBucket.Get(checkID)
if !bytes.Equal(ns, s.namespaceBytes) {
return nil
}
}
if strings.HasPrefix(string(checkID), idOrName) {
if exists {
return errors.Wrapf(ErrCtrExists, "more than one result for container ID %s", idOrName)
}
id = checkID
exists = true
}
return nil
})
if err != nil {
return err
} else if !exists {
if isPod {
return errors.Wrapf(ErrNoSuchCtr, "%s is a pod, not a container", idOrName)
}
return errors.Wrapf(ErrNoSuchCtr, "no container with name or ID %s found", idOrName)
} }
return s.getContainerFromDB(id, ctr, ctrBucket) return s.getContainerFromDB(id, ctr, ctrBucket)
@ -941,6 +964,11 @@ func (s *BoltState) LookupPod(idOrName string) (*Pod, error) {
return err return err
} }
namesBkt, err := getNamesBucket(tx)
if err != nil {
return err
}
nsBkt, err := getNSBucket(tx) nsBkt, err := getNSBucket(tx)
if err != nil { if err != nil {
return err return err
@ -954,41 +982,56 @@ func (s *BoltState) LookupPod(idOrName string) (*Pod, error) {
// It might not be in our namespace, but getPodFromDB() // It might not be in our namespace, but getPodFromDB()
// will handle that case. // will handle that case.
id = []byte(idOrName) id = []byte(idOrName)
} else { return s.getPodFromDB(id, pod, podBkt)
// They did not give us a full pod ID. }
// Search for partial ID or full name matches
// Use else-if in case the name is set to a partial ID
exists := false
err = idBucket.ForEach(func(checkID, checkName []byte) error {
// If the pod isn't in our namespace, we
// can't match it
if s.namespaceBytes != nil {
ns := nsBkt.Get(checkID)
if !bytes.Equal(ns, s.namespaceBytes) {
return nil
}
}
if string(checkName) == idOrName {
if exists {
return errors.Wrapf(ErrPodExists, "more than one result for ID or name %s", idOrName)
}
id = checkID
exists = true
} else if strings.HasPrefix(string(checkID), idOrName) {
if exists {
return errors.Wrapf(ErrPodExists, "more than one result for ID or name %s", idOrName)
}
id = checkID
exists = true
}
return nil // Next, check if the full name was given
}) isCtr := false
if err != nil { fullID := namesBkt.Get([]byte(idOrName))
return err if fullID != nil {
} else if !exists { // The name exists and maps to an ID.
return errors.Wrapf(ErrNoSuchPod, "no pod with name or ID %s found", idOrName) // However, we aren't yet sure if the ID is a pod.
podExists = podBkt.Bucket(fullID)
if podExists != nil {
// A pod bucket matching the full ID was found.
return s.getPodFromDB(fullID, pod, podBkt)
} }
// Don't error if we have a name match but it's not a
// pod - there's a chance we have a pod with an ID
// starting with those characters.
// However, so we can return a good error, note whether
// this is a container.
isCtr = true
}
// They did not give us a full pod name or ID.
// Search for partial ID matches.
exists := false
err = idBucket.ForEach(func(checkID, checkName []byte) error {
// If the pod isn't in our namespace, we
// can't match it
if s.namespaceBytes != nil {
ns := nsBkt.Get(checkID)
if !bytes.Equal(ns, s.namespaceBytes) {
return nil
}
}
if strings.HasPrefix(string(checkID), idOrName) {
if exists {
return errors.Wrapf(ErrPodExists, "more than one result for ID or name %s", idOrName)
}
id = checkID
exists = true
}
return nil
})
if err != nil {
return err
} else if !exists {
if isCtr {
return errors.Wrapf(ErrNoSuchPod, "%s is a container, not a pod", idOrName)
}
return errors.Wrapf(ErrNoSuchPod, "no pod with name or ID %s found", idOrName)
} }
// We might have found a container ID, but it's OK // We might have found a container ID, but it's OK