@@ -41,6 +41,31 @@ type Options struct {
4141 additionalConfigs []string
4242}
4343
44+ // paths defines the search paths used for config reading.
45+ type paths struct {
46+ usr string
47+ etc string
48+ home string
49+ uid int
50+ }
51+
52+ func defaultPaths () (* paths , error ) {
53+ etcPath , err := overrideContainersConfigPath ()
54+ if err != nil {
55+ return nil , err
56+ }
57+ homePath , err := userConfigPath ()
58+ if err != nil {
59+ return nil , err
60+ }
61+ return & paths {
62+ usr : defaultContainersConfig ,
63+ etc : etcPath ,
64+ home : homePath ,
65+ uid : unshare .GetRootlessUID (),
66+ }, nil
67+ }
68+
4469// New returns a Config as described in the containers.conf(5) man page.
4570func New (options * Options ) (* Config , error ) {
4671 if options == nil {
@@ -49,7 +74,11 @@ func New(options *Options) (*Config, error) {
4974 cachedConfigMutex .Lock ()
5075 defer cachedConfigMutex .Unlock ()
5176 }
52- return newLocked (options )
77+ paths , err := defaultPaths ()
78+ if err != nil {
79+ return nil , err
80+ }
81+ return newLocked (options , paths )
5382}
5483
5584// Default returns the default container config. If no default config has been
@@ -62,21 +91,25 @@ func Default() (*Config, error) {
6291 if cachedConfig != nil || cachedConfigError != nil {
6392 return cachedConfig , cachedConfigError
6493 }
65- cachedConfig , cachedConfigError = newLocked (& Options {SetDefault : true })
94+ paths , err := defaultPaths ()
95+ if err != nil {
96+ return nil , err
97+ }
98+ cachedConfig , cachedConfigError = newLocked (& Options {SetDefault : true }, paths )
6699 return cachedConfig , cachedConfigError
67100}
68101
69102// A helper function for New() expecting the caller to hold the
70103// cachedConfigMutex if options.SetDefault is set..
71- func newLocked (options * Options ) (* Config , error ) {
104+ func newLocked (options * Options , paths * paths ) (* Config , error ) {
72105 // Start with the built-in defaults
73106 config , err := defaultConfig ()
74107 if err != nil {
75108 return nil , err
76109 }
77110
78111 // Now, gather the system configs and merge them as needed.
79- configs , err := systemConfigs ()
112+ configs , err := systemConfigs (paths )
80113 if err != nil {
81114 return nil , fmt .Errorf ("finding config on system: %w" , err )
82115 }
@@ -152,24 +185,18 @@ func NewConfig(userConfigPath string) (*Config, error) {
152185// Returns the list of configuration files, if they exist in order of hierarchy.
153186// The files are read in order and each new file can/will override previous
154187// file settings.
155- func systemConfigs () (configs []string , finalErr error ) {
188+ func systemConfigs (paths * paths ) (configs []string , finalErr error ) {
156189 if path := os .Getenv (containersConfEnv ); path != "" {
157190 if err := fileutils .Exists (path ); err != nil {
158191 return nil , fmt .Errorf ("%s file: %w" , containersConfEnv , err )
159192 }
160193 return append (configs , path ), nil
161194 }
162195
163- configs = append (configs , defaultContainersConfig )
164-
165- var err error
166- path , err := overrideContainersConfigPath ()
167- if err != nil {
168- return nil , err
169- }
170- configs = append (configs , path )
196+ configs = append (configs , paths .usr )
197+ configs = append (configs , paths .etc )
171198
172- configs , err = addConfigs (path + ".d" , configs )
199+ configs , err : = addConfigs (paths . etc + ".d" , configs )
173200 if err != nil {
174201 return nil , err
175202 }
@@ -178,27 +205,21 @@ func systemConfigs() (configs []string, finalErr error) {
178205 // /etc/containers/containers.rootless.conf
179206 // /etc/containers/containers.rootless.conf.d/
180207 // /etc/containers/containers.rootless.conf.d/<UID>/
181- uid := unshare .GetRootlessUID ()
182- if uid > 0 {
183- rootlessOverwritePath := filepath .Join (filepath .Dir (path ), "containers.rootless.conf" )
208+ if paths .uid > 0 {
209+ rootlessOverwritePath := filepath .Join (filepath .Dir (paths .etc ), "containers.rootless.conf" )
184210 configs = append (configs , rootlessOverwritePath )
185211 rootlessOverwritePathD := rootlessOverwritePath + ".d"
186212 configs , err = addConfigs (rootlessOverwritePathD , configs )
187213 if err != nil {
188214 return nil , err
189215 }
190- configs , err = addConfigs (filepath .Join (rootlessOverwritePathD , strconv .Itoa (uid )), configs )
216+ configs , err = addConfigs (filepath .Join (rootlessOverwritePathD , strconv .Itoa (paths . uid )), configs )
191217 if err != nil {
192218 return nil , err
193219 }
194220 }
195-
196- path , err = userConfigPath ()
197- if err != nil {
198- return nil , err
199- }
200- configs = append (configs , path )
201- configs , err = addConfigs (path + ".d" , configs )
221+ configs = append (configs , paths .home )
222+ configs , err = addConfigs (paths .home + ".d" , configs )
202223 if err != nil {
203224 return nil , err
204225 }
0 commit comments