Skip to content

Commit 4c0b3bd

Browse files
authored
Merge pull request #4117 from twz123/containerd-config-detection
Improve k0s-managed containerd config detection
2 parents ae27910 + 0780516 commit 4c0b3bd

File tree

2 files changed

+69
-13
lines changed

2 files changed

+69
-13
lines changed

pkg/component/worker/containerd/component.go

+14-12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"context"
2323
"crypto/md5"
2424
"encoding/hex"
25+
"errors"
2526
"fmt"
2627
"io"
2728
"os"
@@ -47,10 +48,7 @@ import (
4748
"github.com/k0sproject/k0s/pkg/supervisor"
4849
)
4950

50-
const magicMarker = "# k0s_managed=true"
51-
52-
const confTmpl = `
53-
# k0s_managed=true
51+
const confTmpl = `# k0s_managed=true
5452
# This is a placeholder configuration for k0s managed containerd.
5553
# If you wish to override the config, remove the first line and replace this file with your custom configuration.
5654
# For reference see https://github.com/containerd/containerd/blob/main/docs/man/containerd-config.toml.5.md
@@ -322,11 +320,12 @@ func (c *Component) Stop() error {
322320
// This is the md5sum of the default k0s containerd config file before 1.27
323321
const pre1_27ConfigSum = "59039b43303742a5496b13fd57f9beec"
324322

325-
// isK0sManagedConfig checks if the config file is k0s managed
326-
// - If the config file does not exist, it's k0s managed
327-
// - If the config file md5sum matches the pre 1.27 config, it's k0s managed
328-
// - If the config file has the magic marker, it's k0s managed
329-
func isK0sManagedConfig(path string) (bool, error) {
323+
// isK0sManagedConfig checks if the config file is k0s managed:
324+
// - If the config file doesn't exist, it's k0s managed.
325+
// - If the config file's md5sum matches the pre 1.27 config, it's k0s managed.
326+
// - If the config file starts with the magic marker line "# k0s_managed=true",
327+
// it's k0s managed.
328+
func isK0sManagedConfig(path string) (_ bool, err error) {
330329
// If the file does not exist, it's k0s managed (new install)
331330
if !file.Exists(path) {
332331
return true, nil
@@ -343,14 +342,17 @@ func isK0sManagedConfig(path string) (bool, error) {
343342
if err != nil {
344343
return false, err
345344
}
346-
defer f.Close()
345+
defer func() { err = errors.Join(err, f.Close()) }()
347346
scanner := bufio.NewScanner(f)
348347
for scanner.Scan() {
349-
if strings.Contains(scanner.Text(), magicMarker) {
348+
switch scanner.Text() {
349+
case "": // K0s versions before 1.30 had a leading empty line.
350+
continue
351+
case "# k0s_managed=true":
350352
return true, nil
351353
}
352354
}
353-
return false, nil
355+
return false, scanner.Err()
354356
}
355357

356358
func isPre1_27ManagedConfig(path string) (bool, error) {

pkg/component/worker/containerd/component_test.go

+55-1
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,67 @@ import (
2121
"path/filepath"
2222
"testing"
2323

24+
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
25+
workerconfig "github.com/k0sproject/k0s/pkg/component/worker/config"
26+
"github.com/k0sproject/k0s/pkg/config"
27+
2428
"github.com/stretchr/testify/require"
2529
)
2630

2731
func Test_isK0sManagedConfig(t *testing.T) {
2832

2933
t.Run("should return true if file does not exist", func(t *testing.T) {
30-
isManaged, err := isK0sManagedConfig("non-existent.toml")
34+
isManaged, err := isK0sManagedConfig(filepath.Join(t.TempDir(), "non-existent.toml"))
35+
require.NoError(t, err)
36+
require.True(t, isManaged)
37+
})
38+
39+
t.Run("should return true for generated default config", func(t *testing.T) {
40+
defaultConfigPath := filepath.Join(t.TempDir(), "default.toml")
41+
42+
underTest := Component{
43+
K0sVars: &config.CfgVars{
44+
RunDir: /* The merged config file will be written here: */ t.TempDir(),
45+
},
46+
confPath:/* The main config file will be written here: */ defaultConfigPath,
47+
importsPath:/* some empty dir: */ t.TempDir(),
48+
Profile:/* Some non-nil pause image: */ &workerconfig.Profile{PauseImage: &v1beta1.ImageSpec{}},
49+
}
50+
err := underTest.setupConfig()
51+
52+
require.NoError(t, err)
53+
require.FileExists(t, defaultConfigPath, "The generated config file is missing.")
54+
55+
isManaged, err := isK0sManagedConfig(defaultConfigPath)
56+
require.NoError(t, err)
57+
require.True(t, isManaged, "The generated config file should qualify as k0s-managed, but doesn't.")
58+
})
59+
60+
t.Run("should return false if file has no marker", func(t *testing.T) {
61+
unmanagedPath := filepath.Join(t.TempDir(), "unmanaged.toml")
62+
require.NoError(t, os.WriteFile(unmanagedPath, []byte(" # k0s_managed=true"), 0644))
63+
64+
isManaged, err := isK0sManagedConfig(unmanagedPath)
65+
require.NoError(t, err)
66+
require.False(t, isManaged)
67+
})
68+
69+
t.Run("should return true for pre-1.30 generated config", func(t *testing.T) {
70+
tmpDir := t.TempDir()
71+
configPath := filepath.Join(tmpDir, "containerd.toml")
72+
cfg := `
73+
# k0s_managed=true
74+
# This is a placeholder configuration for k0s managed containerD.
75+
# If you wish to override the config, remove the first line and replace this file with your custom configuration.
76+
# For reference see https://github.com/containerd/containerd/blob/main/docs/man/containerd-config.toml.5.md
77+
version = 2
78+
imports = [
79+
"/run/k0s/containerd-cri.toml",
80+
]
81+
`
82+
err := os.WriteFile(configPath, []byte(cfg), 0644)
83+
require.NoError(t, err)
84+
isManaged, err := isK0sManagedConfig(configPath)
3185
require.NoError(t, err)
3286
require.True(t, isManaged)
3387
})

0 commit comments

Comments
 (0)