Add location in DB for saving files to bind mount in

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>

Closes: #462
Approved by: baude
This commit is contained in:
Matthew Heon
2018-03-07 12:24:56 -05:00
committed by Atomic Bot
parent fcc3663355
commit c657511bce
4 changed files with 54 additions and 10 deletions

View File

@ -148,6 +148,12 @@ type containerState struct {
// Only populated if we created a network namespace for the container, // Only populated if we created a network namespace for the container,
// and the network namespace is currently active // and the network namespace is currently active
Routes []*types.Route `json:"routes,omitempty"` Routes []*types.Route `json:"routes,omitempty"`
// BindMounts contains files that will be bind-mounted into the
// container when it is mounted.
// These include /etc/hosts and /etc/resolv.conf
// This maps the path the file will be mounted to in the container to
// the path of the file on disk outside the container
BindMounts map[string]string `json:"bindMounts,omitempty"`
} }
// ExecSession contains information on an active exec session // ExecSession contains information on an active exec session

View File

@ -16,7 +16,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 = 13 const DBSchema = 14
// 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 {
@ -105,7 +105,8 @@ func (s *SQLState) Refresh() (err error) {
NetNSPath=?, NetNSPath=?,
ExecSessions=?, ExecSessions=?,
IPs=?, IPs=?,
Routes=?;` Routes=?,
BindMounts=?;`
if !s.valid { if !s.valid {
return ErrDBClosed return ErrDBClosed
@ -136,7 +137,8 @@ func (s *SQLState) Refresh() (err error) {
"", "",
"{}", "{}",
"[]", "[]",
"[]") "[]",
"{}")
if err != nil { if err != nil {
return errors.Wrapf(err, "error refreshing database state") return errors.Wrapf(err, "error refreshing database state")
} }
@ -274,7 +276,8 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
NetNSPath, NetNSPath,
ExecSessions, ExecSessions,
IPs, IPs,
Routes Routes,
BindMounts
FROM containerState WHERE ID=?;` FROM containerState WHERE ID=?;`
var ( var (
@ -291,6 +294,7 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
execSessions string execSessions string
ipsJSON string ipsJSON string
routesJSON string routesJSON string
bindMountsJSON string
) )
if !s.valid { if !s.valid {
@ -315,7 +319,8 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
&netNSPath, &netNSPath,
&execSessions, &execSessions,
&ipsJSON, &ipsJSON,
&routesJSON) &routesJSON,
&bindMountsJSON)
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 {
@ -359,6 +364,12 @@ func (s *SQLState) UpdateContainer(ctr *Container) error {
newState.Routes = routes newState.Routes = routes
} }
bindMounts := make(map[string]string)
if err := json.Unmarshal([]byte(bindMountsJSON), &bindMounts); err != nil {
return errors.Wrapf(err, "error parsing container %s bind mounts JSON", ctr.ID())
}
newState.BindMounts = bindMounts
if newState.Mountpoint != "" { if newState.Mountpoint != "" {
newState.Mounted = true newState.Mounted = true
} }
@ -422,7 +433,8 @@ func (s *SQLState) SaveContainer(ctr *Container) (err error) {
NetNSPath=?, NetNSPath=?,
ExecSessions=?, ExecSessions=?,
IPs=?, IPs=?,
Routes=? Routes=?,
BindMounts=?
WHERE Id=?;` WHERE Id=?;`
if !ctr.valid { if !ctr.valid {
@ -449,6 +461,11 @@ func (s *SQLState) SaveContainer(ctr *Container) (err error) {
return errors.Wrapf(err, "error marshalling container %s routes to JSON", ctr.ID()) return errors.Wrapf(err, "error marshalling container %s routes to JSON", ctr.ID())
} }
bindMountsJSON, err := json.Marshal(ctr.state.BindMounts)
if err != nil {
return errors.Wrapf(err, "error marshalling container %s bind mounts to JSON", ctr.ID())
}
if !s.valid { if !s.valid {
return ErrDBClosed return ErrDBClosed
} }
@ -482,6 +499,7 @@ func (s *SQLState) SaveContainer(ctr *Container) (err error) {
execSessionsJSON, execSessionsJSON,
ipsJSON, ipsJSON,
routesJSON, routesJSON,
bindMountsJSON,
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())

View File

@ -36,7 +36,8 @@ const (
containerState.NetNSPath, containerState.NetNSPath,
containerState.ExecSessions, containerState.ExecSessions,
containerState.IPs, containerState.IPs,
containerState.Routes containerState.Routes,
containerState.BindMounts
FROM containers FROM containers
INNER JOIN INNER JOIN
containerState ON containers.Id = containerState.Id ` containerState ON containers.Id = containerState.Id `
@ -278,6 +279,7 @@ func prepareDB(db *sql.DB) (err error) {
ExecSessions TEXT NOT NULL, ExecSessions TEXT NOT NULL,
IPs TEXT NOT NULL, IPs TEXT NOT NULL,
Routes TEXT NOT NULL, Routes TEXT NOT NULL,
BindMounts TEXT NOT NULL,
CHECK (State>0), CHECK (State>0),
CHECK (OomKilled IN (0, 1)), CHECK (OomKilled IN (0, 1)),
@ -488,6 +490,7 @@ func (s *SQLState) ctrFromScannable(row scannable) (*Container, error) {
execSessions string execSessions string
ipsJSON string ipsJSON string
routesJSON string routesJSON string
bindMountsJSON string
) )
err := row.Scan( err := row.Scan(
@ -542,7 +545,8 @@ func (s *SQLState) ctrFromScannable(row scannable) (*Container, error) {
&netNSPath, &netNSPath,
&execSessions, &execSessions,
&ipsJSON, &ipsJSON,
&routesJSON) &routesJSON,
&bindMountsJSON)
if err != nil { if err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return nil, ErrNoSuchCtr return nil, ErrNoSuchCtr
@ -641,6 +645,12 @@ func (s *SQLState) ctrFromScannable(row scannable) (*Container, error) {
ctr.state.Routes = routes ctr.state.Routes = routes
} }
bindMounts := make(map[string]string)
if err := json.Unmarshal([]byte(bindMountsJSON), &bindMounts); err != nil {
return nil, errors.Wrapf(err, "error parsing container %s bind mounts JSON", id)
}
ctr.state.BindMounts = bindMounts
labels := make(map[string]string) labels := make(map[string]string)
if err := json.Unmarshal([]byte(labelsJSON), &labels); err != nil { if err := json.Unmarshal([]byte(labelsJSON), &labels); err != nil {
return nil, errors.Wrapf(err, "error parsing container %s labels JSON", id) return nil, errors.Wrapf(err, "error parsing container %s labels JSON", id)
@ -778,7 +788,7 @@ func (s *SQLState) addContainer(ctr *Container, pod *Pod) (err error) {
addCtrState = `INSERT INTO containerState VALUES ( addCtrState = `INSERT INTO containerState VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ? ?, ?, ?, ?, ?
);` );`
addRegistry = "INSERT INTO registry VALUES (?, ?);" addRegistry = "INSERT INTO registry VALUES (?, ?);"
checkCtrInPod = "SELECT 1 FROM containers WHERE Id=? AND Pod=?;" checkCtrInPod = "SELECT 1 FROM containers WHERE Id=? AND Pod=?;"
@ -835,6 +845,11 @@ func (s *SQLState) addContainer(ctr *Container, pod *Pod) (err error) {
return errors.Wrapf(err, "error marshalling container %s routes to JSON", ctr.ID()) return errors.Wrapf(err, "error marshalling container %s routes to JSON", ctr.ID())
} }
bindMountsJSON, err := json.Marshal(ctr.state.BindMounts)
if err != nil {
return errors.Wrapf(err, "error marshalling container %s bind mounts to JSON", ctr.ID())
}
netNSPath := "" netNSPath := ""
if ctr.state.NetNS != nil { if ctr.state.NetNS != nil {
netNSPath = ctr.state.NetNS.Path() netNSPath = ctr.state.NetNS.Path()
@ -959,7 +974,8 @@ func (s *SQLState) addContainer(ctr *Container, pod *Pod) (err error) {
netNSPath, netNSPath,
execSessionsJSON, execSessionsJSON,
ipsJSON, ipsJSON,
routesJSON) routesJSON,
bindMountsJSON)
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())
} }

View File

@ -67,6 +67,10 @@ func getTestContainer(id, name, locksDir string) (*Container, error) {
PID: 46765, PID: 46765,
}, },
}, },
BindMounts: map[string]string{
"/1/2/3": "/4/5/6",
"/test/file.test": "/test2/file2.test",
},
}, },
valid: true, valid: true,
} }