-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Change merge strategy to preserve Plugins when importing Configs #7347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
rayburgemeestre
wants to merge
1
commit into
containerd:main
from
rayburgemeestre:change-merge-strategy-for-containerd-plugins
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
outputConfigintooutputConfig&generateConfigincommand/config.go)