Add network namespaces to SQL state

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>

Closes: #109
Approved by: mheon
This commit is contained in:
Matthew Heon
2017-12-06 15:54:59 -05:00
committed by Atomic Bot
parent 824a648fcb
commit 0ff92f8e20
4 changed files with 101 additions and 24 deletions

View File

@ -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 {
// TODO hostport mappings for forwarded ports should be undone
podNetwork := getPodNetwork(id, name, ns.Path(), ports)
if err := r.netPlugin.TearDownPod(podNetwork); err != nil {
return errors.Wrapf(err, "failed to remove network namespace %s", id)
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(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 {
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
}