From bca0857530d1ead52d03595b1558bfe98112d187 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Thu, 22 Aug 2019 17:22:25 -0700 Subject: [PATCH] Fix toml plugin decoding Do not rely on toml metadata when decoding plugin's configs as it's not possible to merge toml.MetaData structs during import. Signed-off-by: Maksym Pavlenko --- services/server/config/config.go | 7 ++----- services/server/config/config_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/services/server/config/config.go b/services/server/config/config.go index 7c47cd745..57dc71585 100644 --- a/services/server/config/config.go +++ b/services/server/config/config.go @@ -64,8 +64,6 @@ type Config struct { Imports []string `toml:"imports"` StreamProcessors []StreamProcessor `toml:"stream_processors"` - - md toml.MetaData } // StreamProcessor provides configuration for diff content processors @@ -203,7 +201,7 @@ func (c *Config) Decode(p *plugin.Registration) (interface{}, error) { if !ok { return p.Config, nil } - if err := c.md.PrimitiveDecode(data, p.Config); err != nil { + if err := toml.PrimitiveDecode(data, p.Config); err != nil { return nil, err } return p.Config, nil @@ -258,11 +256,10 @@ func LoadConfig(path string, out *Config) error { // loadConfigFile decodes a TOML file at the given path func loadConfigFile(path string) (*Config, error) { config := &Config{} - md, err := toml.DecodeFile(path, &config) + _, err := toml.DecodeFile(path, &config) if err != nil { return nil, err } - config.md = md return config, nil } diff --git a/services/server/config/config_test.go b/services/server/config/config_test.go index 56f9cb030..8e65a4630 100644 --- a/services/server/config/config_test.go +++ b/services/server/config/config_test.go @@ -23,6 +23,8 @@ import ( "testing" "gotest.tools/assert" + + "github.com/containerd/containerd/plugin" ) func TestMergeConfigs(t *testing.T) { @@ -161,3 +163,28 @@ imports = ["data1.toml", "data2.toml"] filepath.Join(tempDir, "data2.toml"), }, out.Imports) } + +func TestDecodePlugin(t *testing.T) { + data := ` +version = 1 +[plugins.linux] + shim_debug = true +` + + tempDir, err := ioutil.TempDir("", "containerd_") + assert.NilError(t, err) + defer os.RemoveAll(tempDir) + + path := filepath.Join(tempDir, "config.toml") + err = ioutil.WriteFile(path, []byte(data), 0600) + assert.NilError(t, err) + + var out Config + err = LoadConfig(path, &out) + assert.NilError(t, err) + + pluginConfig := map[string]interface{}{} + _, err = out.Decode(&plugin.Registration{ID: "linux", Config: &pluginConfig}) + assert.NilError(t, err) + assert.Equal(t, true, pluginConfig["shim_debug"]) +}