Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cmd/containerd/command/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package command

import (
"context"
"io"
"os"
"path/filepath"

Expand All @@ -34,6 +35,10 @@ import (
)

func outputConfig(ctx context.Context, config *srvconfig.Config) error {
return generateConfig(ctx, config, os.Stdout)
}

func generateConfig(ctx context.Context, config *srvconfig.Config, output io.Writer) error {
plugins, err := server.LoadPlugins(ctx, config)
if err != nil {
return err
Expand Down Expand Up @@ -66,13 +71,13 @@ func outputConfig(ctx context.Context, config *srvconfig.Config) error {
}
}

// for the time being, keep the defaultConfig's version set at 1 so that
// for the time being, keep the defaultConfig's version set at 3 so that
// when a config without a version is loaded from disk and has no version
// set, we assume it's a v1 config. But when generating new configs via
// set, we assume it's a v3 config. But when generating new configs via
// this command, generate the max configuration version
config.Version = version.ConfigVersion

return toml.NewEncoder(os.Stdout).SetIndentTables(true).Encode(config)
return toml.NewEncoder(output).SetIndentTables(true).Encode(config)
}

func defaultConfig() *srvconfig.Config {
Expand Down
167 changes: 167 additions & 0 deletions cmd/containerd/command/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package command

import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/pelletier/go-toml/v2"

srvconfig "github.com/containerd/containerd/v2/cmd/containerd/server/config"
// without the following two includes the behavior of this unit test would be different
_ "github.com/containerd/containerd/v2/plugins/cri"
_ "github.com/containerd/containerd/v2/plugins/cri/runtime"
"github.com/stretchr/testify/assert"
)

func TestCommandConfig(t *testing.T) {
// deprecated but still accepted at the time of writing
data1 := ` version = 2
[plugins."io.containerd.grpc.v1.runtime".registry.configs."registry-1.docker.io".auth]
username = "my-username"
password = "my-password"
`
// the old location, should not be accepted at all
data2 := ` version = 2
[plugins."io.containerd.grpc.v1.cri".registry.configs."registry-1.docker.io".auth]
username = "should-not-be-present"
password = "should-not-be-present"
`
data3 := `
[plugins."io.containerd.grpc.v1.runtime"]
sandbox_image = "my-sandbox-image:1.0"
`
data4 := `
[plugins."io.containerd.grpc.v1.runtime".registry]
config_path = "/my-custom-certs.d-config-path"
`
data5 := `
[plugins."io.containerd.grpc.v1.runtime".containerd.runtimes.runc]
runtime_type = "io.containerd.runc.v2"
[plugins."io.containerd.grpc.v1.runtime".containerd.runtimes.runc.options]
SystemdCgroup = true
`
data6 := `
[plugins.'io.containerd.grpc.v1.cri'.cni]
bin_dir = '/should-not-be-present'
conf_dir = '/should-not-be-present'
`
data7 := `
[plugins.'io.containerd.grpc.v1.runtime'.cni]
bin_dir = '/custom-bin-dir'
`
data8 := `
[plugins.'io.containerd.grpc.v1.runtime'.cni]
conf_dir = '/custom-conf-dir'
`
data9 := `
[plugins]
[plugins."io.containerd.grpc.v1.runtime"]
[plugins."io.containerd.grpc.v1.runtime".containerd]
default_runtime_name = "nvidia"
[plugins."io.containerd.grpc.v1.runtime".containerd.runtimes]
[plugins."io.containerd.grpc.v1.runtime".containerd.runtimes.nvidia]
privileged_without_host_devices = false
runtime_type = "io.containerd.runc.v2"
[plugins."io.containerd.grpc.v1.runtime".containerd.runtimes.nvidia.options]
BinaryName = "/usr/bin/nvidia-container-runtime"
SystemdCgroup = true
`
expectedRuntimes := `
[plugins.'io.containerd.grpc.v1.runtime'.containerd]
default_runtime_name = 'nvidia'

[plugins.'io.containerd.grpc.v1.runtime'.containerd.runtimes]
[plugins.'io.containerd.grpc.v1.runtime'.containerd.runtimes.nvidia]
privileged_without_host_devices = false
runtime_type = 'io.containerd.runc.v2'

[plugins.'io.containerd.grpc.v1.runtime'.containerd.runtimes.nvidia.options]
BinaryName = '/usr/bin/nvidia-container-runtime'
SystemdCgroup = true

[plugins.'io.containerd.grpc.v1.runtime'.containerd.runtimes.runc]
runtime_type = 'io.containerd.runc.v2'

[plugins.'io.containerd.grpc.v1.runtime'.containerd.runtimes.runc.options]
SystemdCgroup = true
`
// currently we cannot invoke testMergeConfig() more than once, due to:
// panic: io.containerd.content.v1.content: plugin: id already registered
asserts := []CheckAsserts{
{Expected: false, Value: "should-not-be-present"},
{Expected: true, Value: "/custom-bin-dir"},
{Expected: true, Value: "/custom-conf-dir"},
{Expected: true, Value: "my-username"},
{Expected: true, Value: "my-password"},
{Expected: true, Value: "my-sandbox-image:1.0"},
{Expected: true, Value: "my-sandbox-image:1.0"},
{Expected: true, Value: "/my-custom-certs.d-config-path"},
{Expected: true, Value: expectedRuntimes},
}
testMergeConfig(t, []string{data1, data2, data3, data4, data5, data6, data7, data8, data9}, asserts)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was worth keeping this test because as opposed to the unit tests in server/config/config_test.go, this one also tests the mapping back and forth (and removal of fields that are not mapping to the structs).

If you do not agree it's that useful, I can remove them. (If we want to remove, I can also undo the splitting of outputConfig into outputConfig & generateConfig in command/config.go)

}

type CheckAsserts struct {
Expected bool
Value string
}

func testMergeConfig(t *testing.T, inputs []string, asserts []CheckAsserts) {
tempDir := t.TempDir()
var result srvconfig.Config

for i, data := range inputs {
// write input to a file on disk
inputFilePath := filepath.Join(tempDir, fmt.Sprintf("data%d.toml", i+1))
err := os.WriteFile(inputFilePath, []byte(data), 0600)
assert.NoError(t, err)

// append it to the main config as an import statement
result.Imports = append(result.Imports, inputFilePath)
}

// now write the main config file that imports all written files so far
mainFilepath := filepath.Join(tempDir, "containerd.toml")
resultString, err := toml.Marshal(result)
assert.NoError(t, err)
err = os.WriteFile(mainFilepath, resultString, 0600)
assert.NoError(t, err)

// now load this config, and see if all the imports results in what we expect
config := defaultConfig()
ctx := context.Background()
err = srvconfig.LoadConfig(ctx, mainFilepath, config)
assert.NoError(t, err)

var buf bytes.Buffer
err = generateConfig(ctx, config, &buf)
assert.NoError(t, err)

for _, item := range asserts {
if item.Expected {
assert.Contains(t, buf.String(), item.Value)
} else {
assert.NotContains(t, buf.String(), item.Value)
}
}
}
4 changes: 0 additions & 4 deletions cmd/containerd/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,6 @@ func mergeConfig(to, from *Config) error {
}

// Replace entire sections instead of merging map's values.
for k, v := range from.Plugins {
to.Plugins[k] = v
}

for k, v := range from.StreamProcessors {
to.StreamProcessors[k] = v
}
Expand Down
132 changes: 132 additions & 0 deletions cmd/containerd/server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
package config

import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"testing"

"github.com/pelletier/go-toml/v2"

"github.com/stretchr/testify/assert"

"github.com/containerd/containerd/v2/version"
Expand Down Expand Up @@ -260,3 +265,130 @@ func TestDecodePluginInV1Config(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, true, pluginConfig["shim_debug"])
}

func TestMergingTwoPluginConfigs(t *testing.T) {
data1 := `
[plugins."io.containerd.grpc.v1.cri".cni]
bin_dir = "/cm/local/apps/kubernetes/current/bin/cni"
`
data2 := `
[plugins."io.containerd.grpc.v1.cri".registry]
config_path = "/cm/local/apps/containerd/var/etc/certs.d"
`
expected := `
[cni]
bin_dir = '/cm/local/apps/kubernetes/current/bin/cni'

[registry]
config_path = '/cm/local/apps/containerd/var/etc/certs.d'
`

testMergeConfig(t, []string{data1, data2}, expected, "io.containerd.grpc.v1.cri")
testMergeConfig(t, []string{data2, data1}, expected, "io.containerd.grpc.v1.cri")
}

func TestMergingTwoPluginConfigsMerge(t *testing.T) {
data1 := `
[plugins."io.containerd.grpc.v1.cri".cni]
bin_dir = "/cm/local/apps/kubernetes/current/bin/cni"
`
data2 := `
[plugins."io.containerd.grpc.v1.cri".cni]
conf_dir = "/tmp"
`
expected := `
[cni]
bin_dir = '/cm/local/apps/kubernetes/current/bin/cni'
conf_dir = '/tmp'
`
testMergeConfig(t, []string{data1, data2}, expected, "io.containerd.grpc.v1.cri")
}

func TestMergingTwoPluginConfigsWithNesting(t *testing.T) {
// Configuration that configures runtime: runc
data1 := `
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
runtime_type = "io.containerd.runc.v2"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
`
// Configuration that configures runtime: nvidia (and makes it default)
data2 := `
[plugins]
[plugins."io.containerd.grpc.v1.cri"]
[plugins."io.containerd.grpc.v1.cri".containerd]
default_runtime_name = "nvidia"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.nvidia]
privileged_without_host_devices = false
runtime_engine = ""
runtime_root = ""
runtime_type = "io.containerd.runc.v2"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.nvidia.options]
BinaryName = "/usr/bin/nvidia-container-runtime"
SystemdCgroup = true
`
// Configuration that changes back default runtime to runc
data3 := `
[plugins]
[plugins."io.containerd.grpc.v1.cri"]
[plugins."io.containerd.grpc.v1.cri".containerd]
default_runtime_name = "runc"
`
expected := `
[containerd]
default_runtime_name = 'runc'

[containerd.runtimes]
[containerd.runtimes.nvidia]
privileged_without_host_devices = false
runtime_engine = ''
runtime_root = ''
runtime_type = 'io.containerd.runc.v2'

[containerd.runtimes.nvidia.options]
BinaryName = '/usr/bin/nvidia-container-runtime'
SystemdCgroup = true

[containerd.runtimes.runc]
runtime_type = 'io.containerd.runc.v2'

[containerd.runtimes.runc.options]
SystemdCgroup = true
`
testMergeConfig(t, []string{data1, data2, data3}, expected, "io.containerd.grpc.v1.cri")
testMergeConfig(t, []string{data2, data1, data3}, expected, "io.containerd.grpc.v1.cri")

expected = strings.Replace(expected, "default_runtime_name = 'runc'", "default_runtime_name = 'nvidia'", 1)
testMergeConfig(t, []string{data3, data1, data2}, expected, "io.containerd.grpc.v1.cri")
}

func testMergeConfig(t *testing.T, inputs []string, expected string, comparePlugin string) {
tempDir := t.TempDir()
var result Config

for i, data := range inputs {
filename := fmt.Sprintf("data%d.toml", i+1)
filepath := filepath.Join(tempDir, filename)
err := os.WriteFile(filepath, []byte(data), 0600)
assert.NoError(t, err)

var tempOut Config
err = LoadConfig(context.Background(), filepath, &tempOut)
assert.NoError(t, err)

if i == 0 {
result = tempOut
} else {
err = mergeConfig(&result, &tempOut)
assert.NoError(t, err)
}
}

criPlugin := result.Plugins[comparePlugin]
var buf bytes.Buffer
if err := toml.NewEncoder(&buf).SetIndentTables(true).Encode(criPlugin); err != nil {
panic(err)
}
assert.Equal(t, strings.TrimLeft(expected, "\n"), buf.String())
}