Add a struct indicating if some Runtime fields were set

To configure runtime fields from the database, we need to know
whether they were explicitly overwritten by the user (we don't
want to overwrite anything that was explicitly set). Store a
struct containing whether the variables we'll grab from the DB
were explicitly set by the user so we know what we can and can't
overwrite.

This determines whether libpod runtime and static dirs were set
via config file in a horribly hackish way (double TOML decode),
but I can't think of a better way, and it shouldn't be that
expensive as the libpod config is tiny.

Signed-off-by: Matthew Heon <mheon@redhat.com>
This commit is contained in:
Matthew Heon
2018-12-02 14:06:33 -05:00
parent 137e0948ae
commit aa7ce33b7a
2 changed files with 56 additions and 1 deletions

View File

@ -30,9 +30,26 @@ func WithStorageConfig(config storage.StoreOptions) RuntimeOption {
}
rt.config.StorageConfig.RunRoot = config.RunRoot
if config.RunRoot != "" {
rt.configuredFrom.storageRunRootSet = true
}
rt.config.StorageConfig.GraphRoot = config.GraphRoot
if config.GraphRoot != "" {
rt.configuredFrom.storageGraphRootSet = true
}
rt.config.StorageConfig.GraphDriverName = config.GraphDriverName
rt.config.StaticDir = filepath.Join(config.GraphRoot, "libpod")
if config.GraphDriverName != "" {
rt.configuredFrom.storageGraphDriverSet = true
}
// Only set our static dir if it was not already explicitly
// overridden
if config.GraphRoot != "" && !rt.configuredFrom.libpodStaticDirSet {
rt.config.StaticDir = filepath.Join(config.GraphRoot, "libpod")
rt.configuredFrom.libpodStaticDirSet = true
}
rt.config.StorageConfig.GraphDriverOptions = make([]string, len(config.GraphDriverOptions))
copy(rt.config.StorageConfig.GraphDriverOptions, config.GraphDriverOptions)
@ -174,6 +191,7 @@ func WithStaticDir(dir string) RuntimeOption {
}
rt.config.StaticDir = dir
rt.configuredFrom.libpodStaticDirSet = true
return nil
}
@ -226,6 +244,7 @@ func WithTmpDir(dir string) RuntimeOption {
}
rt.config.TmpDir = dir
rt.configuredFrom.libpodTmpDirSet = true
return nil
}

View File

@ -84,6 +84,7 @@ type Runtime struct {
lock sync.RWMutex
imageRuntime *image.Runtime
firewallBackend firewall.FirewallBackend
configuredFrom *runtimeConfiguredFrom
}
// RuntimeConfig contains configuration options used to set up the runtime
@ -175,6 +176,20 @@ type RuntimeConfig struct {
EnableLabeling bool `toml:"label"`
}
// runtimeConfiguredFrom is a struct used during early runtime init to help
// assemble the full RuntimeConfig struct from defaults.
// It indicated whether several fields in the runtime configuration were set
// explicitly.
// If they were not, we may override them with information from the database,
// if it exists and differs from what is present in the system already.
type runtimeConfiguredFrom struct {
storageGraphDriverSet bool
storageGraphRootSet bool
storageRunRootSet bool
libpodStaticDirSet bool
libpodTmpDirSet bool
}
var (
defaultRuntimeConfig = RuntimeConfig{
// Leave this empty so containers/storage will use its defaults
@ -253,6 +268,7 @@ func SetXdgRuntimeDir(val string) error {
func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
runtime = new(Runtime)
runtime.config = new(RuntimeConfig)
runtime.configuredFrom = new(runtimeConfiguredFrom)
// Copy the default configuration
tmpDir, err := getDefaultTmpDir()
@ -307,6 +323,25 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
if err != nil {
return nil, errors.Wrapf(err, "error reading configuration file %s", configPath)
}
// This is ugly, but we need to decode twice.
// Once to check if libpod static and tmp dirs were explicitly
// set (not enough to check if they're not the default value,
// might have been explicitly configured to the default).
// A second time to actually get a usable config.
tmpConfig := new(RuntimeConfig)
if _, err := toml.Decode(string(contents), tmpConfig); err != nil {
return nil, errors.Wrapf(err, "error decoding configuration file %s",
configPath)
}
if tmpConfig.StaticDir != "" {
runtime.configuredFrom.libpodStaticDirSet = true
}
if tmpConfig.TmpDir != "" {
runtime.configuredFrom.libpodTmpDirSet = true
}
if _, err := toml.Decode(string(contents), runtime.config); err != nil {
return nil, errors.Wrapf(err, "error decoding configuration file %s", configPath)
}
@ -348,6 +383,7 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) {
func NewRuntimeFromConfig(configPath string, options ...RuntimeOption) (runtime *Runtime, err error) {
runtime = new(Runtime)
runtime.config = new(RuntimeConfig)
runtime.configuredFrom = new(runtimeConfiguredFrom)
// Set two fields not in the TOML config
runtime.config.StateType = defaultRuntimeConfig.StateType