diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2017-11-01 11:24:59 -0400 |
---|---|---|
committer | Matthew Heon <matthew.heon@gmail.com> | 2017-11-01 11:24:59 -0400 |
commit | a031b83a09a8628435317a03f199cdc18b78262f (patch) | |
tree | bc017a96769ce6de33745b8b0b1304ccf38e9df0 /server/sandbox_network.go | |
parent | 2b74391cd5281f6fdf391ff8ad50fd1490f6bf89 (diff) | |
download | podman-a031b83a09a8628435317a03f199cdc18b78262f.tar.gz podman-a031b83a09a8628435317a03f199cdc18b78262f.tar.bz2 podman-a031b83a09a8628435317a03f199cdc18b78262f.zip |
Initial checkin from CRI-O repo
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Diffstat (limited to 'server/sandbox_network.go')
-rw-r--r-- | server/sandbox_network.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/server/sandbox_network.go b/server/sandbox_network.go new file mode 100644 index 000000000..15cf99c8f --- /dev/null +++ b/server/sandbox_network.go @@ -0,0 +1,70 @@ +package server + +import ( + "fmt" + "net" + + "github.com/kubernetes-incubator/cri-o/libkpod/sandbox" + "github.com/sirupsen/logrus" + "k8s.io/kubernetes/pkg/kubelet/network/hostport" +) + +// networkStart sets up the sandbox's network and returns the pod IP on success +// or an error +func (s *Server) networkStart(hostNetwork bool, sb *sandbox.Sandbox) (string, error) { + if hostNetwork { + return s.BindAddress(), nil + } + + podNetwork := newPodNetwork(sb) + err := s.netPlugin.SetUpPod(podNetwork) + if err != nil { + return "", fmt.Errorf("failed to create pod network sandbox %s(%s): %v", sb.Name(), sb.ID(), err) + } + + var ip string + if ip, err = s.netPlugin.GetPodNetworkStatus(podNetwork); err != nil { + return "", fmt.Errorf("failed to get network status for pod sandbox %s(%s): %v", sb.Name(), sb.ID(), err) + } + + if len(sb.PortMappings()) > 0 { + ip4 := net.ParseIP(ip).To4() + if ip4 == nil { + return "", fmt.Errorf("failed to get valid ipv4 address for sandbox %s(%s)", sb.Name(), sb.ID()) + } + + if err = s.hostportManager.Add(sb.ID(), &hostport.PodPortMapping{ + Name: sb.Name(), + PortMappings: sb.PortMappings(), + IP: ip4, + HostNetwork: false, + }, "lo"); err != nil { + return "", fmt.Errorf("failed to add hostport mapping for sandbox %s(%s): %v", sb.Name(), sb.ID(), err) + } + + } + return ip, nil +} + +// networkStop cleans up and removes a pod's network. It is best-effort and +// must call the network plugin even if the network namespace is already gone +func (s *Server) networkStop(hostNetwork bool, sb *sandbox.Sandbox) error { + if !hostNetwork { + if err := s.hostportManager.Remove(sb.ID(), &hostport.PodPortMapping{ + Name: sb.Name(), + PortMappings: sb.PortMappings(), + HostNetwork: false, + }); err != nil { + logrus.Warnf("failed to remove hostport for pod sandbox %s(%s): %v", + sb.Name(), sb.ID(), err) + } + + podNetwork := newPodNetwork(sb) + if err := s.netPlugin.TearDownPod(podNetwork); err != nil { + logrus.Warnf("failed to destroy network for pod sandbox %s(%s): %v", + sb.Name(), sb.ID(), err) + } + } + + return nil +} |