summaryrefslogtreecommitdiff
path: root/libpod/container_linux.go
blob: 2330f27a745e90de805dbb89f042602dd17d9b7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// +build linux

package libpod

import (
	"github.com/containernetworking/plugins/pkg/ns"
	"github.com/sirupsen/logrus"
)

type containerPlatformState struct {

	// NetNSPath is the path of the container's network namespace
	// Will only be set if config.CreateNetNS is true, or the container was
	// told to join another container's network namespace
	NetNS ns.NetNS `json:"-"`
}

func (ctr *Container) setNamespace(netNSPath string, newState *containerState) error {
	if netNSPath != "" {
		// Check if the container's old state has a good netns
		if ctr.state.NetNS != nil && netNSPath == ctr.state.NetNS.Path() {
			newState.NetNS = ctr.state.NetNS
		} else {
			// Tear down the existing namespace
			if err := ctr.runtime.teardownNetNS(ctr); err != nil {
				logrus.Warnf(err.Error())
			}

			// Open the new network namespace
			ns, err := joinNetNS(netNSPath)
			if err == nil {
				newState.NetNS = ns
			} else {
				logrus.Errorf("error joining network namespace for container %s", ctr.ID())
				ctr.valid = false
			}
		}
	} else {
		// The container no longer has a network namespace
		// Tear down the old one
		if err := ctr.runtime.teardownNetNS(ctr); err != nil {
			logrus.Warnf(err.Error())
		}
	}
	return nil
}

func (ctr *Container) setNamespaceStatePath() string {
	if ctr.state.NetNS != nil {
		return ctr.state.NetNS.Path()
	}
	return ""
}