summaryrefslogtreecommitdiff
path: root/libpod/boltdb_state.go
diff options
context:
space:
mode:
Diffstat (limited to 'libpod/boltdb_state.go')
-rw-r--r--libpod/boltdb_state.go401
1 files changed, 200 insertions, 201 deletions
diff --git a/libpod/boltdb_state.go b/libpod/boltdb_state.go
index 63e40a98f..176781f07 100644
--- a/libpod/boltdb_state.go
+++ b/libpod/boltdb_state.go
@@ -5,6 +5,7 @@ import (
"strings"
"sync"
+ "github.com/containers/libpod/libpod/define"
bolt "github.com/etcd-io/bbolt"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
@@ -65,7 +66,7 @@ func NewBoltState(path string, runtime *Runtime) (State, error) {
if err != nil {
return nil, errors.Wrapf(err, "error opening database %s", path)
}
- // Everywhere else, we use s.closeDBCon(db) to ensure the state's DB
+ // Everywhere else, we use s.deferredCloseDBCon(db) to ensure the state's DB
// mutex is also unlocked.
// However, here, the mutex has not been locked, since we just created
// the DB connection, and it hasn't left this function yet - no risk of
@@ -73,42 +74,50 @@ func NewBoltState(path string, runtime *Runtime) (State, error) {
// As such, just a db.Close() is fine here.
defer db.Close()
- // Perform initial database setup
- err = db.Update(func(tx *bolt.Tx) error {
- if _, err := tx.CreateBucketIfNotExists(idRegistryBkt); err != nil {
- return errors.Wrapf(err, "error creating id-registry bucket")
- }
- if _, err := tx.CreateBucketIfNotExists(nameRegistryBkt); err != nil {
- return errors.Wrapf(err, "error creating name-registry bucket")
- }
- if _, err := tx.CreateBucketIfNotExists(nsRegistryBkt); err != nil {
- return errors.Wrapf(err, "error creating ns-registry bucket")
- }
- if _, err := tx.CreateBucketIfNotExists(ctrBkt); err != nil {
- return errors.Wrapf(err, "error creating containers bucket")
- }
- if _, err := tx.CreateBucketIfNotExists(allCtrsBkt); err != nil {
- return errors.Wrapf(err, "error creating all containers bucket")
- }
- if _, err := tx.CreateBucketIfNotExists(podBkt); err != nil {
- return errors.Wrapf(err, "error creating pods bucket")
- }
- if _, err := tx.CreateBucketIfNotExists(allPodsBkt); err != nil {
- return errors.Wrapf(err, "error creating all pods bucket")
- }
- if _, err := tx.CreateBucketIfNotExists(volBkt); err != nil {
- return errors.Wrapf(err, "error creating volume bucket")
- }
- if _, err := tx.CreateBucketIfNotExists(allVolsBkt); err != nil {
- return errors.Wrapf(err, "error creating all volumes bucket")
+ createBuckets := [][]byte{
+ idRegistryBkt,
+ nameRegistryBkt,
+ nsRegistryBkt,
+ ctrBkt,
+ allCtrsBkt,
+ podBkt,
+ allPodsBkt,
+ volBkt,
+ allVolsBkt,
+ runtimeConfigBkt,
+ }
+
+ // Does the DB need an update?
+ needsUpdate := false
+ err = db.View(func(tx *bolt.Tx) error {
+ for _, bkt := range createBuckets {
+ if test := tx.Bucket(bkt); test == nil {
+ needsUpdate = true
+ break
+ }
}
- if _, err := tx.CreateBucketIfNotExists(runtimeConfigBkt); err != nil {
- return errors.Wrapf(err, "error creating runtime-config bucket")
+ return nil
+ })
+ if err != nil {
+ return nil, errors.Wrapf(err, "error checking DB schema")
+ }
+
+ if !needsUpdate {
+ state.valid = true
+ return state, nil
+ }
+
+ // Ensure schema is properly created in DB
+ err = db.Update(func(tx *bolt.Tx) error {
+ for _, bkt := range createBuckets {
+ if _, err := tx.CreateBucketIfNotExists(bkt); err != nil {
+ return errors.Wrapf(err, "error creating bucket %s", string(bkt))
+ }
}
return nil
})
if err != nil {
- return nil, errors.Wrapf(err, "error creating initial database layout")
+ return nil, errors.Wrapf(err, "error creating buckets for DB")
}
state.valid = true
@@ -125,14 +134,14 @@ func (s *BoltState) Close() error {
// Refresh clears container and pod states after a reboot
func (s *BoltState) Refresh() error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
db, err := s.getDBCon()
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
idBucket, err := getIDBucket(tx)
@@ -163,13 +172,13 @@ func (s *BoltState) Refresh() error {
if podBkt == nil {
// This is neither a pod nor a container
// Error out on the dangling ID
- return errors.Wrapf(ErrInternal, "id %s is not a pod or a container", string(id))
+ return errors.Wrapf(define.ErrInternal, "id %s is not a pod or a container", string(id))
}
// Get the state
stateBytes := podBkt.Get(stateKey)
if stateBytes == nil {
- return errors.Wrapf(ErrInternal, "pod %s missing state key", string(id))
+ return errors.Wrapf(define.ErrInternal, "pod %s missing state key", string(id))
}
state := new(podState)
@@ -202,7 +211,7 @@ func (s *BoltState) Refresh() error {
stateBytes := ctrBkt.Get(stateKey)
if stateBytes == nil {
// Badly formatted container bucket
- return errors.Wrapf(ErrInternal, "container %s missing state in DB", string(id))
+ return errors.Wrapf(define.ErrInternal, "container %s missing state in DB", string(id))
}
state := new(ContainerState)
@@ -235,7 +244,7 @@ func (s *BoltState) Refresh() error {
// the database was first initialized
func (s *BoltState) GetDBConfig() (*DBConfig, error) {
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
cfg := new(DBConfig)
@@ -244,7 +253,7 @@ func (s *BoltState) GetDBConfig() (*DBConfig, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
configBucket, err := getRuntimeConfigBucket(tx)
@@ -282,14 +291,14 @@ func (s *BoltState) GetDBConfig() (*DBConfig, error) {
// ValidateDBConfig validates paths in the given runtime against the database
func (s *BoltState) ValidateDBConfig(runtime *Runtime) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
db, err := s.getDBCon()
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
// Check runtime configuration
if err := checkRuntimeConfig(db, runtime); err != nil {
@@ -316,11 +325,11 @@ func (s *BoltState) SetNamespace(ns string) error {
// Container retrieves a single container from the state by its full ID
func (s *BoltState) Container(id string) (*Container, error) {
if id == "" {
- return nil, ErrEmptyID
+ return nil, define.ErrEmptyID
}
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
ctrID := []byte(id)
@@ -333,7 +342,7 @@ func (s *BoltState) Container(id string) (*Container, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
ctrBucket, err := getCtrBucket(tx)
@@ -354,11 +363,11 @@ func (s *BoltState) Container(id string) (*Container, error) {
// partial ID or name
func (s *BoltState) LookupContainer(idOrName string) (*Container, error) {
if idOrName == "" {
- return nil, ErrEmptyID
+ return nil, define.ErrEmptyID
}
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
ctr := new(Container)
@@ -369,14 +378,9 @@ func (s *BoltState) LookupContainer(idOrName string) (*Container, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
- idBucket, err := getIDBucket(tx)
- if err != nil {
- return err
- }
-
ctrBucket, err := getCtrBucket(tx)
if err != nil {
return err
@@ -427,7 +431,7 @@ func (s *BoltState) LookupContainer(idOrName string) (*Container, error) {
// We were not given a full container ID or name.
// Search for partial ID matches.
exists := false
- err = idBucket.ForEach(func(checkID, checkName []byte) error {
+ err = ctrBucket.ForEach(func(checkID, checkName []byte) error {
// If the container isn't in our namespace, we
// can't match it
if s.namespaceBytes != nil {
@@ -438,7 +442,7 @@ func (s *BoltState) LookupContainer(idOrName string) (*Container, error) {
}
if strings.HasPrefix(string(checkID), idOrName) {
if exists {
- return errors.Wrapf(ErrCtrExists, "more than one result for container ID %s", idOrName)
+ return errors.Wrapf(define.ErrCtrExists, "more than one result for container ID %s", idOrName)
}
id = checkID
exists = true
@@ -450,9 +454,9 @@ func (s *BoltState) LookupContainer(idOrName string) (*Container, error) {
return err
} else if !exists {
if isPod {
- return errors.Wrapf(ErrNoSuchCtr, "%s is a pod, not a container", idOrName)
+ return errors.Wrapf(define.ErrNoSuchCtr, "%s is a pod, not a container", idOrName)
}
- return errors.Wrapf(ErrNoSuchCtr, "no container with name or ID %s found", idOrName)
+ return errors.Wrapf(define.ErrNoSuchCtr, "no container with name or ID %s found", idOrName)
}
return s.getContainerFromDB(id, ctr, ctrBucket)
@@ -467,11 +471,11 @@ func (s *BoltState) LookupContainer(idOrName string) (*Container, error) {
// HasContainer checks if a container is present in the state
func (s *BoltState) HasContainer(id string) (bool, error) {
if id == "" {
- return false, ErrEmptyID
+ return false, define.ErrEmptyID
}
if !s.valid {
- return false, ErrDBClosed
+ return false, define.ErrDBClosed
}
ctrID := []byte(id)
@@ -480,7 +484,7 @@ func (s *BoltState) HasContainer(id string) (bool, error) {
if err != nil {
return false, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
exists := false
@@ -515,15 +519,15 @@ func (s *BoltState) HasContainer(id string) (bool, error) {
// The container being added cannot belong to a pod
func (s *BoltState) AddContainer(ctr *Container) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !ctr.valid {
- return ErrCtrRemoved
+ return define.ErrCtrRemoved
}
if ctr.config.Pod != "" {
- return errors.Wrapf(ErrInvalidArg, "cannot add a container that belongs to a pod with AddContainer - use AddContainerToPod")
+ return errors.Wrapf(define.ErrInvalidArg, "cannot add a container that belongs to a pod with AddContainer - use AddContainerToPod")
}
return s.addContainer(ctr, nil)
@@ -534,18 +538,18 @@ func (s *BoltState) AddContainer(ctr *Container) error {
// pod, use RemoveContainerFromPod
func (s *BoltState) RemoveContainer(ctr *Container) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if ctr.config.Pod != "" {
- return errors.Wrapf(ErrPodExists, "container %s is part of a pod, use RemoveContainerFromPod instead", ctr.ID())
+ return errors.Wrapf(define.ErrPodExists, "container %s is part of a pod, use RemoveContainerFromPod instead", ctr.ID())
}
db, err := s.getDBCon()
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
return s.removeContainer(ctr, nil, tx)
@@ -556,15 +560,15 @@ func (s *BoltState) RemoveContainer(ctr *Container) error {
// UpdateContainer updates a container's state from the database
func (s *BoltState) UpdateContainer(ctr *Container) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !ctr.valid {
- return ErrCtrRemoved
+ return define.ErrCtrRemoved
}
if s.namespace != "" && s.namespace != ctr.config.Namespace {
- return errors.Wrapf(ErrNSMismatch, "container %s is in namespace %q, does not match our namespace %q", ctr.ID(), ctr.config.Namespace, s.namespace)
+ return errors.Wrapf(define.ErrNSMismatch, "container %s is in namespace %q, does not match our namespace %q", ctr.ID(), ctr.config.Namespace, s.namespace)
}
newState := new(ContainerState)
@@ -576,7 +580,7 @@ func (s *BoltState) UpdateContainer(ctr *Container) error {
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
ctrBucket, err := getCtrBucket(tx)
@@ -587,12 +591,12 @@ func (s *BoltState) UpdateContainer(ctr *Container) error {
ctrToUpdate := ctrBucket.Bucket(ctrID)
if ctrToUpdate == nil {
ctr.valid = false
- return errors.Wrapf(ErrNoSuchCtr, "container %s does not exist in database", ctr.ID())
+ return errors.Wrapf(define.ErrNoSuchCtr, "container %s does not exist in database", ctr.ID())
}
newStateBytes := ctrToUpdate.Get(stateKey)
if newStateBytes == nil {
- return errors.Wrapf(ErrInternal, "container %s does not have a state key in DB", ctr.ID())
+ return errors.Wrapf(define.ErrInternal, "container %s does not have a state key in DB", ctr.ID())
}
if err := json.Unmarshal(newStateBytes, newState); err != nil {
@@ -624,15 +628,15 @@ func (s *BoltState) UpdateContainer(ctr *Container) error {
// SaveContainer saves a container's current state in the database
func (s *BoltState) SaveContainer(ctr *Container) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !ctr.valid {
- return ErrCtrRemoved
+ return define.ErrCtrRemoved
}
if s.namespace != "" && s.namespace != ctr.config.Namespace {
- return errors.Wrapf(ErrNSMismatch, "container %s is in namespace %q, does not match our namespace %q", ctr.ID(), ctr.config.Namespace, s.namespace)
+ return errors.Wrapf(define.ErrNSMismatch, "container %s is in namespace %q, does not match our namespace %q", ctr.ID(), ctr.config.Namespace, s.namespace)
}
stateJSON, err := json.Marshal(ctr.state)
@@ -647,7 +651,7 @@ func (s *BoltState) SaveContainer(ctr *Container) error {
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
ctrBucket, err := getCtrBucket(tx)
@@ -658,7 +662,7 @@ func (s *BoltState) SaveContainer(ctr *Container) error {
ctrToSave := ctrBucket.Bucket(ctrID)
if ctrToSave == nil {
ctr.valid = false
- return errors.Wrapf(ErrNoSuchCtr, "container %s does not exist in DB", ctr.ID())
+ return errors.Wrapf(define.ErrNoSuchCtr, "container %s does not exist in DB", ctr.ID())
}
// Update the state
@@ -687,15 +691,15 @@ func (s *BoltState) SaveContainer(ctr *Container) error {
// container. If the slice is empty, no containers depend on the given container
func (s *BoltState) ContainerInUse(ctr *Container) ([]string, error) {
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
if !ctr.valid {
- return nil, ErrCtrRemoved
+ return nil, define.ErrCtrRemoved
}
if s.namespace != "" && s.namespace != ctr.config.Namespace {
- return nil, errors.Wrapf(ErrNSMismatch, "container %s is in namespace %q, does not match our namespace %q", ctr.ID(), ctr.config.Namespace, s.namespace)
+ return nil, errors.Wrapf(define.ErrNSMismatch, "container %s is in namespace %q, does not match our namespace %q", ctr.ID(), ctr.config.Namespace, s.namespace)
}
depCtrs := []string{}
@@ -704,7 +708,7 @@ func (s *BoltState) ContainerInUse(ctr *Container) ([]string, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
ctrBucket, err := getCtrBucket(tx)
@@ -715,12 +719,12 @@ func (s *BoltState) ContainerInUse(ctr *Container) ([]string, error) {
ctrDB := ctrBucket.Bucket([]byte(ctr.ID()))
if ctrDB == nil {
ctr.valid = false
- return errors.Wrapf(ErrNoSuchCtr, "no container with ID %s found in DB", ctr.ID())
+ return errors.Wrapf(define.ErrNoSuchCtr, "no container with ID %s found in DB", ctr.ID())
}
dependsBkt := ctrDB.Bucket(dependenciesBkt)
if dependsBkt == nil {
- return errors.Wrapf(ErrInternal, "container %s has no dependencies bucket", ctr.ID())
+ return errors.Wrapf(define.ErrInternal, "container %s has no dependencies bucket", ctr.ID())
}
// Iterate through and add dependencies
@@ -746,7 +750,7 @@ func (s *BoltState) ContainerInUse(ctr *Container) ([]string, error) {
// AllContainers retrieves all the containers in the database
func (s *BoltState) AllContainers() ([]*Container, error) {
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
ctrs := []*Container{}
@@ -755,7 +759,7 @@ func (s *BoltState) AllContainers() ([]*Container, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
allCtrsBucket, err := getAllCtrsBucket(tx)
@@ -774,7 +778,7 @@ func (s *BoltState) AllContainers() ([]*Container, error) {
// be much less helpful.
ctrExists := ctrBucket.Bucket(id)
if ctrExists == nil {
- return errors.Wrapf(ErrInternal, "state is inconsistent - container ID %s in all containers, but container not found", string(id))
+ return errors.Wrapf(define.ErrInternal, "state is inconsistent - container ID %s in all containers, but container not found", string(id))
}
ctr := new(Container)
@@ -786,7 +790,7 @@ func (s *BoltState) AllContainers() ([]*Container, error) {
// ignore it safely.
// We just won't include the container in the
// results.
- if errors.Cause(err) != ErrNSMismatch {
+ if errors.Cause(err) != define.ErrNSMismatch {
// Even if it's not an NS mismatch, it's
// not worth erroring over.
// If we do, a single bad container JSON
@@ -813,11 +817,11 @@ func (s *BoltState) AllContainers() ([]*Container, error) {
// comment on this function in state.go.
func (s *BoltState) RewriteContainerConfig(ctr *Container, newCfg *ContainerConfig) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !ctr.valid {
- return ErrCtrRemoved
+ return define.ErrCtrRemoved
}
newCfgJSON, err := json.Marshal(newCfg)
@@ -829,7 +833,7 @@ func (s *BoltState) RewriteContainerConfig(ctr *Container, newCfg *ContainerConf
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
ctrBkt, err := getCtrBucket(tx)
@@ -840,7 +844,7 @@ func (s *BoltState) RewriteContainerConfig(ctr *Container, newCfg *ContainerConf
ctrDB := ctrBkt.Bucket([]byte(ctr.ID()))
if ctrDB == nil {
ctr.valid = false
- return errors.Wrapf(ErrNoSuchCtr, "no container with ID %s found in DB", ctr.ID())
+ return errors.Wrapf(define.ErrNoSuchCtr, "no container with ID %s found in DB", ctr.ID())
}
if err := ctrDB.Put(configKey, newCfgJSON); err != nil {
@@ -857,11 +861,11 @@ func (s *BoltState) RewriteContainerConfig(ctr *Container, newCfg *ContainerConf
// comment on this function in state.go.
func (s *BoltState) RewritePodConfig(pod *Pod, newCfg *PodConfig) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !pod.valid {
- return ErrPodRemoved
+ return define.ErrPodRemoved
}
newCfgJSON, err := json.Marshal(newCfg)
@@ -873,7 +877,7 @@ func (s *BoltState) RewritePodConfig(pod *Pod, newCfg *PodConfig) error {
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
podBkt, err := getPodBucket(tx)
@@ -884,7 +888,7 @@ func (s *BoltState) RewritePodConfig(pod *Pod, newCfg *PodConfig) error {
podDB := podBkt.Bucket([]byte(pod.ID()))
if podDB == nil {
pod.valid = false
- return errors.Wrapf(ErrNoSuchPod, "no pod with ID %s found in DB", pod.ID())
+ return errors.Wrapf(define.ErrNoSuchPod, "no pod with ID %s found in DB", pod.ID())
}
if err := podDB.Put(configKey, newCfgJSON); err != nil {
@@ -899,11 +903,11 @@ func (s *BoltState) RewritePodConfig(pod *Pod, newCfg *PodConfig) error {
// Pod retrieves a pod given its full ID
func (s *BoltState) Pod(id string) (*Pod, error) {
if id == "" {
- return nil, ErrEmptyID
+ return nil, define.ErrEmptyID
}
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
podID := []byte(id)
@@ -916,7 +920,7 @@ func (s *BoltState) Pod(id string) (*Pod, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
podBkt, err := getPodBucket(tx)
@@ -936,11 +940,11 @@ func (s *BoltState) Pod(id string) (*Pod, error) {
// LookupPod retrieves a pod from full or unique partial ID or name
func (s *BoltState) LookupPod(idOrName string) (*Pod, error) {
if idOrName == "" {
- return nil, ErrEmptyID
+ return nil, define.ErrEmptyID
}
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
pod := new(Pod)
@@ -951,14 +955,9 @@ func (s *BoltState) LookupPod(idOrName string) (*Pod, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
- idBucket, err := getIDBucket(tx)
- if err != nil {
- return err
- }
-
podBkt, err := getPodBucket(tx)
if err != nil {
return err
@@ -1006,7 +1005,7 @@ func (s *BoltState) LookupPod(idOrName string) (*Pod, error) {
// They did not give us a full pod name or ID.
// Search for partial ID matches.
exists := false
- err = idBucket.ForEach(func(checkID, checkName []byte) error {
+ err = podBkt.ForEach(func(checkID, checkName []byte) error {
// If the pod isn't in our namespace, we
// can't match it
if s.namespaceBytes != nil {
@@ -1017,7 +1016,7 @@ func (s *BoltState) LookupPod(idOrName string) (*Pod, error) {
}
if strings.HasPrefix(string(checkID), idOrName) {
if exists {
- return errors.Wrapf(ErrPodExists, "more than one result for ID or name %s", idOrName)
+ return errors.Wrapf(define.ErrPodExists, "more than one result for ID or name %s", idOrName)
}
id = checkID
exists = true
@@ -1029,9 +1028,9 @@ func (s *BoltState) LookupPod(idOrName string) (*Pod, error) {
return err
} else if !exists {
if isCtr {
- return errors.Wrapf(ErrNoSuchPod, "%s is a container, not a pod", idOrName)
+ return errors.Wrapf(define.ErrNoSuchPod, "%s is a container, not a pod", idOrName)
}
- return errors.Wrapf(ErrNoSuchPod, "no pod with name or ID %s found", idOrName)
+ return errors.Wrapf(define.ErrNoSuchPod, "no pod with name or ID %s found", idOrName)
}
// We might have found a container ID, but it's OK
@@ -1048,11 +1047,11 @@ func (s *BoltState) LookupPod(idOrName string) (*Pod, error) {
// HasPod checks if a pod with the given ID exists in the state
func (s *BoltState) HasPod(id string) (bool, error) {
if id == "" {
- return false, ErrEmptyID
+ return false, define.ErrEmptyID
}
if !s.valid {
- return false, ErrDBClosed
+ return false, define.ErrDBClosed
}
podID := []byte(id)
@@ -1063,7 +1062,7 @@ func (s *BoltState) HasPod(id string) (bool, error) {
if err != nil {
return false, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
podBkt, err := getPodBucket(tx)
@@ -1095,19 +1094,19 @@ func (s *BoltState) HasPod(id string) (bool, error) {
// PodHasContainer checks if the given pod has a container with the given ID
func (s *BoltState) PodHasContainer(pod *Pod, id string) (bool, error) {
if id == "" {
- return false, ErrEmptyID
+ return false, define.ErrEmptyID
}
if !s.valid {
- return false, ErrDBClosed
+ return false, define.ErrDBClosed
}
if !pod.valid {
- return false, ErrPodRemoved
+ return false, define.ErrPodRemoved
}
if s.namespace != "" && s.namespace != pod.config.Namespace {
- return false, errors.Wrapf(ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
+ return false, errors.Wrapf(define.ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
}
ctrID := []byte(id)
@@ -1119,7 +1118,7 @@ func (s *BoltState) PodHasContainer(pod *Pod, id string) (bool, error) {
if err != nil {
return false, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
podBkt, err := getPodBucket(tx)
@@ -1131,13 +1130,13 @@ func (s *BoltState) PodHasContainer(pod *Pod, id string) (bool, error) {
podDB := podBkt.Bucket(podID)
if podDB == nil {
pod.valid = false
- return errors.Wrapf(ErrNoSuchPod, "pod %s not found in database", pod.ID())
+ return errors.Wrapf(define.ErrNoSuchPod, "pod %s not found in database", pod.ID())
}
// Get pod containers bucket
podCtrs := podDB.Bucket(containersBkt)
if podCtrs == nil {
- return errors.Wrapf(ErrInternal, "pod %s missing containers bucket in DB", pod.ID())
+ return errors.Wrapf(define.ErrInternal, "pod %s missing containers bucket in DB", pod.ID())
}
// Don't bother with a namespace check on the container -
@@ -1162,15 +1161,15 @@ func (s *BoltState) PodHasContainer(pod *Pod, id string) (bool, error) {
// PodContainersByID returns the IDs of all containers present in the given pod
func (s *BoltState) PodContainersByID(pod *Pod) ([]string, error) {
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
if !pod.valid {
- return nil, ErrPodRemoved
+ return nil, define.ErrPodRemoved
}
if s.namespace != "" && s.namespace != pod.config.Namespace {
- return nil, errors.Wrapf(ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
+ return nil, errors.Wrapf(define.ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
}
podID := []byte(pod.ID())
@@ -1181,7 +1180,7 @@ func (s *BoltState) PodContainersByID(pod *Pod) ([]string, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
podBkt, err := getPodBucket(tx)
@@ -1193,13 +1192,13 @@ func (s *BoltState) PodContainersByID(pod *Pod) ([]string, error) {
podDB := podBkt.Bucket(podID)
if podDB == nil {
pod.valid = false
- return errors.Wrapf(ErrNoSuchPod, "pod %s not found in database", pod.ID())
+ return errors.Wrapf(define.ErrNoSuchPod, "pod %s not found in database", pod.ID())
}
// Get pod containers bucket
podCtrs := podDB.Bucket(containersBkt)
if podCtrs == nil {
- return errors.Wrapf(ErrInternal, "pod %s missing containers bucket in DB", pod.ID())
+ return errors.Wrapf(define.ErrInternal, "pod %s missing containers bucket in DB", pod.ID())
}
// Iterate through all containers in the pod
@@ -1224,15 +1223,15 @@ func (s *BoltState) PodContainersByID(pod *Pod) ([]string, error) {
// PodContainers returns all the containers present in the given pod
func (s *BoltState) PodContainers(pod *Pod) ([]*Container, error) {
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
if !pod.valid {
- return nil, ErrPodRemoved
+ return nil, define.ErrPodRemoved
}
if s.namespace != "" && s.namespace != pod.config.Namespace {
- return nil, errors.Wrapf(ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
+ return nil, errors.Wrapf(define.ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
}
podID := []byte(pod.ID())
@@ -1243,7 +1242,7 @@ func (s *BoltState) PodContainers(pod *Pod) ([]*Container, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
podBkt, err := getPodBucket(tx)
@@ -1260,13 +1259,13 @@ func (s *BoltState) PodContainers(pod *Pod) ([]*Container, error) {
podDB := podBkt.Bucket(podID)
if podDB == nil {
pod.valid = false
- return errors.Wrapf(ErrNoSuchPod, "pod %s not found in database", pod.ID())
+ return errors.Wrapf(define.ErrNoSuchPod, "pod %s not found in database", pod.ID())
}
// Get pod containers bucket
podCtrs := podDB.Bucket(containersBkt)
if podCtrs == nil {
- return errors.Wrapf(ErrInternal, "pod %s missing containers bucket in DB", pod.ID())
+ return errors.Wrapf(define.ErrInternal, "pod %s missing containers bucket in DB", pod.ID())
}
// Iterate through all containers in the pod
@@ -1295,11 +1294,11 @@ func (s *BoltState) PodContainers(pod *Pod) ([]*Container, error) {
// the sub bucket holding the container dependencies that this volume has
func (s *BoltState) AddVolume(volume *Volume) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !volume.valid {
- return ErrVolumeRemoved
+ return define.ErrVolumeRemoved
}
volName := []byte(volume.Name())
@@ -1313,7 +1312,7 @@ func (s *BoltState) AddVolume(volume *Volume) error {
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
volBkt, err := getVolBucket(tx)
@@ -1329,7 +1328,7 @@ func (s *BoltState) AddVolume(volume *Volume) error {
// Check if we already have a volume with the given name
volExists := allVolsBkt.Get(volName)
if volExists != nil {
- return errors.Wrapf(ErrVolumeExists, "name %s is in use", volume.Name())
+ return errors.Wrapf(define.ErrVolumeExists, "name %s is in use", volume.Name())
}
// We are good to add the volume
@@ -1361,7 +1360,7 @@ func (s *BoltState) AddVolume(volume *Volume) error {
// RemoveVolume removes the given volume from the state
func (s *BoltState) RemoveVolume(volume *Volume) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
volName := []byte(volume.Name())
@@ -1370,7 +1369,7 @@ func (s *BoltState) RemoveVolume(volume *Volume) error {
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
volBkt, err := getVolBucket(tx)
@@ -1392,7 +1391,7 @@ func (s *BoltState) RemoveVolume(volume *Volume) error {
volDB := volBkt.Bucket(volName)
if volDB == nil {
volume.valid = false
- return errors.Wrapf(ErrNoSuchVolume, "volume %s does not exist in DB", volume.Name())
+ return errors.Wrapf(define.ErrNoSuchVolume, "volume %s does not exist in DB", volume.Name())
}
// Check if volume is not being used by any container
@@ -1422,7 +1421,7 @@ func (s *BoltState) RemoveVolume(volume *Volume) error {
return errors.Wrapf(err, "error getting list of dependencies from dependencies bucket for volumes %q", volume.Name())
}
if len(deps) > 0 {
- return errors.Wrapf(ErrVolumeBeingUsed, "volume %s is being used by container(s) %s", volume.Name(), strings.Join(deps, ","))
+ return errors.Wrapf(define.ErrVolumeBeingUsed, "volume %s is being used by container(s) %s", volume.Name(), strings.Join(deps, ","))
}
}
@@ -1443,7 +1442,7 @@ func (s *BoltState) RemoveVolume(volume *Volume) error {
// AllVolumes returns all volumes present in the state
func (s *BoltState) AllVolumes() ([]*Volume, error) {
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
volumes := []*Volume{}
@@ -1452,7 +1451,7 @@ func (s *BoltState) AllVolumes() ([]*Volume, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
allVolsBucket, err := getAllVolsBucket(tx)
@@ -1469,14 +1468,14 @@ func (s *BoltState) AllVolumes() ([]*Volume, error) {
// This check can be removed if performance becomes an
// issue, but much less helpful errors will be produced
if volExists == nil {
- return errors.Wrapf(ErrInternal, "inconsistency in state - volume %s is in all volumes bucket but volume not found", string(id))
+ return errors.Wrapf(define.ErrInternal, "inconsistency in state - volume %s is in all volumes bucket but volume not found", string(id))
}
volume := new(Volume)
volume.config = new(VolumeConfig)
if err := s.getVolumeFromDB(id, volume, volBucket); err != nil {
- if errors.Cause(err) != ErrNSMismatch {
+ if errors.Cause(err) != define.ErrNSMismatch {
logrus.Errorf("Error retrieving volume %s from the database: %v", string(id), err)
}
} else {
@@ -1497,11 +1496,11 @@ func (s *BoltState) AllVolumes() ([]*Volume, error) {
// Volume retrieves a volume from full name
func (s *BoltState) Volume(name string) (*Volume, error) {
if name == "" {
- return nil, ErrEmptyID
+ return nil, define.ErrEmptyID
}
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
volName := []byte(name)
@@ -1513,7 +1512,7 @@ func (s *BoltState) Volume(name string) (*Volume, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
volBkt, err := getVolBucket(tx)
@@ -1533,11 +1532,11 @@ func (s *BoltState) Volume(name string) (*Volume, error) {
// HasVolume returns true if the given volume exists in the state, otherwise it returns false
func (s *BoltState) HasVolume(name string) (bool, error) {
if name == "" {
- return false, ErrEmptyID
+ return false, define.ErrEmptyID
}
if !s.valid {
- return false, ErrDBClosed
+ return false, define.ErrDBClosed
}
volName := []byte(name)
@@ -1548,7 +1547,7 @@ func (s *BoltState) HasVolume(name string) (bool, error) {
if err != nil {
return false, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
volBkt, err := getVolBucket(tx)
@@ -1575,11 +1574,11 @@ func (s *BoltState) HasVolume(name string) (bool, error) {
// volume. If the slice is empty, no containers use the given volume
func (s *BoltState) VolumeInUse(volume *Volume) ([]string, error) {
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
if !volume.valid {
- return nil, ErrVolumeRemoved
+ return nil, define.ErrVolumeRemoved
}
depCtrs := []string{}
@@ -1588,7 +1587,7 @@ func (s *BoltState) VolumeInUse(volume *Volume) ([]string, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
volBucket, err := getVolBucket(tx)
@@ -1604,12 +1603,12 @@ func (s *BoltState) VolumeInUse(volume *Volume) ([]string, error) {
volDB := volBucket.Bucket([]byte(volume.Name()))
if volDB == nil {
volume.valid = false
- return errors.Wrapf(ErrNoSuchVolume, "no volume with name %s found in DB", volume.Name())
+ return errors.Wrapf(define.ErrNoSuchVolume, "no volume with name %s found in DB", volume.Name())
}
dependsBkt := volDB.Bucket(volDependenciesBkt)
if dependsBkt == nil {
- return errors.Wrapf(ErrInternal, "volume %s has no dependencies bucket", volume.Name())
+ return errors.Wrapf(define.ErrInternal, "volume %s has no dependencies bucket", volume.Name())
}
// Iterate through and add dependencies
@@ -1641,15 +1640,15 @@ func (s *BoltState) VolumeInUse(volume *Volume) ([]string, error) {
// AddPod adds the given pod to the state.
func (s *BoltState) AddPod(pod *Pod) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !pod.valid {
- return ErrPodRemoved
+ return define.ErrPodRemoved
}
if s.namespace != "" && s.namespace != pod.config.Namespace {
- return errors.Wrapf(ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
+ return errors.Wrapf(define.ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
}
podID := []byte(pod.ID())
@@ -1674,7 +1673,7 @@ func (s *BoltState) AddPod(pod *Pod) error {
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
podBkt, err := getPodBucket(tx)
@@ -1705,11 +1704,11 @@ func (s *BoltState) AddPod(pod *Pod) error {
// Check if we already have something with the given ID and name
idExist := idsBkt.Get(podID)
if idExist != nil {
- return errors.Wrapf(ErrPodExists, "ID %s is in use", pod.ID())
+ return errors.Wrapf(define.ErrPodExists, "ID %s is in use", pod.ID())
}
nameExist := namesBkt.Get(podName)
if nameExist != nil {
- return errors.Wrapf(ErrPodExists, "name %s is in use", pod.Name())
+ return errors.Wrapf(define.ErrPodExists, "name %s is in use", pod.Name())
}
// We are good to add the pod
@@ -1765,15 +1764,15 @@ func (s *BoltState) AddPod(pod *Pod) error {
// Only empty pods can be removed
func (s *BoltState) RemovePod(pod *Pod) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !pod.valid {
- return ErrPodRemoved
+ return define.ErrPodRemoved
}
if s.namespace != "" && s.namespace != pod.config.Namespace {
- return errors.Wrapf(ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
+ return errors.Wrapf(define.ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
}
podID := []byte(pod.ID())
@@ -1783,7 +1782,7 @@ func (s *BoltState) RemovePod(pod *Pod) error {
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
podBkt, err := getPodBucket(tx)
@@ -1815,7 +1814,7 @@ func (s *BoltState) RemovePod(pod *Pod) error {
podDB := podBkt.Bucket(podID)
if podDB == nil {
pod.valid = false
- return errors.Wrapf(ErrNoSuchPod, "pod %s does not exist in DB", pod.ID())
+ return errors.Wrapf(define.ErrNoSuchPod, "pod %s does not exist in DB", pod.ID())
}
// Check if pod is empty
@@ -1827,7 +1826,7 @@ func (s *BoltState) RemovePod(pod *Pod) error {
if podCtrsBkt != nil {
cursor := podCtrsBkt.Cursor()
if id, _ := cursor.First(); id != nil {
- return errors.Wrapf(ErrCtrExists, "pod %s is not empty", pod.ID())
+ return errors.Wrapf(define.ErrCtrExists, "pod %s is not empty", pod.ID())
}
}
@@ -1861,15 +1860,15 @@ func (s *BoltState) RemovePod(pod *Pod) error {
// RemovePodContainers removes all containers in a pod
func (s *BoltState) RemovePodContainers(pod *Pod) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !pod.valid {
- return ErrPodRemoved
+ return define.ErrPodRemoved
}
if s.namespace != "" && s.namespace != pod.config.Namespace {
- return errors.Wrapf(ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
+ return errors.Wrapf(define.ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
}
podID := []byte(pod.ID())
@@ -1878,7 +1877,7 @@ func (s *BoltState) RemovePodContainers(pod *Pod) error {
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
podBkt, err := getPodBucket(tx)
@@ -1910,12 +1909,12 @@ func (s *BoltState) RemovePodContainers(pod *Pod) error {
podDB := podBkt.Bucket(podID)
if podDB == nil {
pod.valid = false
- return errors.Wrapf(ErrNoSuchPod, "pod %s does not exist in DB", pod.ID())
+ return errors.Wrapf(define.ErrNoSuchPod, "pod %s does not exist in DB", pod.ID())
}
podCtrsBkt := podDB.Bucket(containersBkt)
if podCtrsBkt == nil {
- return errors.Wrapf(ErrInternal, "pod %s does not have a containers bucket", pod.ID())
+ return errors.Wrapf(define.ErrInternal, "pod %s does not have a containers bucket", pod.ID())
}
// Traverse all containers in the pod with a cursor
@@ -1926,7 +1925,7 @@ func (s *BoltState) RemovePodContainers(pod *Pod) error {
if ctr == nil {
// This should never happen
// State is inconsistent
- return errors.Wrapf(ErrNoSuchCtr, "pod %s referenced nonexistant container %s", pod.ID(), string(id))
+ return errors.Wrapf(define.ErrNoSuchCtr, "pod %s referenced nonexistant container %s", pod.ID(), string(id))
}
ctrDeps := ctr.Bucket(dependenciesBkt)
// This should never be nil, but if it is, we're
@@ -1935,7 +1934,7 @@ func (s *BoltState) RemovePodContainers(pod *Pod) error {
err = ctrDeps.ForEach(func(depID, name []byte) error {
exists := podCtrsBkt.Get(depID)
if exists == nil {
- return errors.Wrapf(ErrCtrExists, "container %s has dependency %s outside of pod %s", string(id), string(depID), pod.ID())
+ return errors.Wrapf(define.ErrCtrExists, "container %s has dependency %s outside of pod %s", string(id), string(depID), pod.ID())
}
return nil
})
@@ -1947,7 +1946,7 @@ func (s *BoltState) RemovePodContainers(pod *Pod) error {
// Dependencies are set, we're clear to remove
if err := ctrBkt.DeleteBucket(id); err != nil {
- return errors.Wrapf(ErrInternal, "error deleting container %s from DB", string(id))
+ return errors.Wrapf(define.ErrInternal, "error deleting container %s from DB", string(id))
}
if err := idsBkt.Delete(id); err != nil {
@@ -1989,19 +1988,19 @@ func (s *BoltState) RemovePodContainers(pod *Pod) error {
// The container will be added to the state and the pod
func (s *BoltState) AddContainerToPod(pod *Pod, ctr *Container) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !pod.valid {
- return ErrPodRemoved
+ return define.ErrPodRemoved
}
if !ctr.valid {
- return ErrCtrRemoved
+ return define.ErrCtrRemoved
}
if ctr.config.Pod != pod.ID() {
- return errors.Wrapf(ErrNoSuchCtr, "container %s is not part of pod %s", ctr.ID(), pod.ID())
+ return errors.Wrapf(define.ErrNoSuchCtr, "container %s is not part of pod %s", ctr.ID(), pod.ID())
}
return s.addContainer(ctr, pod)
@@ -2011,35 +2010,35 @@ func (s *BoltState) AddContainerToPod(pod *Pod, ctr *Container) error {
// The container will also be removed from the state
func (s *BoltState) RemoveContainerFromPod(pod *Pod, ctr *Container) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !pod.valid {
- return ErrPodRemoved
+ return define.ErrPodRemoved
}
if s.namespace != "" {
if s.namespace != pod.config.Namespace {
- return errors.Wrapf(ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
+ return errors.Wrapf(define.ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
}
if s.namespace != ctr.config.Namespace {
- return errors.Wrapf(ErrNSMismatch, "container %s in in namespace %q but we are in namespace %q", ctr.ID(), ctr.config.Namespace, s.namespace)
+ return errors.Wrapf(define.ErrNSMismatch, "container %s in in namespace %q but we are in namespace %q", ctr.ID(), ctr.config.Namespace, s.namespace)
}
}
if ctr.config.Pod == "" {
- return errors.Wrapf(ErrNoSuchPod, "container %s is not part of a pod, use RemoveContainer instead", ctr.ID())
+ return errors.Wrapf(define.ErrNoSuchPod, "container %s is not part of a pod, use RemoveContainer instead", ctr.ID())
}
if ctr.config.Pod != pod.ID() {
- return errors.Wrapf(ErrInvalidArg, "container %s is not part of pod %s", ctr.ID(), pod.ID())
+ return errors.Wrapf(define.ErrInvalidArg, "container %s is not part of pod %s", ctr.ID(), pod.ID())
}
db, err := s.getDBCon()
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.Update(func(tx *bolt.Tx) error {
return s.removeContainer(ctr, pod, tx)
@@ -2050,15 +2049,15 @@ func (s *BoltState) RemoveContainerFromPod(pod *Pod, ctr *Container) error {
// UpdatePod updates a pod's state from the database
func (s *BoltState) UpdatePod(pod *Pod) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !pod.valid {
- return ErrPodRemoved
+ return define.ErrPodRemoved
}
if s.namespace != "" && s.namespace != pod.config.Namespace {
- return errors.Wrapf(ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
+ return errors.Wrapf(define.ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
}
newState := new(podState)
@@ -2067,7 +2066,7 @@ func (s *BoltState) UpdatePod(pod *Pod) error {
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
podID := []byte(pod.ID())
@@ -2080,13 +2079,13 @@ func (s *BoltState) UpdatePod(pod *Pod) error {
podDB := podBkt.Bucket(podID)
if podDB == nil {
pod.valid = false
- return errors.Wrapf(ErrNoSuchPod, "no pod with ID %s found in database", pod.ID())
+ return errors.Wrapf(define.ErrNoSuchPod, "no pod with ID %s found in database", pod.ID())
}
// Get the pod state JSON
podStateBytes := podDB.Get(stateKey)
if podStateBytes == nil {
- return errors.Wrapf(ErrInternal, "pod %s is missing state key in DB", pod.ID())
+ return errors.Wrapf(define.ErrInternal, "pod %s is missing state key in DB", pod.ID())
}
if err := json.Unmarshal(podStateBytes, newState); err != nil {
@@ -2107,15 +2106,15 @@ func (s *BoltState) UpdatePod(pod *Pod) error {
// SavePod saves a pod's state to the database
func (s *BoltState) SavePod(pod *Pod) error {
if !s.valid {
- return ErrDBClosed
+ return define.ErrDBClosed
}
if !pod.valid {
- return ErrPodRemoved
+ return define.ErrPodRemoved
}
if s.namespace != "" && s.namespace != pod.config.Namespace {
- return errors.Wrapf(ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
+ return errors.Wrapf(define.ErrNSMismatch, "pod %s is in namespace %q but we are in namespace %q", pod.ID(), pod.config.Namespace, s.namespace)
}
stateJSON, err := json.Marshal(pod.state)
@@ -2127,7 +2126,7 @@ func (s *BoltState) SavePod(pod *Pod) error {
if err != nil {
return err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
podID := []byte(pod.ID())
@@ -2140,7 +2139,7 @@ func (s *BoltState) SavePod(pod *Pod) error {
podDB := podBkt.Bucket(podID)
if podDB == nil {
pod.valid = false
- return errors.Wrapf(ErrNoSuchPod, "no pod with ID %s found in database", pod.ID())
+ return errors.Wrapf(define.ErrNoSuchPod, "no pod with ID %s found in database", pod.ID())
}
// Set the pod state JSON
@@ -2160,7 +2159,7 @@ func (s *BoltState) SavePod(pod *Pod) error {
// AllPods returns all pods present in the state
func (s *BoltState) AllPods() ([]*Pod, error) {
if !s.valid {
- return nil, ErrDBClosed
+ return nil, define.ErrDBClosed
}
pods := []*Pod{}
@@ -2169,7 +2168,7 @@ func (s *BoltState) AllPods() ([]*Pod, error) {
if err != nil {
return nil, err
}
- defer s.closeDBCon(db)
+ defer s.deferredCloseDBCon(db)
err = db.View(func(tx *bolt.Tx) error {
allPodsBucket, err := getAllPodsBucket(tx)
@@ -2187,7 +2186,7 @@ func (s *BoltState) AllPods() ([]*Pod, error) {
// This check can be removed if performance becomes an
// issue, but much less helpful errors will be produced
if podExists == nil {
- return errors.Wrapf(ErrInternal, "inconsistency in state - pod %s is in all pods bucket but pod not found", string(id))
+ return errors.Wrapf(define.ErrInternal, "inconsistency in state - pod %s is in all pods bucket but pod not found", string(id))
}
pod := new(Pod)
@@ -2195,7 +2194,7 @@ func (s *BoltState) AllPods() ([]*Pod, error) {
pod.state = new(podState)
if err := s.getPodFromDB(id, pod, podBucket); err != nil {
- if errors.Cause(err) != ErrNSMismatch {
+ if errors.Cause(err) != define.ErrNSMismatch {
logrus.Errorf("Error retrieving pod %s from the database: %v", string(id), err)
}
} else {