Skip to content

Commit 2566372

Browse files
committed
Fix config import relative path glob
Previously, resolveImports would apply a glob filter if the path contained any '*', or otherwise convert relative paths to absolute. This meant that it was impossible to specify globs with paths relative to the main config file. This commit first resolves relative to absolute paths, then applies the glob filter (if any). A test case is added to ensure that this now works as expected. Signed-off-by: Angelos Kolaitis <[email protected]>
1 parent 96bf529 commit 2566372

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

cmd/containerd/server/config/config.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ func resolveImports(parent string, imports []string) ([]string, error) {
375375
var out []string
376376

377377
for _, path := range imports {
378+
path = filepath.Clean(path)
379+
if !filepath.IsAbs(path) {
380+
path = filepath.Join(filepath.Dir(parent), path)
381+
}
382+
378383
if strings.Contains(path, "*") {
379384
matches, err := filepath.Glob(path)
380385
if err != nil {
@@ -383,11 +388,6 @@ func resolveImports(parent string, imports []string) ([]string, error) {
383388

384389
out = append(out, matches...)
385390
} else {
386-
path = filepath.Clean(path)
387-
if !filepath.IsAbs(path) {
388-
path = filepath.Join(filepath.Dir(parent), path)
389-
}
390-
391391
out = append(out, path)
392392
}
393393
}

cmd/containerd/server/config/config_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ func TestResolveImports(t *testing.T) {
104104
filepath.Join(tempDir, "test.toml"),
105105
filepath.Join(tempDir, "current.toml"),
106106
})
107+
108+
t.Run("GlobRelativePath", func(t *testing.T) {
109+
imports, err := resolveImports(filepath.Join(tempDir, "root.toml"), []string{
110+
"config_*.toml", // Glob files from working dir
111+
})
112+
assert.NoError(t, err)
113+
assert.Equal(t, imports, []string{
114+
filepath.Join(tempDir, "config_1.toml"),
115+
filepath.Join(tempDir, "config_2.toml"),
116+
})
117+
})
107118
}
108119

109120
func TestLoadSingleConfig(t *testing.T) {

0 commit comments

Comments
 (0)