2016-09-19 07:21:14 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2016-12-12 15:37:03 +00:00
|
|
|
"crypto/rand"
|
2016-12-07 16:38:36 +00:00
|
|
|
"errors"
|
2016-09-19 07:21:14 +00:00
|
|
|
"fmt"
|
2016-12-12 15:37:03 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-11-23 17:16:21 +00:00
|
|
|
"sync"
|
2016-09-19 07:21:14 +00:00
|
|
|
|
2016-11-23 17:16:21 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-12-13 08:34:55 +00:00
|
|
|
"github.com/containernetworking/cni/pkg/ns"
|
2016-09-20 08:27:11 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-09-26 23:55:12 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2016-12-13 08:34:55 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2017-02-01 00:45:59 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2016-09-20 14:15:22 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
|
2016-09-19 07:21:14 +00:00
|
|
|
)
|
|
|
|
|
2016-11-23 17:16:21 +00:00
|
|
|
type sandboxNetNs struct {
|
|
|
|
sync.Mutex
|
2016-12-13 08:34:55 +00:00
|
|
|
ns ns.NetNS
|
|
|
|
symlink *os.File
|
|
|
|
closed bool
|
|
|
|
restored bool
|
2016-11-23 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
2016-12-12 15:37:03 +00:00
|
|
|
func (ns *sandboxNetNs) symlinkCreate(name string) error {
|
|
|
|
b := make([]byte, 4)
|
|
|
|
_, randErr := rand.Reader.Read(b)
|
|
|
|
if randErr != nil {
|
|
|
|
return randErr
|
|
|
|
}
|
|
|
|
|
|
|
|
nsName := fmt.Sprintf("%s-%x", name, b)
|
|
|
|
symlinkPath := filepath.Join(nsRunDir, nsName)
|
|
|
|
|
|
|
|
if err := os.Symlink(ns.ns.Path(), symlinkPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := os.Open(symlinkPath)
|
|
|
|
if err != nil {
|
|
|
|
if removeErr := os.RemoveAll(symlinkPath); removeErr != nil {
|
|
|
|
return removeErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ns.symlink = fd
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *sandboxNetNs) symlinkRemove() error {
|
|
|
|
if err := ns.symlink.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.RemoveAll(ns.symlink.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSymbolicLink(path string) (bool, error) {
|
|
|
|
fi, err := os.Lstat(path)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fi.Mode()&os.ModeSymlink == os.ModeSymlink, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func netNsGet(nspath, name string) (*sandboxNetNs, error) {
|
2016-11-23 17:16:21 +00:00
|
|
|
if err := ns.IsNSorErr(nspath); err != nil {
|
|
|
|
return nil, errSandboxClosedNetNS
|
|
|
|
}
|
|
|
|
|
2016-12-12 15:37:03 +00:00
|
|
|
symlink, symlinkErr := isSymbolicLink(nspath)
|
|
|
|
if symlinkErr != nil {
|
|
|
|
return nil, symlinkErr
|
|
|
|
}
|
|
|
|
|
|
|
|
var resolvedNsPath string
|
|
|
|
if symlink {
|
|
|
|
path, err := os.Readlink(nspath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resolvedNsPath = path
|
|
|
|
} else {
|
|
|
|
resolvedNsPath = nspath
|
|
|
|
}
|
|
|
|
|
|
|
|
netNS, err := ns.GetNS(resolvedNsPath)
|
2016-11-23 17:16:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-14 18:47:05 +00:00
|
|
|
netNs := &sandboxNetNs{ns: netNS, closed: false, restored: true}
|
2016-12-12 15:37:03 +00:00
|
|
|
|
|
|
|
if symlink {
|
|
|
|
fd, err := os.Open(nspath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
netNs.symlink = fd
|
|
|
|
} else {
|
|
|
|
if err := netNs.symlinkCreate(name); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return netNs, nil
|
2016-11-23 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func hostNetNsPath() (string, error) {
|
|
|
|
netNS, err := ns.GetCurrentNS()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer netNS.Close()
|
|
|
|
return netNS.Path(), nil
|
|
|
|
}
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
type sandbox struct {
|
2017-05-04 16:41:15 +00:00
|
|
|
id string
|
|
|
|
namespace string
|
|
|
|
// OCI pod name (eg "<namespace>-<name>-<attempt>")
|
|
|
|
name string
|
|
|
|
// Kubernetes pod name (eg, "<name>")
|
|
|
|
kubeName string
|
2016-10-24 12:01:01 +00:00
|
|
|
logDir string
|
|
|
|
labels fields.Set
|
|
|
|
annotations map[string]string
|
|
|
|
infraContainer *oci.Container
|
2017-04-19 18:55:17 +00:00
|
|
|
containers oci.ContainerStorer
|
2016-10-24 12:01:01 +00:00
|
|
|
processLabel string
|
|
|
|
mountLabel string
|
2016-11-23 17:16:21 +00:00
|
|
|
netns *sandboxNetNs
|
2016-10-24 12:01:01 +00:00
|
|
|
metadata *pb.PodSandboxMetadata
|
2016-12-08 23:32:17 +00:00
|
|
|
shmPath string
|
2016-12-13 08:34:55 +00:00
|
|
|
cgroupParent string
|
2017-02-21 10:51:16 +00:00
|
|
|
privileged bool
|
2017-03-24 14:28:14 +00:00
|
|
|
resolvPath string
|
2017-03-29 23:11:57 +00:00
|
|
|
hostname string
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 22:29:41 +00:00
|
|
|
const (
|
2017-05-04 16:41:15 +00:00
|
|
|
defaultShmSize = 64 * 1024 * 1024
|
|
|
|
nsRunDir = "/var/run/netns"
|
|
|
|
podInfraCommand = "/pause"
|
2016-09-20 22:29:41 +00:00
|
|
|
)
|
|
|
|
|
2016-12-07 16:38:36 +00:00
|
|
|
var (
|
2016-11-23 17:16:21 +00:00
|
|
|
errSandboxIDEmpty = errors.New("PodSandboxId should not be empty")
|
|
|
|
errSandboxClosedNetNS = errors.New("PodSandbox networking namespace is closed")
|
2016-12-07 16:38:36 +00:00
|
|
|
)
|
|
|
|
|
2016-09-19 07:21:14 +00:00
|
|
|
func (s *sandbox) addContainer(c *oci.Container) {
|
|
|
|
s.containers.Add(c.Name(), c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) getContainer(name string) *oci.Container {
|
|
|
|
return s.containers.Get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) removeContainer(c *oci.Container) {
|
|
|
|
s.containers.Delete(c.Name())
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:16:21 +00:00
|
|
|
func (s *sandbox) netNs() ns.NetNS {
|
|
|
|
if s.netns == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.netns.ns
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) netNsPath() string {
|
|
|
|
if s.netns == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2016-12-12 15:37:03 +00:00
|
|
|
return s.netns.symlink.Name()
|
2016-11-23 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) netNsCreate() error {
|
|
|
|
if s.netns != nil {
|
|
|
|
return fmt.Errorf("net NS already created")
|
|
|
|
}
|
|
|
|
|
|
|
|
netNS, err := ns.NewNS()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.netns = &sandboxNetNs{
|
2016-12-13 08:34:55 +00:00
|
|
|
ns: netNS,
|
2016-11-23 17:16:21 +00:00
|
|
|
closed: false,
|
|
|
|
}
|
|
|
|
|
2016-12-12 15:37:03 +00:00
|
|
|
if err := s.netns.symlinkCreate(s.name); err != nil {
|
|
|
|
logrus.Warnf("Could not create nentns symlink %v", err)
|
|
|
|
|
2017-03-27 17:14:11 +00:00
|
|
|
if err1 := s.netns.ns.Close(); err1 != nil {
|
|
|
|
return err1
|
2016-12-12 15:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:16:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sandbox) netNsRemove() error {
|
|
|
|
if s.netns == nil {
|
|
|
|
logrus.Warn("no networking namespace")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s.netns.Lock()
|
|
|
|
defer s.netns.Unlock()
|
|
|
|
|
|
|
|
if s.netns.closed {
|
|
|
|
// netNsRemove() can be called multiple
|
|
|
|
// times without returning an error.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-12 15:37:03 +00:00
|
|
|
if err := s.netns.symlinkRemove(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:16:21 +00:00
|
|
|
if err := s.netns.ns.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-14 18:47:05 +00:00
|
|
|
if s.netns.restored {
|
|
|
|
if err := unix.Unmount(s.netns.ns.Path(), unix.MNT_DETACH); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(s.netns.ns.Path()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:16:21 +00:00
|
|
|
s.netns.closed = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-09-30 22:23:38 +00:00
|
|
|
func (s *Server) generatePodIDandName(name string, namespace string, attempt uint32) (string, string, error) {
|
2016-09-20 08:27:11 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
id = stringid.GenerateNonCryptoID()
|
|
|
|
)
|
2016-09-22 23:02:46 +00:00
|
|
|
if namespace == "" {
|
2017-05-04 16:41:15 +00:00
|
|
|
return "", "", fmt.Errorf("cannot generate pod ID without namespace")
|
2016-09-22 23:02:46 +00:00
|
|
|
}
|
2016-09-30 22:23:38 +00:00
|
|
|
|
|
|
|
if name, err = s.reservePodName(id, fmt.Sprintf("%s-%s-%v", namespace, name, attempt)); err != nil {
|
2016-09-20 08:27:11 +00:00
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
return id, name, err
|
|
|
|
}
|
|
|
|
|
2017-02-03 14:41:28 +00:00
|
|
|
func (s *Server) getPodSandboxFromRequest(podSandboxID string) (*sandbox, error) {
|
|
|
|
if podSandboxID == "" {
|
2016-12-07 16:38:36 +00:00
|
|
|
return nil, errSandboxIDEmpty
|
2016-10-06 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 14:41:28 +00:00
|
|
|
sandboxID, err := s.podIDIndex.Get(podSandboxID)
|
2016-10-06 22:32:01 +00:00
|
|
|
if err != nil {
|
2017-02-03 14:41:28 +00:00
|
|
|
return nil, fmt.Errorf("PodSandbox with ID starting with %s not found: %v", podSandboxID, err)
|
2016-10-06 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sb := s.getSandbox(sandboxID)
|
|
|
|
if sb == nil {
|
2016-10-18 14:48:33 +00:00
|
|
|
return nil, fmt.Errorf("specified pod sandbox not found: %s", sandboxID)
|
2016-10-06 22:32:01 +00:00
|
|
|
}
|
|
|
|
return sb, nil
|
|
|
|
}
|