diff options
author | Matthew Heon <matthew.heon@pm.me> | 2020-10-29 14:27:36 -0400 |
---|---|---|
committer | Matthew Heon <mheon@redhat.com> | 2020-11-03 16:26:42 -0500 |
commit | 844d540d042fe7477e62f8cf28e524fcd756f6c8 (patch) | |
tree | 42ff7cfba8d1216e56e7791560c21230333411d1 /libpod/boltdb_state_internal.go | |
parent | 63efde15f14b18f5af385e89936b4ab0868cb357 (diff) | |
download | podman-844d540d042fe7477e62f8cf28e524fcd756f6c8.tar.gz podman-844d540d042fe7477e62f8cf28e524fcd756f6c8.tar.bz2 podman-844d540d042fe7477e62f8cf28e524fcd756f6c8.zip |
Add tests for network aliases
As part of this, we need two new functions, for retrieving all
aliases for a network and removing all aliases for a network,
both required to test.
Also, rework handling for some things the tests discovered were
broken (notably conflicts between container name and existing
aliases).
Signed-off-by: Matthew Heon <matthew.heon@pm.me>
Diffstat (limited to 'libpod/boltdb_state_internal.go')
-rw-r--r-- | libpod/boltdb_state_internal.go | 60 |
1 files changed, 53 insertions, 7 deletions
diff --git a/libpod/boltdb_state_internal.go b/libpod/boltdb_state_internal.go index 1ecf99661..d5c755a52 100644 --- a/libpod/boltdb_state_internal.go +++ b/libpod/boltdb_state_internal.go @@ -634,6 +634,13 @@ func (s *BoltState) addContainer(ctr *Container, pod *Pod) error { return errors.Wrapf(err, "name \"%s\" is in use", ctr.Name()) } + // Check that we don't have any empty network names + for _, net := range ctr.config.Networks { + if net == "" { + return errors.Wrapf(define.ErrInvalidArg, "network names cannot be an empty string") + } + } + // If we have network aliases, check if they are already in use. for net, aliases := range ctr.config.NetworkAliases { // Aliases cannot conflict with container names. @@ -682,6 +689,51 @@ func (s *BoltState) addContainer(ctr *Container, pod *Pod) error { return errors.Wrapf(err, "error adding container %s to all containers bucket in DB", ctr.ID()) } + // Check aliases for all networks, remove conflicts with the + // container name. + for _, net := range ctr.config.Networks { + netAliasesBkt := allAliasesBkt.Bucket([]byte(net)) + if netAliasesBkt == nil { + continue + } + + otherCtrID := netAliasesBkt.Get(ctrName) + if otherCtrID == nil { + continue + } + + if err := netAliasesBkt.Delete(ctrName); err != nil { + return errors.Wrapf(err, "error removing container %s name from network aliases for network %s", ctr.ID(), net) + } + + // We now need to remove from the other container. + // To do this, we work through the container bucket, + // then its aliases bucket, then its aliases for this + // specific network, then we remove the alias. + // Just slightly ridiculous. Just slightly. + otherCtr := ctrBucket.Bucket(otherCtrID) + if otherCtr == nil { + // The state is inconsistent, but we can't do + // much... + logrus.Errorf("Container %s referred to by network alias but not present in state", string(otherCtrID)) + continue + } + otherCtrAliases := otherCtr.Bucket(aliasesBkt) + if otherCtrAliases == nil { + logrus.Errorf("Container %s is missing aliases but but has an alias", string(otherCtrID)) + continue + } + otherCtrNetworkAliases := otherCtrAliases.Bucket([]byte(net)) + if otherCtrNetworkAliases == nil { + logrus.Errorf("Container %s is missing network aliases bucket for network %s but has alias in that network", string(otherCtrID), net) + } + if otherCtrNetworkAliases.Get(ctrName) != nil { + if err := otherCtrNetworkAliases.Delete(ctrName); err != nil { + return errors.Wrapf(err, "error removing container %s name from network %s aliases of container %s", ctr.Name(), net, string(otherCtrID)) + } + } + } + for net, aliases := range ctr.config.NetworkAliases { netAliasesBkt, err := allAliasesBkt.CreateBucketIfNotExists([]byte(net)) if err != nil { @@ -689,13 +741,7 @@ func (s *BoltState) addContainer(ctr *Container, pod *Pod) error { } for _, alias := range aliases { if err := netAliasesBkt.Put([]byte(alias), ctrID); err != nil { - return errors.Wrapf(err, "error adding container %s network aliasa %q to network %q", ctr.ID(), alias, net) - } - } - // If the container's name is present in the aliases - remove it. - if namePresent := netAliasesBkt.Get(ctrName); namePresent != nil { - if err := netAliasesBkt.Delete(ctrName); err != nil { - return errors.Wrapf(err, "error cleaning container name %q from network %q aliases", ctr.Name(), net) + return errors.Wrapf(err, "error adding container %s network alias %q to network %q", ctr.ID(), alias, net) } } } |