Skip to content

Commit 9a595db

Browse files
authored
Merge pull request #22043 from medyagh/preload_fix_containerdformat
ci: fix preload config parse for containerd
2 parents cdae2b4 + 9f6084e commit 9a595db

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

hack/preload-images/preload_images.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,35 @@ var verifyContainerdStorage = func() error {
188188
if err != nil {
189189
return fmt.Errorf("%v: %v:\n%s", cmd.Args, err, stderr.String())
190190
}
191-
var driver string
192-
for _, line := range strings.Split(string(output), "\n") {
193-
if strings.Contains(line, "snapshotter = ") {
194-
driver = strings.Split(line, " = ")[1]
195-
driver = strings.Trim(driver, "\"")
196-
}
197-
}
191+
driver := parseContainerdSnapshotter(string(output))
198192
if driver != containerdSnapshotter {
199193
return fmt.Errorf("containerd snapshotter %s does not match requested %s", driver, containerdSnapshotter)
200194
}
201195
return nil
202196
}
203197

198+
func parseContainerdSnapshotter(cfg string) string {
199+
var driver string
200+
for _, line := range strings.Split(cfg, "\n") {
201+
line = strings.TrimSpace(line)
202+
if !strings.HasPrefix(line, "snapshotter") { // e.g. snapshotter = "overlayfs" or snapshotter = 'overlayfs'
203+
continue
204+
}
205+
parts := strings.SplitN(line, "=", 2)
206+
if len(parts) != 2 {
207+
continue
208+
}
209+
val := strings.Trim(strings.TrimSpace(parts[1]), "\"'")
210+
if val == "" {
211+
continue
212+
}
213+
if driver == "" {
214+
driver = val
215+
}
216+
}
217+
return driver
218+
}
219+
204220
var verifyPodmanStorage = func() error {
205221
cmd := exec.Command("docker", "exec", profile, "sudo", "podman", "info", "-f", "json")
206222
var stderr bytes.Buffer
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import "testing"
4+
5+
const containerd2Config = `version = 3
6+
root = '/var/lib/containerd'
7+
8+
[plugins]
9+
[plugins.'io.containerd.cri.v1.images']
10+
snapshotter = 'overlayfs'
11+
12+
[plugins.'io.containerd.cri.v1.runtime'.containerd]
13+
[plugins.'io.containerd.cri.v1.runtime'.containerd.runtimes]
14+
[plugins.'io.containerd.cri.v1.runtime'.containerd.runtimes.runc]
15+
snapshotter = ''
16+
`
17+
18+
const containerd17Config = `disabled_plugins = []
19+
imports = ["/etc/containerd/config.toml"]
20+
oom_score = 0
21+
plugin_dir = ""
22+
required_plugins = []
23+
root = "/var/lib/containerd"
24+
state = "/run/containerd"
25+
temp = ""
26+
version = 2
27+
28+
[plugins]
29+
30+
[plugins."io.containerd.grpc.v1.cri"]
31+
[plugins."io.containerd.grpc.v1.cri".containerd]
32+
snapshotter = "overlayfs"
33+
34+
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
35+
36+
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
37+
snapshotter = ""
38+
`
39+
40+
func TestParseContainerdSnapshotterCases(t *testing.T) {
41+
tcs := []struct {
42+
name string
43+
cfg string
44+
want string
45+
}{
46+
{name: "v2 config with single quotes", cfg: containerd2Config, want: "overlayfs"},
47+
{name: "v1.7 config with double quotes", cfg: containerd17Config, want: "overlayfs"},
48+
{name: "single line double quotes", cfg: "snapshotter = \"overlayfs\"\n", want: "overlayfs"},
49+
}
50+
for _, tc := range tcs {
51+
tc := tc
52+
t.Run(tc.name, func(t *testing.T) {
53+
if got := parseContainerdSnapshotter(tc.cfg); got != tc.want {
54+
t.Fatalf("expected %q, got %q", tc.want, got)
55+
}
56+
})
57+
}
58+
}

0 commit comments

Comments
 (0)