2017-07-19 19:03:22 +00:00
|
|
|
package sandbox
|
2016-09-19 07:21:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-09-05 20:10:42 +00:00
|
|
|
"github.com/containernetworking/plugins/pkg/ns"
|
2017-05-21 13:47:01 +00:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
|
|
|
"github.com/docker/docker/pkg/symlink"
|
2016-09-26 23:55:12 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/oci"
|
2017-08-05 11:40:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
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"
|
2017-08-04 11:13:19 +00:00
|
|
|
pb "k8s.io/kubernetes/pkg/kubelet/apis/cri/v1alpha1/runtime"
|
2017-06-15 20:56:17 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubelet/network/hostport"
|
2016-09-19 07:21:14 +00:00
|
|
|
)
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
// NetNs handles data pertaining a network namespace
|
|
|
|
type NetNs struct {
|
2016-11-23 17:16:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
func (ns *NetNs) symlinkCreate(name string) error {
|
2016-12-12 15:37:03 +00:00
|
|
|
b := make([]byte, 4)
|
|
|
|
_, randErr := rand.Reader.Read(b)
|
|
|
|
if randErr != nil {
|
|
|
|
return randErr
|
|
|
|
}
|
|
|
|
|
|
|
|
nsName := fmt.Sprintf("%s-%x", name, b)
|
2017-07-19 19:03:22 +00:00
|
|
|
symlinkPath := filepath.Join(NsRunDir, nsName)
|
2016-12-12 15:37:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
func (ns *NetNs) symlinkRemove() error {
|
2016-12-12 15:37:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
// NetNsGet returns the NetNs associated with the given nspath and name
|
|
|
|
func NetNsGet(nspath, name string) (*NetNs, error) {
|
2016-11-23 17:16:21 +00:00
|
|
|
if err := ns.IsNSorErr(nspath); err != nil {
|
2017-07-19 19:03:22 +00:00
|
|
|
return nil, ErrClosedNetNS
|
2016-11-23 17:16:21 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
netNs := &NetNs{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
|
|
|
}
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
// HostNetNsPath returns the current network namespace for the host
|
|
|
|
func HostNetNsPath() (string, error) {
|
2016-11-23 17:16:21 +00:00
|
|
|
netNS, err := ns.GetCurrentNS()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer netNS.Close()
|
|
|
|
return netNS.Path(), nil
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:35:15 +00:00
|
|
|
// Sandbox contains data surrounding kubernetes sandboxes on the server
|
|
|
|
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
|
2017-07-19 19:03:22 +00:00
|
|
|
netns *NetNs
|
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-06-02 21:15:19 +00:00
|
|
|
trusted bool
|
2017-03-24 14:28:14 +00:00
|
|
|
resolvPath string
|
2017-09-13 08:28:41 +00:00
|
|
|
hostnamePath string
|
2017-03-29 23:11:57 +00:00
|
|
|
hostname string
|
2017-06-15 20:56:17 +00:00
|
|
|
portMappings []*hostport.PortMapping
|
2017-08-16 22:42:20 +00:00
|
|
|
stopped bool
|
2017-08-29 22:57:26 +00:00
|
|
|
// ipv4 or ipv6 cache
|
2017-09-27 18:46:31 +00:00
|
|
|
ip string
|
|
|
|
seccompProfilePath string
|
2016-09-19 07:21:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 22:29:41 +00:00
|
|
|
const (
|
2017-07-19 19:03:22 +00:00
|
|
|
// DefaultShmSize is the default shm size
|
|
|
|
DefaultShmSize = 64 * 1024 * 1024
|
|
|
|
// NsRunDir is the default directory in which running network namespaces
|
|
|
|
// are stored
|
|
|
|
NsRunDir = "/var/run/netns"
|
|
|
|
// PodInfraCommand is the default command when starting a pod infrastructure
|
|
|
|
// container
|
|
|
|
PodInfraCommand = "/pause"
|
2016-09-20 22:29:41 +00:00
|
|
|
)
|
|
|
|
|
2016-12-07 16:38:36 +00:00
|
|
|
var (
|
2017-07-19 19:03:22 +00:00
|
|
|
// ErrIDEmpty is the erro returned when the id of the sandbox is empty
|
|
|
|
ErrIDEmpty = errors.New("PodSandboxId should not be empty")
|
|
|
|
// ErrClosedNetNS is the error returned when the network namespace of the
|
|
|
|
// sandbox is closed
|
|
|
|
ErrClosedNetNS = errors.New("PodSandbox networking namespace is closed")
|
2016-12-07 16:38:36 +00:00
|
|
|
)
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
// New creates and populates a new pod sandbox
|
2017-07-18 20:35:15 +00:00
|
|
|
// New sandboxes have no containers, no infra container, and no network namespaces associated with them
|
|
|
|
// An infra container must be attached before the sandbox is added to the state
|
2017-07-19 19:03:22 +00:00
|
|
|
func New(id, namespace, name, kubeName, logDir string, labels, annotations map[string]string, processLabel, mountLabel string, metadata *pb.PodSandboxMetadata, shmPath, cgroupParent string, privileged, trusted bool, resolvPath, hostname string, portMappings []*hostport.PortMapping) (*Sandbox, error) {
|
2017-07-18 20:35:15 +00:00
|
|
|
sb := new(Sandbox)
|
|
|
|
sb.id = id
|
|
|
|
sb.namespace = namespace
|
|
|
|
sb.name = name
|
|
|
|
sb.kubeName = kubeName
|
|
|
|
sb.logDir = logDir
|
|
|
|
sb.labels = labels
|
|
|
|
sb.annotations = annotations
|
|
|
|
sb.containers = oci.NewMemoryStore()
|
|
|
|
sb.processLabel = processLabel
|
|
|
|
sb.mountLabel = mountLabel
|
|
|
|
sb.metadata = metadata
|
|
|
|
sb.shmPath = shmPath
|
|
|
|
sb.cgroupParent = cgroupParent
|
|
|
|
sb.privileged = privileged
|
|
|
|
sb.trusted = trusted
|
|
|
|
sb.resolvPath = resolvPath
|
|
|
|
sb.hostname = hostname
|
|
|
|
sb.portMappings = portMappings
|
|
|
|
|
|
|
|
return sb, nil
|
|
|
|
}
|
|
|
|
|
2017-09-27 18:46:31 +00:00
|
|
|
// SetSeccompProfilePath sets the seccomp profile path
|
|
|
|
func (s *Sandbox) SetSeccompProfilePath(pp string) {
|
|
|
|
s.seccompProfilePath = pp
|
|
|
|
}
|
|
|
|
|
|
|
|
// SeccompProfilePath returns the seccomp profile path
|
|
|
|
func (s *Sandbox) SeccompProfilePath() string {
|
|
|
|
return s.seccompProfilePath
|
|
|
|
}
|
|
|
|
|
2017-08-29 22:57:26 +00:00
|
|
|
// AddIP stores the ip in the sandbox
|
|
|
|
func (s *Sandbox) AddIP(ip string) {
|
|
|
|
s.ip = ip
|
|
|
|
}
|
|
|
|
|
|
|
|
// IP returns the ip of the sandbox
|
|
|
|
func (s *Sandbox) IP() string {
|
|
|
|
return s.ip
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:35:15 +00:00
|
|
|
// ID returns the id of the sandbox
|
|
|
|
func (s *Sandbox) ID() string {
|
|
|
|
return s.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// Namespace returns the namespace for the sandbox
|
|
|
|
func (s *Sandbox) Namespace() string {
|
|
|
|
return s.namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the sandbox
|
|
|
|
func (s *Sandbox) Name() string {
|
|
|
|
return s.name
|
|
|
|
}
|
|
|
|
|
|
|
|
// KubeName returns the kubernetes name for the sandbox
|
|
|
|
func (s *Sandbox) KubeName() string {
|
|
|
|
return s.kubeName
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogDir returns the location of the logging directory for the sandbox
|
|
|
|
func (s *Sandbox) LogDir() string {
|
|
|
|
return s.logDir
|
|
|
|
}
|
|
|
|
|
|
|
|
// Labels returns the labels associated with the sandbox
|
|
|
|
func (s *Sandbox) Labels() fields.Set {
|
|
|
|
return s.labels
|
|
|
|
}
|
|
|
|
|
|
|
|
// Annotations returns a list of annotations for the sandbox
|
|
|
|
func (s *Sandbox) Annotations() map[string]string {
|
|
|
|
return s.annotations
|
|
|
|
}
|
|
|
|
|
|
|
|
// InfraContainer returns the infrastructure container for the sandbox
|
|
|
|
func (s *Sandbox) InfraContainer() *oci.Container {
|
|
|
|
return s.infraContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Containers returns the ContainerStorer that contains information on all
|
|
|
|
// of the containers in the sandbox
|
|
|
|
func (s *Sandbox) Containers() oci.ContainerStorer {
|
|
|
|
return s.containers
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessLabel returns the process label for the sandbox
|
|
|
|
func (s *Sandbox) ProcessLabel() string {
|
|
|
|
return s.processLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
// MountLabel returns the mount label for the sandbox
|
|
|
|
func (s *Sandbox) MountLabel() string {
|
|
|
|
return s.mountLabel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata returns a set of metadata about the sandbox
|
|
|
|
func (s *Sandbox) Metadata() *pb.PodSandboxMetadata {
|
|
|
|
return s.metadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShmPath returns the shm path of the sandbox
|
|
|
|
func (s *Sandbox) ShmPath() string {
|
|
|
|
return s.shmPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// CgroupParent returns the cgroup parent of the sandbox
|
|
|
|
func (s *Sandbox) CgroupParent() string {
|
|
|
|
return s.cgroupParent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Privileged returns whether or not the containers in the sandbox are
|
|
|
|
// privileged containers
|
|
|
|
func (s *Sandbox) Privileged() bool {
|
|
|
|
return s.privileged
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trusted returns whether or not the containers in the sandbox are trusted
|
|
|
|
func (s *Sandbox) Trusted() bool {
|
|
|
|
return s.trusted
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolvPath returns the resolv path for the sandbox
|
|
|
|
func (s *Sandbox) ResolvPath() string {
|
|
|
|
return s.resolvPath
|
|
|
|
}
|
|
|
|
|
2017-09-13 08:28:41 +00:00
|
|
|
// AddHostnamePath adds the hostname path to the sandbox
|
|
|
|
func (s *Sandbox) AddHostnamePath(hostname string) {
|
|
|
|
s.hostnamePath = hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
// HostnamePath retrieves the hostname path from a sandbox
|
|
|
|
func (s *Sandbox) HostnamePath() string {
|
|
|
|
return s.hostnamePath
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:35:15 +00:00
|
|
|
// Hostname returns the hsotname of the sandbox
|
|
|
|
func (s *Sandbox) Hostname() string {
|
|
|
|
return s.hostname
|
|
|
|
}
|
|
|
|
|
|
|
|
// PortMappings returns a list of port mappings between the host and the sandbox
|
|
|
|
func (s *Sandbox) PortMappings() []*hostport.PortMapping {
|
|
|
|
return s.portMappings
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddContainer adds a container to the sandbox
|
|
|
|
func (s *Sandbox) AddContainer(c *oci.Container) {
|
2016-09-19 07:21:14 +00:00
|
|
|
s.containers.Add(c.Name(), c)
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:35:15 +00:00
|
|
|
// GetContainer retrieves a container from the sandbox
|
|
|
|
func (s *Sandbox) GetContainer(name string) *oci.Container {
|
2016-09-19 07:21:14 +00:00
|
|
|
return s.containers.Get(name)
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:35:15 +00:00
|
|
|
// RemoveContainer deletes a container from the sandbox
|
|
|
|
func (s *Sandbox) RemoveContainer(c *oci.Container) {
|
2016-09-19 07:21:14 +00:00
|
|
|
s.containers.Delete(c.Name())
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:35:15 +00:00
|
|
|
// SetInfraContainer sets the infrastructure container of a sandbox
|
|
|
|
// Attempts to set the infrastructure container after one is already present will throw an error
|
|
|
|
func (s *Sandbox) SetInfraContainer(infraCtr *oci.Container) error {
|
|
|
|
if s.infraContainer != nil {
|
|
|
|
return fmt.Errorf("sandbox already has an infra container")
|
|
|
|
} else if infraCtr == nil {
|
|
|
|
return fmt.Errorf("must provide non-nil infra container")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.infraContainer = infraCtr
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
// RemoveInfraContainer removes the infrastructure container of a sandbox
|
|
|
|
func (s *Sandbox) RemoveInfraContainer() {
|
|
|
|
s.infraContainer = nil
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:35:15 +00:00
|
|
|
// NetNs retrieves the network namespace of the sandbox
|
|
|
|
// If the sandbox uses the host namespace, nil is returned
|
|
|
|
func (s *Sandbox) NetNs() ns.NetNS {
|
2016-11-23 17:16:21 +00:00
|
|
|
if s.netns == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.netns.ns
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:35:15 +00:00
|
|
|
// NetNsPath returns the path to the network namespace of the sandbox.
|
|
|
|
// If the sandbox uses the host namespace, nil is returned
|
|
|
|
func (s *Sandbox) NetNsPath() string {
|
2016-11-23 17:16:21 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-07-18 20:35:15 +00:00
|
|
|
// NetNsCreate creates a new network namespace for the sandbox
|
|
|
|
func (s *Sandbox) NetNsCreate() error {
|
2016-11-23 17:16:21 +00:00
|
|
|
if s.netns != nil {
|
|
|
|
return fmt.Errorf("net NS already created")
|
|
|
|
}
|
|
|
|
|
|
|
|
netNS, err := ns.NewNS()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
s.netns = &NetNs{
|
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
|
|
|
|
}
|
|
|
|
|
2017-08-16 22:42:20 +00:00
|
|
|
// SetStopped sets the sandbox state to stopped.
|
|
|
|
// This should be set after a stop operation succeeds
|
|
|
|
// so that subsequent stops can return fast.
|
|
|
|
func (s *Sandbox) SetStopped() {
|
|
|
|
s.stopped = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stopped returns whether the sandbox state has been
|
|
|
|
// set to stopped.
|
|
|
|
func (s *Sandbox) Stopped() bool {
|
|
|
|
return s.stopped
|
|
|
|
}
|
|
|
|
|
2017-07-18 20:35:15 +00:00
|
|
|
// NetNsJoin attempts to join the sandbox to an existing network namespace
|
|
|
|
// This will fail if the sandbox is already part of a network namespace
|
|
|
|
func (s *Sandbox) NetNsJoin(nspath, name string) error {
|
|
|
|
if s.netns != nil {
|
|
|
|
return fmt.Errorf("sandbox already has a network namespace, cannot join another")
|
|
|
|
}
|
|
|
|
|
2017-07-19 19:03:22 +00:00
|
|
|
netNS, err := NetNsGet(nspath, name)
|
2017-07-18 20:35:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.netns = netNS
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetNsRemove removes the network namespace associated with the sandbox
|
|
|
|
func (s *Sandbox) NetNsRemove() error {
|
2016-11-23 17:16:21 +00:00
|
|
|
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 {
|
2017-05-21 13:47:01 +00:00
|
|
|
// we got namespaces in the form of
|
|
|
|
// /var/run/netns/cni-0d08effa-06eb-a963-f51a-e2b0eceffc5d
|
|
|
|
// but /var/run on most system is symlinked to /run so we first resolve
|
|
|
|
// the symlink and then try and see if it's mounted
|
|
|
|
fp, err := symlink.FollowSymlinkInScope(s.netns.ns.Path(), "/")
|
|
|
|
if err != nil {
|
2016-12-14 18:47:05 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-05-21 13:47:01 +00:00
|
|
|
if mounted, err := mount.Mounted(fp); err == nil && mounted {
|
|
|
|
if err := unix.Unmount(fp, unix.MNT_DETACH); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-12-14 18:47:05 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|