Skip to content

Commit d668951

Browse files
committed
feat(containerd): support 2.x
Signed-off-by: Vaughn Dice <[email protected]>
1 parent 4868fab commit d668951

File tree

5 files changed

+72
-11
lines changed

5 files changed

+72
-11
lines changed

.github/workflows/helm-chart-smoketest.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ on:
44
workflow_call:
55

66
env:
7-
# TODO: bump to a more recent K8S_VERSION once rcm supports containerd 2.0+
8-
# see https://github.com/spinframework/runtime-class-manager/issues/371
9-
# For k3d in particular, containerd 2.0 is used starting with k3s image tag v1.32.2-k3s1
10-
K8S_VERSION: v1.32.1
7+
K8S_VERSION: v1.32.3
118
MICROK8S_CHANNEL: 1.32/stable
129
SHIM_SPIN_VERSION: v0.19.0
1310
DOCKER_BUILD_SUMMARY: false

internal/containerd/configure.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ func (c *Config) AddRuntime(shimPath string) error {
4949
runtimeName := shim.RuntimeName(path.Base(shimPath))
5050
l := slog.With("runtime", runtimeName)
5151

52-
cfg := generateConfig(shimPath, runtimeName)
53-
5452
// Containerd config file needs to exist, otherwise return the error
5553
data, err := afero.ReadFile(c.hostFs, c.configPath)
5654
if err != nil {
@@ -63,6 +61,8 @@ func (c *Config) AddRuntime(shimPath string) error {
6361
return nil
6462
}
6563

64+
cfg := generateConfig(shimPath, runtimeName, data)
65+
6666
// Open file in append mode
6767
file, err := c.hostFs.OpenFile(c.configPath, os.O_APPEND|os.O_WRONLY, 0o644) //nolint:mnd // file permissions
6868
if err != nil {
@@ -83,8 +83,6 @@ func (c *Config) RemoveRuntime(shimPath string) (changed bool, err error) {
8383
runtimeName := shim.RuntimeName(path.Base(shimPath))
8484
l := slog.With("runtime", runtimeName)
8585

86-
cfg := generateConfig(shimPath, runtimeName)
87-
8886
// Containerd config file needs to exist, otherwise return the error
8987
data, err := afero.ReadFile(c.hostFs, c.configPath)
9088
if err != nil {
@@ -97,6 +95,8 @@ func (c *Config) RemoveRuntime(shimPath string) (changed bool, err error) {
9795
return false, nil
9896
}
9997

98+
cfg := generateConfig(shimPath, runtimeName, data)
99+
100100
// Convert the file data to a string and replace the target string with an empty string.
101101
modifiedData := strings.ReplaceAll(string(data), cfg, "")
102102

@@ -113,10 +113,17 @@ func (c *Config) RestartRuntime() error {
113113
return c.restarter.Restart()
114114
}
115115

116-
func generateConfig(shimPath string, runtimeName string) string {
116+
func generateConfig(shimPath string, runtimeName string, configData []byte) string {
117+
// Config domain for containerd 1.0 (config version 2)
118+
domain := "io.containerd.grpc.v1.cri"
119+
if strings.Contains(string(configData), "version = 3") {
120+
// Config domain for containerd 2.0 (config version 3)
121+
domain = "io.containerd.cri.v1.runtime"
122+
}
123+
117124
return fmt.Sprintf(`
118125
# RCM runtime config for %s
119-
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.%s]
126+
[plugins."%s".containerd.runtimes.%s]
120127
runtime_type = "%s"
121-
`, runtimeName, runtimeName, shimPath)
128+
`, runtimeName, domain, runtimeName, shimPath)
122129
}

internal/containerd/configure_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,58 @@ func TestConfig_RemoveRuntime(t *testing.T) {
201201
})
202202
}
203203
}
204+
205+
func TestGenerateConfig_ContainerdVersions(t *testing.T) {
206+
type fields struct {
207+
hostFs afero.Fs
208+
configPath string
209+
}
210+
type args struct {
211+
shimPath string
212+
}
213+
tests := []struct {
214+
name string
215+
fields fields
216+
args args
217+
wantErr bool
218+
wantFileContent string
219+
}{
220+
{"containerd 1.x", fields{
221+
hostFs: tests.FixtureFs("../../testdata/node-installer/containerd/1.x"),
222+
configPath: "/etc/containerd/config.toml",
223+
}, args{"/opt/rcm/bin/containerd-shim-spin-v1"}, false, `version = 2
224+
# RCM runtime config for spin-v1
225+
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.spin-v1]
226+
runtime_type = "/opt/rcm/bin/containerd-shim-spin-v1"
227+
`},
228+
{"containerd 2.x", fields{
229+
hostFs: tests.FixtureFs("../../testdata/node-installer/containerd/2.x"),
230+
configPath: "/etc/containerd/config.toml",
231+
}, args{"/opt/rcm/bin/containerd-shim-spin-v1"}, false, `version = 3
232+
# RCM runtime config for spin-v1
233+
[plugins."io.containerd.cri.v1.runtime".containerd.runtimes.spin-v1]
234+
runtime_type = "/opt/rcm/bin/containerd-shim-spin-v1"
235+
`},
236+
}
237+
for _, tt := range tests {
238+
t.Run(tt.name, func(t *testing.T) {
239+
c := &Config{
240+
hostFs: tt.fields.hostFs,
241+
configPath: tt.fields.configPath,
242+
}
243+
err := c.AddRuntime(tt.args.shimPath)
244+
245+
if tt.wantErr {
246+
require.Error(t, err)
247+
return
248+
}
249+
250+
require.NoError(t, err)
251+
252+
gotContent, err := afero.ReadFile(c.hostFs, c.configPath)
253+
require.NoError(t, err)
254+
255+
assert.Equal(t, tt.wantFileContent, string(gotContent))
256+
})
257+
}
258+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version = 2
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version = 3

0 commit comments

Comments
 (0)