diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2017-12-06 15:54:59 -0500 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2017-12-14 23:59:21 +0000 |
commit | 0ff92f8e20edb46eb8a9d82b929e153bcdaa3044 (patch) | |
tree | 14289c5dea9b738004837144ec6c5045d2f4789d /libpod/networking.go | |
parent | 824a648fcb87c112fb498db94b8e39a84ba649bd (diff) | |
download | podman-0ff92f8e20edb46eb8a9d82b929e153bcdaa3044.tar.gz podman-0ff92f8e20edb46eb8a9d82b929e153bcdaa3044.tar.bz2 podman-0ff92f8e20edb46eb8a9d82b929e153bcdaa3044.zip |
Add network namespaces to SQL state
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Closes: #109
Approved by: mheon
Diffstat (limited to 'libpod/networking.go')
-rw-r--r-- | libpod/networking.go | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/libpod/networking.go b/libpod/networking.go index 893d6863a..f613ad5f8 100644 --- a/libpod/networking.go +++ b/libpod/networking.go @@ -3,11 +3,12 @@ package libpod import ( "github.com/containernetworking/plugins/pkg/ns" "github.com/cri-o/ocicni/pkg/ocicni" + "github.com/pkg/errors" "github.com/sirupsen/logrus" ) // Get an OCICNI network config -func getPodNetwork(id, name, nsPath string, ports []ocicni.PortMappings) ocicni.PodNetwork { +func getPodNetwork(id, name, nsPath string, ports []ocicni.PortMapping) ocicni.PodNetwork { return ocicni.PodNetwork{ Name: name, Namespace: name, // TODO is there something else we should put here? We don't know about Kube namespaces @@ -17,29 +18,31 @@ func getPodNetwork(id, name, nsPath string, ports []ocicni.PortMappings) ocicni. } } -// Create and configure a new network namespace -func (r *Runtime) createNetNS(id, name string, ports []ocicni.PortMapping) (n ns.NetNS, err error) { +// Create and configure a new network namespace for a container +func (r *Runtime) createNetNS(ctr *Container) (err error) { ns, err := ns.NewNS() if err != nil { - return nil, errors.Wrapf(err, "error creating network namespace %s", id) + return errors.Wrapf(err, "error creating network namespace for container %s", ctr.ID()) } defer func() { if err != nil { if err2 := ns.Close(); err2 != nil { - logrus.Errorf("Error closing partially created network namespace %s: %v", id, err2) + logrus.Errorf("Error closing partially created network namespace for container %s: %v", ctr.ID(), err2) } } }() - podNetwork := getPodNetwork(id, name, ns.Path(), ports) + podNetwork := getPodNetwork(ctr.ID(), ctr.Name(), ns.Path(), ctr.config.PortMappings) if err := r.netPlugin.SetUpPod(podNetwork); err != nil { - return nil, errors.Wrapf(err, "error configuring network namespace %s", id) + return errors.Wrapf(err, "error configuring network namespace for container %s", ctr.ID()) } // TODO hostport mappings for forwarded ports - return ns, nil + ctr.state.NetNS = ns + + return nil } // Join an existing network namespace @@ -53,13 +56,25 @@ func joinNetNS(path string) (ns.NetNS, error) { } // Tear down a network namespace -func (r *Runtime) teardownNetNS(id, name string, ports []ocicni.PortMapping, ns ns.NetNS) error { +func (r *Runtime) teardownNetNS(ctr *Container) error { + if ctr.state.NetNS == nil { + // The container has no network namespace, we're set + return nil + } + // TODO hostport mappings for forwarded ports should be undone - podNetwork := getPodNetwork(id, name, ns.Path(), ports) + podNetwork := getPodNetwork(ctr.ID(), ctr.Name(), ctr.state.NetNS.Path(), ctr.config.PortMappings) + // The network may have already been torn down, so don't fail here, just log if err := r.netPlugin.TearDownPod(podNetwork); err != nil { - return errors.Wrapf(err, "failed to remove network namespace %s", id) + logrus.Errorf("Failed to tear down network namespace for container %s: %v", ctr.ID(), err) + } + + if err := ctr.state.NetNS.Close(); err != nil { + return errors.Wrapf(err, "error closing network namespace for container %s", ctr.ID()) } + ctr.state.NetNS = nil + return nil } |