Skip to content

Commit

Permalink
Add support for drop-in config
Browse files Browse the repository at this point in the history
Signed-off-by: Harshal Patil <harpatil@redhat.com>
  • Loading branch information
harche committed Apr 26, 2024
1 parent c051e2a commit 7ca8f82
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 19 deletions.
52 changes: 50 additions & 2 deletions types/options.go
Expand Up @@ -49,6 +49,12 @@ var (
defaultConfigFile = SystemConfigFile
// DefaultStoreOptions is a reasonable default set of options.
defaultStoreOptions StoreOptions

// defaultOverrideConfigFile path to override the default system wide storage.conf file
defaultOverrideConfigFile = "/etc/containers/storage.conf"

// defaultDropInConfigDir path to the folder containing drop in config files
defaultDropInConfigDir = defaultOverrideConfigFile + ".d"
)

func loadDefaultStoreOptions() {
Expand Down Expand Up @@ -114,11 +120,53 @@ func loadDefaultStoreOptions() {

// loadStoreOptions returns the default storage ops for containers
func loadStoreOptions() (StoreOptions, error) {
storageConf, err := DefaultConfigFile()
baseConf, err := DefaultConfigFile()
if err != nil {
return defaultStoreOptions, err
}
return loadStoreOptionsFromConfFile(storageConf)

// Load the base config file
baseOptions, err := loadStoreOptionsFromConfFile(baseConf)
if err != nil {
return defaultStoreOptions, err
}

if _, err := os.Stat(defaultDropInConfigDir); err == nil {
// The directory exists, so merge the configuration from this directory
err = mergeConfigFromDirectory(&baseOptions, defaultDropInConfigDir)
if err != nil {
return defaultStoreOptions, err
}
} else if !os.IsNotExist(err) {
// There was an error other than the directory not existing
return defaultStoreOptions, err
}

return baseOptions, nil
}

func mergeConfigFromDirectory(baseOptions *StoreOptions, configDir string) error {
return filepath.Walk(configDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}

// Only consider files with .conf extension
if filepath.Ext(path) != ".conf" {
return nil
}

// Load drop-in options from the current file
err = ReloadConfigurationFile(path, baseOptions)
if err != nil {
return err
}

return nil
})
}

// usePerUserStorage returns whether the user private storage must be used.
Expand Down
2 changes: 0 additions & 2 deletions types/options_darwin.go
Expand Up @@ -8,8 +8,6 @@ const (
SystemConfigFile = "/usr/share/containers/storage.conf"
)

var defaultOverrideConfigFile = "/etc/containers/storage.conf"

// canUseRootlessOverlay returns true if the overlay driver can be used for rootless containers
func canUseRootlessOverlay(home, runhome string) bool {
return false
Expand Down
5 changes: 0 additions & 5 deletions types/options_freebsd.go
Expand Up @@ -8,11 +8,6 @@ const (
SystemConfigFile = "/usr/local/share/containers/storage.conf"
)

// defaultConfigFile path to the system wide storage.conf file
var (
defaultOverrideConfigFile = "/usr/local/etc/containers/storage.conf"
)

// canUseRootlessOverlay returns true if the overlay driver can be used for rootless containers
func canUseRootlessOverlay(home, runhome string) bool {
return false
Expand Down
5 changes: 0 additions & 5 deletions types/options_linux.go
Expand Up @@ -16,11 +16,6 @@ const (
SystemConfigFile = "/usr/share/containers/storage.conf"
)

// defaultConfigFile path to the system wide storage.conf file
var (
defaultOverrideConfigFile = "/etc/containers/storage.conf"
)

// canUseRootlessOverlay returns true if the overlay driver can be used for rootless containers
func canUseRootlessOverlay(home, runhome string) bool {
// we check first for fuse-overlayfs since it is cheaper.
Expand Down
56 changes: 56 additions & 0 deletions types/options_test.go
Expand Up @@ -206,3 +206,59 @@ func TestReloadConfigurationFile(t *testing.T) {

assert.Equal(t, strings.Contains(content.String(), "Failed to decode the keys [\\\"foo\\\" \\\"storage.options.graphroot\\\"] from \\\"./storage_broken.conf\\\"\""), true)
}

func TestMergeConfigFromDirectory(t *testing.T) {
tempDir, err := os.MkdirTemp("", "testConfigDir")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)

// Creating a mix of files with .txt and .conf extensions
fileNames := []string{"config1.conf", "config2.conf", "ignore.txt", "config3.conf", "config4.txt"}
contents := []string{
`[storage]
runroot = 'temp/run1'
graphroot = 'temp/graph1'`,
`[storage]
runroot = 'temp/run2'
graphroot = 'temp/graph2'`,
`[storage]
runroot = 'should/ignore'
graphroot = 'should/ignore'`,
`[storage]
runroot = 'temp/run3'
graphroot = 'temp/graph3'`,
`[storage]
runroot = 'temp/run4'
graphroot = 'temp/graph4'`,
}
for i, fileName := range fileNames {
filePath := filepath.Join(tempDir, fileName)
if err := os.WriteFile(filePath, []byte(contents[i]), 0o666); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
}

// Set base options
baseOptions := StoreOptions{
RunRoot: "initial/run",
GraphRoot: "initial/graph",
}

// Expected results after merging configurations from only .conf files
expectedOptions := StoreOptions{
RunRoot: "temp/run3", // Last .conf file (config3.conf) read overrides earlier values
GraphRoot: "temp/graph3",
}

// Run the merging function
err = mergeConfigFromDirectory(&baseOptions, tempDir)
if err != nil {
t.Fatalf("Error merging config from directory: %v", err)
}

if baseOptions.RunRoot != expectedOptions.RunRoot || baseOptions.GraphRoot != expectedOptions.GraphRoot {
t.Errorf("Expected RunRoot to be %q and GraphRoot to be %q, got RunRoot %q and GraphRoot %q", expectedOptions.RunRoot, expectedOptions.GraphRoot, baseOptions.RunRoot, baseOptions.GraphRoot)
}
}
5 changes: 0 additions & 5 deletions types/options_windows.go
Expand Up @@ -8,11 +8,6 @@ const (
SystemConfigFile = "/usr/share/containers/storage.conf"
)

// defaultConfigFile path to the system wide storage.conf file
var (
defaultOverrideConfigFile = "/etc/containers/storage.conf"
)

// canUseRootlessOverlay returns true if the overlay driver can be used for rootless containers
func canUseRootlessOverlay(home, runhome string) bool {
return false
Expand Down

0 comments on commit 7ca8f82

Please sign in to comment.