diff --git a/pkg/config/config.go b/pkg/config/config.go index b144d7affb3..9b49ed5086a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -66,7 +66,7 @@ type Iface interface { } // GetStore returns the container storage for a given configuration -func (c *Config) GetStore() (storage.Store, error) { +func (c *RootConfig) GetStore() (storage.Store, error) { return storage.GetStore(storage.StoreOptions{ RunRoot: c.RunRoot, GraphRoot: c.Root, @@ -695,6 +695,18 @@ func (c *RootConfig) Validate(onExecution bool) error { if err := os.MkdirAll(c.LogDir, 0o700); err != nil { return errors.Wrap(err, "invalid log_dir") } + store, err := c.GetStore() + if err != nil { + return errors.Wrapf(err, "failed to get store to set defaults") + } + // This step merges the /etc/container/storage.conf with the + // storage configuration in crio.conf + // If we don't do this step, we risk returning the incorrect info + // on Inspect (/info) requests + c.RunRoot = store.RunRoot() + c.Root = store.GraphRoot() + c.Storage = store.GraphDriverName() + c.StorageOptions = store.GraphOptions() } return nil diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 29a2524b37b..6d172c73129 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -6,6 +6,7 @@ import ( "path" "path/filepath" + "github.com/containers/storage" "github.com/cri-o/cri-o/pkg/config" . "github.com/onsi/ginkgo" @@ -704,6 +705,44 @@ var _ = t.Describe("Config", func() { // Then Expect(err).NotTo(BeNil()) }) + + It("should get default storage options when options are empty", func() { + // Given + defaultStore, err := storage.GetStore(storage.StoreOptions{}) + Expect(err).To(BeNil()) + + sut.RootConfig.RunRoot = "" + sut.RootConfig.Root = "" + sut.RootConfig.Storage = "" + sut.RootConfig.StorageOptions = make([]string, 0) + + // When + err = sut.Validate(true) + + // Then + Expect(err).To(BeNil()) + Expect(sut.RootConfig.RunRoot).To(Equal(defaultStore.RunRoot())) + Expect(sut.RootConfig.Root).To(Equal(defaultStore.GraphRoot())) + Expect(sut.RootConfig.Storage).To(Equal(defaultStore.GraphDriverName())) + Expect(sut.RootConfig.StorageOptions).To(Equal(defaultStore.GraphOptions())) + }) + + It("should override default storage options", func() { + // Given + defaultStore, err := storage.GetStore(storage.StoreOptions{}) + Expect(err).To(BeNil()) + + sut.RootConfig.RunRoot = "/tmp" + sut.RootConfig.Root = "/tmp" + + // When + err = sut.Validate(true) + + // Then + Expect(err).To(BeNil()) + Expect(sut.RootConfig.RunRoot).NotTo(Equal(defaultStore.RunRoot())) + Expect(sut.RootConfig.Root).NotTo(Equal(defaultStore.GraphRoot())) + }) }) t.Describe("ToFile", func() {