Skip to content

Commit cfb0b66

Browse files
committed
add limited support for containerd config patch version-awareness
1 parent 09df23d commit cfb0b66

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

pkg/apis/config/v1alpha4/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ type Cluster struct {
7575
// ContainerdConfigPatches are applied to every node's containerd config
7676
// in the order listed.
7777
// These should be toml stringsto be applied as merge patches
78+
// If the version field in these patches doesn't match containerd config, it will not be a applied
79+
// This way you can write configurations that work for both by supplying two patches
7880
ContainerdConfigPatches []string `yaml:"containerdConfigPatches,omitempty" json:"containerdConfigPatches,omitempty"`
7981

8082
// ContainerdConfigPatchesJSON6902 are applied to every node's containerd config
8183
// in the order listed.
8284
// These should be YAML or JSON formatting RFC 6902 JSON patches
85+
// NOTE: These are not currently version-aware.
8386
ContainerdConfigPatchesJSON6902 []string `yaml:"containerdConfigPatchesJSON6902,omitempty" json:"containerdConfigPatchesJSON6902,omitempty"`
8487
}
8588

pkg/build/nodeimage/containerd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const containerdConfigPatchSystemdCgroupFalse = `
3636
`
3737

3838
func configureContainerdSystemdCgroupFalse(containerCmdr exec.Cmder, config string) error {
39-
patched, err := patch.TOML(config, []string{containerdConfigPatchSystemdCgroupFalse}, []string{})
39+
patched, err := patch.ContainerdTOML(config, []string{containerdConfigPatchSystemdCgroupFalse}, []string{})
4040
if err != nil {
4141
return errors.Wrap(err, "failed to configure containerd SystemdCgroup=false")
4242
}

pkg/cluster/internal/create/actions/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (a *Action) Execute(ctx *actions.ActionContext) error {
127127
if err := node.Command("cat", containerdConfigPath).SetStdout(&buff).Run(); err != nil {
128128
return errors.Wrap(err, "failed to read containerd config from node")
129129
}
130-
patched, err := patch.TOML(buff.String(), ctx.Config.ContainerdConfigPatches, ctx.Config.ContainerdConfigPatchesJSON6902)
130+
patched, err := patch.ContainerdTOML(buff.String(), ctx.Config.ContainerdConfigPatches, ctx.Config.ContainerdConfigPatchesJSON6902)
131131
if err != nil {
132132
return errors.Wrap(err, "failed to patch containerd config")
133133
}

pkg/internal/patch/toml.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,31 @@ import (
2929
"sigs.k8s.io/kind/pkg/errors"
3030
)
3131

32-
// TOML patches toPatch with the patches (should be TOML merge patches) and patches6902 (should be JSON 6902 patches)
33-
func TOML(toPatch string, patches []string, patches6902 []string) (string, error) {
32+
// ContainerdTOML patches toPatch with the patches (should be TOML merge patches) and patches6902 (should be JSON 6902 patches)
33+
func ContainerdTOML(toPatch string, patches []string, patches6902 []string) (string, error) {
3434
// convert to JSON for patching
3535
j, err := tomlToJSON([]byte(toPatch))
3636
if err != nil {
3737
return "", err
3838
}
39+
version, err := containerdConfigVersion(toPatch)
40+
if err != nil {
41+
return "", errors.WithStack(err)
42+
}
3943
// apply merge patches
4044
for _, patch := range patches {
4145
pj, err := tomlToJSON([]byte(patch))
46+
if err != nil {
47+
return "", errors.WithStack(err)
48+
}
49+
patchVersion, err := containerdConfigVersion(patch)
50+
if err != nil {
51+
return "", errors.WithStack(err)
52+
}
53+
// skip if patch sets version and version does not match
54+
if patchVersion != 0 && patchVersion != version {
55+
continue
56+
}
4257
if err != nil {
4358
return "", err
4459
}
@@ -64,6 +79,17 @@ func TOML(toPatch string, patches []string, patches6902 []string) (string, error
6479
return jsonToTOMLString(j)
6580
}
6681

82+
func containerdConfigVersion(configTOML string) (int, error) {
83+
type version struct {
84+
Version int `toml:"version,omitempty"`
85+
}
86+
v := version{}
87+
if err := toml.Unmarshal([]byte(configTOML), &v); err != nil {
88+
return 0, errors.WithStack(err)
89+
}
90+
return v.Version, nil
91+
}
92+
6793
// tomlToJSON converts arbitrary TOML to JSON
6894
func tomlToJSON(t []byte) ([]byte, error) {
6995
// we use github.com.pelletier/go-toml here to unmarshal arbitrary TOML to JSON

pkg/internal/patch/toml_test.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"sigs.k8s.io/kind/pkg/internal/assert"
2323
)
2424

25-
func TestTOML(t *testing.T) {
25+
func TestContainerdTOML(t *testing.T) {
2626
t.Parallel()
2727
type testCase struct {
2828
Name string
@@ -49,6 +49,31 @@ func TestTOML(t *testing.T) {
4949
ExpectError: false,
5050
ExpectOutput: `disabled_plugins = ["restart"]
5151
52+
[plugins]
53+
[plugins.cri]
54+
[plugins.cri.containerd]
55+
[plugins.cri.containerd.runtimes]
56+
[plugins.cri.containerd.runtimes.runsc]
57+
runtime_type = "io.containerd.runsc.v1"
58+
[plugins.linux]
59+
shim_debug = true
60+
`,
61+
},
62+
{
63+
Name: "Only matching patches",
64+
ToPatch: `version = 2
65+
66+
disabled_plugins = ["restart"]
67+
68+
[plugins.linux]
69+
shim_debug = true
70+
[plugins.cri.containerd.runtimes.runsc]
71+
runtime_type = "io.containerd.runsc.v1"`,
72+
Patches: []string{"version = 3\ndisabled_plugins=[\"bar\"]", "version = 2\n disabled_plugins=[\"baz\"]"},
73+
ExpectError: false,
74+
ExpectOutput: `disabled_plugins = ["baz"]
75+
version = 2
76+
5277
[plugins]
5378
[plugins.cri]
5479
[plugins.cri.containerd]
@@ -190,7 +215,7 @@ func TestTOML(t *testing.T) {
190215
tc := tc // capture test case
191216
t.Run(tc.Name, func(t *testing.T) {
192217
t.Parallel()
193-
out, err := TOML(tc.ToPatch, tc.Patches, tc.PatchesJSON6902)
218+
out, err := ContainerdTOML(tc.ToPatch, tc.Patches, tc.PatchesJSON6902)
194219
assert.ExpectError(t, tc.ExpectError, err)
195220
if err == nil {
196221
assert.StringEqual(t, tc.ExpectOutput, out)

0 commit comments

Comments
 (0)