mirror of
https://github.com/containers/podman.git
synced 2025-05-30 23:17:20 +08:00
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:
@ -12,6 +12,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/containerd/cgroups"
|
"github.com/containerd/cgroups"
|
||||||
|
"github.com/containernetworking/plugins/pkg/ns"
|
||||||
"github.com/containers/storage"
|
"github.com/containers/storage"
|
||||||
"github.com/containers/storage/pkg/archive"
|
"github.com/containers/storage/pkg/archive"
|
||||||
"github.com/cri-o/ocicni/pkg/ocicni"
|
"github.com/cri-o/ocicni/pkg/ocicni"
|
||||||
@ -100,6 +101,10 @@ type containerRuntimeInfo struct {
|
|||||||
OOMKilled bool `json:"oomKilled,omitempty"`
|
OOMKilled bool `json:"oomKilled,omitempty"`
|
||||||
// PID is the PID of a running container
|
// PID is the PID of a running container
|
||||||
PID int `json:"pid,omitempty"`
|
PID int `json:"pid,omitempty"`
|
||||||
|
// 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
|
||||||
// TODO: Save information about image used in container if one is used
|
// TODO: Save information about image used in container if one is used
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,11 +3,12 @@ package libpod
|
|||||||
import (
|
import (
|
||||||
"github.com/containernetworking/plugins/pkg/ns"
|
"github.com/containernetworking/plugins/pkg/ns"
|
||||||
"github.com/cri-o/ocicni/pkg/ocicni"
|
"github.com/cri-o/ocicni/pkg/ocicni"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Get an OCICNI network config
|
// 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{
|
return ocicni.PodNetwork{
|
||||||
Name: name,
|
Name: name,
|
||||||
Namespace: name, // TODO is there something else we should put here? We don't know about Kube namespaces
|
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
|
// Create and configure a new network namespace for a container
|
||||||
func (r *Runtime) createNetNS(id, name string, ports []ocicni.PortMapping) (n ns.NetNS, err error) {
|
func (r *Runtime) createNetNS(ctr *Container) (err error) {
|
||||||
ns, err := ns.NewNS()
|
ns, err := ns.NewNS()
|
||||||
if err != nil {
|
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() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err2 := ns.Close(); err2 != 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 {
|
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
|
// TODO hostport mappings for forwarded ports
|
||||||
|
|
||||||
return ns, nil
|
ctr.state.NetNS = ns
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Join an existing network namespace
|
// Join an existing network namespace
|
||||||
@ -53,13 +56,25 @@ func joinNetNS(path string) (ns.NetNS, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Tear down a network namespace
|
// Tear down a network namespace
|
||||||
func (r *Runtime) teardownNetNS(id, name string, ports []ocicni.PortMapping, ns ns.NetNS) error {
|
func (r *Runtime) teardownNetNS(ctr *Container) error {
|
||||||
// TODO hostport mappings for forwarded ports should be undone
|
if ctr.state.NetNS == nil {
|
||||||
podNetwork := getPodNetwork(id, name, ns.Path(), ports)
|
// The container has no network namespace, we're set
|
||||||
|
return nil
|
||||||
if err := r.netPlugin.TearDownPod(podNetwork); err != nil {
|
|
||||||
return errors.Wrapf(err, "failed to remove network namespace %s", id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
// DBSchema is the current DB schema version
|
// DBSchema is the current DB schema version
|
||||||
// Increments every time a change is made to the database's tables
|
// Increments every time a change is made to the database's tables
|
||||||
const DBSchema = 3
|
const DBSchema = 4
|
||||||
|
|
||||||
// SQLState is a state implementation backed by a persistent SQLite3 database
|
// SQLState is a state implementation backed by a persistent SQLite3 database
|
||||||
type SQLState struct {
|
type SQLState struct {
|
||||||
@ -151,7 +151,8 @@ func (s *SQLState) Container(id string) (*Container, error) {
|
|||||||
containerState.FinishedTime,
|
containerState.FinishedTime,
|
||||||
containerState.ExitCode,
|
containerState.ExitCode,
|
||||||
containerState.OomKilled,
|
containerState.OomKilled,
|
||||||
containerState.Pid
|
containerState.Pid,
|
||||||
|
containerState.NetNSPath
|
||||||
FROM containers
|
FROM containers
|
||||||
INNER JOIN
|
INNER JOIN
|
||||||
containerState ON containers.Id = containerState.Id
|
containerState ON containers.Id = containerState.Id
|
||||||
@ -186,7 +187,8 @@ func (s *SQLState) LookupContainer(idOrName string) (*Container, error) {
|
|||||||
containerState.FinishedTime,
|
containerState.FinishedTime,
|
||||||
containerState.ExitCode,
|
containerState.ExitCode,
|
||||||
containerState.OomKilled,
|
containerState.OomKilled,
|
||||||
containerState.Pid
|
containerState.Pid,
|
||||||
|
containerState.NetNSPath
|
||||||
FROM containers
|
FROM containers
|
||||||
INNER JOIN
|
INNER JOIN
|
||||||
containerState ON containers.Id = containerState.Id
|
containerState ON containers.Id = containerState.Id
|
||||||
@ -270,7 +272,7 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) {
|
|||||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
||||||
);`
|
);`
|
||||||
addCtrState = `INSERT INTO containerState VALUES (
|
addCtrState = `INSERT INTO containerState VALUES (
|
||||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
||||||
);`
|
);`
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -297,6 +299,11 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) {
|
|||||||
return errors.Wrapf(err, "error marshaling container %s port mappings to JSON", ctr.ID())
|
return errors.Wrapf(err, "error marshaling container %s port mappings to JSON", ctr.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
netNSPath := ""
|
||||||
|
if ctr.state.NetNS != nil {
|
||||||
|
netNSPath = ctr.state.NetNS.Path()
|
||||||
|
}
|
||||||
|
|
||||||
tx, err := s.db.Begin()
|
tx, err := s.db.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error beginning database transaction")
|
return errors.Wrapf(err, "error beginning database transaction")
|
||||||
@ -342,7 +349,8 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) {
|
|||||||
timeToSQL(ctr.state.FinishedTime),
|
timeToSQL(ctr.state.FinishedTime),
|
||||||
ctr.state.ExitCode,
|
ctr.state.ExitCode,
|
||||||
boolToSQL(ctr.state.OOMKilled),
|
boolToSQL(ctr.state.OOMKilled),
|
||||||
ctr.state.PID)
|
ctr.state.PID,
|
||||||
|
netNSPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error adding container %s state to database", ctr.ID())
|
return errors.Wrapf(err, "error adding container %s state to database", ctr.ID())
|
||||||
}
|
}
|
||||||
@ -381,7 +389,8 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
|
|||||||
FinishedTime,
|
FinishedTime,
|
||||||
ExitCode,
|
ExitCode,
|
||||||
OomKilled,
|
OomKilled,
|
||||||
Pid
|
Pid,
|
||||||
|
NetNSPath
|
||||||
FROM containerState WHERE ID=?;`
|
FROM containerState WHERE ID=?;`
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -394,6 +403,7 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
|
|||||||
exitCode int32
|
exitCode int32
|
||||||
oomKilled int
|
oomKilled int
|
||||||
pid int
|
pid int
|
||||||
|
netNSPath string
|
||||||
)
|
)
|
||||||
|
|
||||||
if !s.valid {
|
if !s.valid {
|
||||||
@ -414,7 +424,8 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
|
|||||||
&finishedTimeString,
|
&finishedTimeString,
|
||||||
&exitCode,
|
&exitCode,
|
||||||
&oomKilled,
|
&oomKilled,
|
||||||
&pid)
|
&pid,
|
||||||
|
&netNSPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// The container may not exist in the database
|
// The container may not exist in the database
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
@ -453,6 +464,32 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
|
|||||||
}
|
}
|
||||||
newState.FinishedTime = finishedTime
|
newState.FinishedTime = finishedTime
|
||||||
|
|
||||||
|
// Do we need to replace the container's netns?
|
||||||
|
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 := s.runtime.teardownNetNS(ctr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open the new network namespace
|
||||||
|
ns, err := joinNetNS(netNSPath)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrapf(err, "error joining network namespace for container %s", ctr.ID())
|
||||||
|
}
|
||||||
|
newState.NetNS = ns
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// The container no longer has a network namespace
|
||||||
|
// Tear down the old one
|
||||||
|
if err := s.runtime.teardownNetNS(ctr); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// New state compiled successfully, swap it into the current state
|
// New state compiled successfully, swap it into the current state
|
||||||
ctr.state = newState
|
ctr.state = newState
|
||||||
|
|
||||||
@ -470,9 +507,15 @@ func (s *SQLState) SaveContainer(ctr *Container) error {
|
|||||||
FinishedTime=?,
|
FinishedTime=?,
|
||||||
ExitCode=?,
|
ExitCode=?,
|
||||||
OomKilled=?,
|
OomKilled=?,
|
||||||
Pid=?
|
Pid=?,
|
||||||
|
NetNSPath=?
|
||||||
WHERE Id=?;`
|
WHERE Id=?;`
|
||||||
|
|
||||||
|
netNSPath := ""
|
||||||
|
if ctr.state.NetNS != nil {
|
||||||
|
netNSPath = ctr.state.NetNS.Path()
|
||||||
|
}
|
||||||
|
|
||||||
if !s.valid {
|
if !s.valid {
|
||||||
return ErrDBClosed
|
return ErrDBClosed
|
||||||
}
|
}
|
||||||
@ -504,6 +547,7 @@ func (s *SQLState) SaveContainer(ctr *Container) error {
|
|||||||
ctr.state.ExitCode,
|
ctr.state.ExitCode,
|
||||||
boolToSQL(ctr.state.OOMKilled),
|
boolToSQL(ctr.state.OOMKilled),
|
||||||
ctr.state.PID,
|
ctr.state.PID,
|
||||||
|
netNSPath,
|
||||||
ctr.ID())
|
ctr.ID())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error updating container %s state in database", ctr.ID())
|
return errors.Wrapf(err, "error updating container %s state in database", ctr.ID())
|
||||||
@ -593,7 +637,8 @@ func (s *SQLState) AllContainers() ([]*Container, error) {
|
|||||||
containerState.FinishedTime,
|
containerState.FinishedTime,
|
||||||
containerState.ExitCode,
|
containerState.ExitCode,
|
||||||
containerState.OomKilled,
|
containerState.OomKilled,
|
||||||
containerState.Pid
|
containerState.Pid,
|
||||||
|
containerState.NetNSPath
|
||||||
FROM containers
|
FROM containers
|
||||||
INNER JOIN
|
INNER JOIN
|
||||||
containerState ON containers.Id = containerState.Id
|
containerState ON containers.Id = containerState.Id
|
||||||
|
@ -200,6 +200,7 @@ func prepareDB(db *sql.DB) (err error) {
|
|||||||
ExitCode INTEGER NOT NULL,
|
ExitCode INTEGER NOT NULL,
|
||||||
OomKilled INTEGER NOT NULL,
|
OomKilled INTEGER NOT NULL,
|
||||||
Pid INTEGER NOT NULL,
|
Pid INTEGER NOT NULL,
|
||||||
|
NetNSPath TEXT NOT NULL,
|
||||||
CHECK (State>0),
|
CHECK (State>0),
|
||||||
CHECK (OomKilled IN (0, 1)),
|
CHECK (OomKilled IN (0, 1)),
|
||||||
FOREIGN KEY (Id) REFERENCES containers(Id) DEFERRABLE INITIALLY DEFERRED
|
FOREIGN KEY (Id) REFERENCES containers(Id) DEFERRABLE INITIALLY DEFERRED
|
||||||
@ -296,6 +297,7 @@ func ctrFromScannable(row scannable, runtime *Runtime, specsDir string, lockDir
|
|||||||
exitCode int32
|
exitCode int32
|
||||||
oomKilled int
|
oomKilled int
|
||||||
pid int
|
pid int
|
||||||
|
netNSPath string
|
||||||
)
|
)
|
||||||
|
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
@ -323,7 +325,8 @@ func ctrFromScannable(row scannable, runtime *Runtime, specsDir string, lockDir
|
|||||||
&finishedTimeString,
|
&finishedTimeString,
|
||||||
&exitCode,
|
&exitCode,
|
||||||
&oomKilled,
|
&oomKilled,
|
||||||
&pid)
|
&pid,
|
||||||
|
&netNSPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return nil, ErrNoSuchCtr
|
return nil, ErrNoSuchCtr
|
||||||
@ -394,6 +397,15 @@ func ctrFromScannable(row scannable, runtime *Runtime, specsDir string, lockDir
|
|||||||
}
|
}
|
||||||
ctr.state.FinishedTime = finishedTime
|
ctr.state.FinishedTime = finishedTime
|
||||||
|
|
||||||
|
// Join the network namespace, if there is one
|
||||||
|
if netNSPath != "" {
|
||||||
|
netNS, err := joinNetNS(netNSPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "error joining network namespace for container %s", id)
|
||||||
|
}
|
||||||
|
ctr.state.NetNS = netNS
|
||||||
|
}
|
||||||
|
|
||||||
ctr.valid = true
|
ctr.valid = true
|
||||||
ctr.runtime = runtime
|
ctr.runtime = runtime
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user