mirror of
https://github.com/containers/podman.git
synced 2025-10-19 04:03:23 +08:00
Add a way to retrieve all network aliases for a ctr
The original interface only allowed retrieving aliases for a specific network, not for all networks. This will allow aliases to be retrieved for every network the container is present in, in a single DB operation. Signed-off-by: Matthew Heon <matthew.heon@pm.me>
This commit is contained in:
@ -1013,9 +1013,21 @@ func (s *BoltState) GetNetworkAliases(ctr *Container, network string) ([]string,
|
|||||||
return errors.Wrapf(define.ErrNoSuchCtr, "container %s does not exist in database", ctr.ID())
|
return errors.Wrapf(define.ErrNoSuchCtr, "container %s does not exist in database", ctr.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ctrNetworkBkt := dbCtr.Bucket(networksBkt)
|
||||||
|
if ctrNetworkBkt == nil {
|
||||||
|
// No networks joined, so no aliases
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
inNetwork := ctrNetworkBkt.Get([]byte(network))
|
||||||
|
if inNetwork == nil {
|
||||||
|
return errors.Wrapf(define.ErrNoAliases, "container %s is not part of network %s, no aliases found")
|
||||||
|
}
|
||||||
|
|
||||||
ctrAliasesBkt := dbCtr.Bucket(aliasesBkt)
|
ctrAliasesBkt := dbCtr.Bucket(aliasesBkt)
|
||||||
if ctrAliasesBkt == nil {
|
if ctrAliasesBkt == nil {
|
||||||
return errors.Wrapf(define.ErrNoAliases, "container %s has no network aliases", ctr.ID())
|
// No aliases
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
netAliasesBkt := ctrAliasesBkt.Bucket([]byte(network))
|
netAliasesBkt := ctrAliasesBkt.Bucket([]byte(network))
|
||||||
@ -1035,6 +1047,77 @@ func (s *BoltState) GetNetworkAliases(ctr *Container, network string) ([]string,
|
|||||||
return aliases, nil
|
return aliases, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAllNetworkAliases retrieves the network aliases for the given container in
|
||||||
|
// all CNI networks.
|
||||||
|
func (s *BoltState) GetAllNetworkAliases(ctr *Container) (map[string][]string, error) {
|
||||||
|
if !s.valid {
|
||||||
|
return nil, define.ErrDBClosed
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ctr.valid {
|
||||||
|
return nil, define.ErrCtrRemoved
|
||||||
|
}
|
||||||
|
|
||||||
|
if s.namespace != "" && s.namespace != ctr.config.Namespace {
|
||||||
|
return nil, errors.Wrapf(define.ErrNSMismatch, "container %s is in namespace %q, does not match our namespace %q", ctr.ID(), ctr.config.Namespace, s.namespace)
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrID := []byte(ctr.ID())
|
||||||
|
|
||||||
|
db, err := s.getDBCon()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer s.deferredCloseDBCon(db)
|
||||||
|
|
||||||
|
aliases := make(map[string][]string)
|
||||||
|
|
||||||
|
err = db.View(func(tx *bolt.Tx) error {
|
||||||
|
ctrBucket, err := getCtrBucket(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dbCtr := ctrBucket.Bucket(ctrID)
|
||||||
|
if dbCtr == nil {
|
||||||
|
ctr.valid = false
|
||||||
|
return errors.Wrapf(define.ErrNoSuchCtr, "container %s does not exist in database", ctr.ID())
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrAliasesBkt := dbCtr.Bucket(aliasesBkt)
|
||||||
|
if ctrAliasesBkt == nil {
|
||||||
|
// No aliases present
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrNetworkBkt := dbCtr.Bucket(networksBkt)
|
||||||
|
if ctrNetworkBkt == nil {
|
||||||
|
// No networks joined, so no aliases
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctrNetworkBkt.ForEach(func(network, v []byte) error {
|
||||||
|
netAliasesBkt := ctrAliasesBkt.Bucket(network)
|
||||||
|
if netAliasesBkt == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
netAliases := []string{}
|
||||||
|
aliases[string(network)] = netAliases
|
||||||
|
|
||||||
|
return netAliasesBkt.ForEach(func(alias, v []byte) error {
|
||||||
|
netAliases = append(netAliases, string(alias))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return aliases, nil
|
||||||
|
}
|
||||||
|
|
||||||
// SetNetworkAliases sets network aliases for the given container in the given
|
// SetNetworkAliases sets network aliases for the given container in the given
|
||||||
// network. All existing aliases for that network (if any exist) will be removed,
|
// network. All existing aliases for that network (if any exist) will be removed,
|
||||||
// to be replaced by the new aliases given.
|
// to be replaced by the new aliases given.
|
||||||
|
@ -568,6 +568,25 @@ func (s *InMemoryState) GetNetworkAliases(ctr *Container, network string) ([]str
|
|||||||
return netAliases, nil
|
return netAliases, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAllNetworkAliases gets all network aliases for the given container.
|
||||||
|
func (s *InMemoryState) GetAllNetworkAliases(ctr *Container) (map[string][]string, error) {
|
||||||
|
if !ctr.valid {
|
||||||
|
return nil, define.ErrCtrRemoved
|
||||||
|
}
|
||||||
|
|
||||||
|
ctr, ok := s.containers[ctr.ID()]
|
||||||
|
if !ok {
|
||||||
|
return nil, define.ErrNoSuchCtr
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrAliases, ok := s.ctrNetworkAliases[ctr.ID()]
|
||||||
|
if !ok {
|
||||||
|
return map[string][]string{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return ctrAliases, nil
|
||||||
|
}
|
||||||
|
|
||||||
// SetNetworkAliases sets network aliases for the given container in the given
|
// SetNetworkAliases sets network aliases for the given container in the given
|
||||||
// network.
|
// network.
|
||||||
func (s *InMemoryState) SetNetworkAliases(ctr *Container, network string, aliases []string) error {
|
func (s *InMemoryState) SetNetworkAliases(ctr *Container, network string, aliases []string) error {
|
||||||
|
@ -100,6 +100,8 @@ type State interface {
|
|||||||
|
|
||||||
// Get network aliases for the given container in the given network.
|
// Get network aliases for the given container in the given network.
|
||||||
GetNetworkAliases(ctr *Container, network string) ([]string, error)
|
GetNetworkAliases(ctr *Container, network string) ([]string, error)
|
||||||
|
// Get all network aliases for the given container.
|
||||||
|
GetAllNetworkAliases(ctr *Container) (map[string][]string, error)
|
||||||
// Set network aliases for the given container in the given network.
|
// Set network aliases for the given container in the given network.
|
||||||
SetNetworkAliases(ctr *Container, network string, aliases []string) error
|
SetNetworkAliases(ctr *Container, network string, aliases []string) error
|
||||||
// Remove network aliases for the given container in the given network.
|
// Remove network aliases for the given container in the given network.
|
||||||
|
Reference in New Issue
Block a user