Fix checks for configuration keys in the DB

Currently, we will error if the DB is configured with the default
containers/storage config, and then opened by a libpod which has
explicitly set the defaults. This is due to us using an empty
config by default (to tell c/storage to use its defaults).

This patch changes our handling so that unset storage config
(using the default) and explicitly setting the defaults are both
compatible.

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

Closes: #423
Approved by: baude
This commit is contained in:
Matthew Heon
2018-02-28 15:31:58 -05:00
committed by Atomic Bot
parent e038393cf5
commit cb7b0edc5b

View File

@ -64,28 +64,31 @@ func checkRuntimeConfig(db *bolt.DB, runtime *Runtime) error {
} }
if err := validateDBAgainstConfig(configBkt, "static dir", if err := validateDBAgainstConfig(configBkt, "static dir",
runtime.config.StaticDir, staticDir); err != nil { runtime.config.StaticDir, staticDir, ""); err != nil {
return err return err
} }
if err := validateDBAgainstConfig(configBkt, "tmp dir", if err := validateDBAgainstConfig(configBkt, "tmp dir",
runtime.config.TmpDir, tmpDir); err != nil { runtime.config.TmpDir, tmpDir, ""); err != nil {
return err return err
} }
if err := validateDBAgainstConfig(configBkt, "run root", if err := validateDBAgainstConfig(configBkt, "run root",
runtime.config.StorageConfig.RunRoot, runRoot); err != nil { runtime.config.StorageConfig.RunRoot, runRoot,
storage.DefaultStoreOptions.RunRoot); err != nil {
return err return err
} }
if err := validateDBAgainstConfig(configBkt, "graph root", if err := validateDBAgainstConfig(configBkt, "graph root",
runtime.config.StorageConfig.GraphRoot, graphRoot); err != nil { runtime.config.StorageConfig.GraphRoot, graphRoot,
storage.DefaultStoreOptions.GraphRoot); err != nil {
return err return err
} }
return validateDBAgainstConfig(configBkt, "graph driver name", return validateDBAgainstConfig(configBkt, "graph driver name",
runtime.config.StorageConfig.GraphDriverName, runtime.config.StorageConfig.GraphDriverName,
graphDriverName) graphDriverName,
storage.DefaultStoreOptions.GraphDriverName)
}) })
return err return err
@ -93,14 +96,36 @@ func checkRuntimeConfig(db *bolt.DB, runtime *Runtime) error {
// Validate a configuration entry in the DB against current runtime config // Validate a configuration entry in the DB against current runtime config
// If the given configuration key does not exist it will be created // If the given configuration key does not exist it will be created
func validateDBAgainstConfig(bucket *bolt.Bucket, fieldName, runtimeValue string, keyName []byte) error { // If the given runtimeValue or value retrieved from the database are the empty
// string and defaultValue is not, defaultValue will be checked instead. This
// ensures that we will not fail on configuration changes in configured c/storage.
func validateDBAgainstConfig(bucket *bolt.Bucket, fieldName, runtimeValue string, keyName []byte, defaultValue string) error {
keyBytes := bucket.Get(keyName) keyBytes := bucket.Get(keyName)
if keyBytes == nil { if keyBytes == nil {
if err := bucket.Put(keyName, []byte(runtimeValue)); err != nil { dbValue := []byte(runtimeValue)
if runtimeValue == "" && defaultValue != "" {
dbValue = []byte(defaultValue)
}
if err := bucket.Put(keyName, dbValue); err != nil {
return errors.Wrapf(err, "error updating %s in DB runtime config", fieldName) return errors.Wrapf(err, "error updating %s in DB runtime config", fieldName)
} }
} else { } else {
if runtimeValue != string(keyBytes) { if runtimeValue != string(keyBytes) {
// If runtimeValue is the empty string, check against
// the default
if runtimeValue == "" && defaultValue != "" &&
string(keyBytes) == defaultValue {
return nil
}
// If DB value is the empty string, check that the
// runtime value is the default
if string(keyBytes) == "" && defaultValue != "" &&
runtimeValue == defaultValue {
return nil
}
return errors.Wrapf(ErrDBBadConfig, "database %s %s does not match our %s %s", return errors.Wrapf(ErrDBBadConfig, "database %s %s does not match our %s %s",
fieldName, string(keyBytes), fieldName, runtimeValue) fieldName, string(keyBytes), fieldName, runtimeValue)
} }