diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2017-12-06 15:54:59 -0500 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2017-12-14 23:59:21 +0000 |
commit | 0ff92f8e20edb46eb8a9d82b929e153bcdaa3044 (patch) | |
tree | 14289c5dea9b738004837144ec6c5045d2f4789d /libpod/sql_state.go | |
parent | 824a648fcb87c112fb498db94b8e39a84ba649bd (diff) | |
download | podman-0ff92f8e20edb46eb8a9d82b929e153bcdaa3044.tar.gz podman-0ff92f8e20edb46eb8a9d82b929e153bcdaa3044.tar.bz2 podman-0ff92f8e20edb46eb8a9d82b929e153bcdaa3044.zip |
Add network namespaces to SQL state
Signed-off-by: Matthew Heon <matthew.heon@gmail.com>
Closes: #109
Approved by: mheon
Diffstat (limited to 'libpod/sql_state.go')
-rw-r--r-- | libpod/sql_state.go | 63 |
1 files changed, 54 insertions, 9 deletions
diff --git a/libpod/sql_state.go b/libpod/sql_state.go index d0969a783..97df749e9 100644 --- a/libpod/sql_state.go +++ b/libpod/sql_state.go @@ -15,7 +15,7 @@ import ( // DBSchema is the current DB schema version // 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 type SQLState struct { @@ -151,7 +151,8 @@ func (s *SQLState) Container(id string) (*Container, error) { containerState.FinishedTime, containerState.ExitCode, containerState.OomKilled, - containerState.Pid + containerState.Pid, + containerState.NetNSPath FROM containers INNER JOIN containerState ON containers.Id = containerState.Id @@ -186,7 +187,8 @@ func (s *SQLState) LookupContainer(idOrName string) (*Container, error) { containerState.FinishedTime, containerState.ExitCode, containerState.OomKilled, - containerState.Pid + containerState.Pid, + containerState.NetNSPath FROM containers INNER JOIN containerState ON containers.Id = containerState.Id @@ -270,7 +272,7 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) { ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? );` 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()) } + netNSPath := "" + if ctr.state.NetNS != nil { + netNSPath = ctr.state.NetNS.Path() + } + tx, err := s.db.Begin() if err != nil { return errors.Wrapf(err, "error beginning database transaction") @@ -342,7 +349,8 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) { timeToSQL(ctr.state.FinishedTime), ctr.state.ExitCode, boolToSQL(ctr.state.OOMKilled), - ctr.state.PID) + ctr.state.PID, + netNSPath) if err != nil { 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, ExitCode, OomKilled, - Pid + Pid, + NetNSPath FROM containerState WHERE ID=?;` var ( @@ -394,6 +403,7 @@ func (s *SQLState) UpdateContainer(ctr *Container) error { exitCode int32 oomKilled int pid int + netNSPath string ) if !s.valid { @@ -414,7 +424,8 @@ func (s *SQLState) UpdateContainer(ctr *Container) error { &finishedTimeString, &exitCode, &oomKilled, - &pid) + &pid, + &netNSPath) if err != nil { // The container may not exist in the database if err == sql.ErrNoRows { @@ -453,6 +464,32 @@ func (s *SQLState) UpdateContainer(ctr *Container) error { } 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 ctr.state = newState @@ -470,9 +507,15 @@ func (s *SQLState) SaveContainer(ctr *Container) error { FinishedTime=?, ExitCode=?, OomKilled=?, - Pid=? + Pid=?, + NetNSPath=? WHERE Id=?;` + netNSPath := "" + if ctr.state.NetNS != nil { + netNSPath = ctr.state.NetNS.Path() + } + if !s.valid { return ErrDBClosed } @@ -504,6 +547,7 @@ func (s *SQLState) SaveContainer(ctr *Container) error { ctr.state.ExitCode, boolToSQL(ctr.state.OOMKilled), ctr.state.PID, + netNSPath, ctr.ID()) if err != nil { 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.ExitCode, containerState.OomKilled, - containerState.Pid + containerState.Pid, + containerState.NetNSPath FROM containers INNER JOIN containerState ON containers.Id = containerState.Id |