From ddd4298a1043fe50bebd47257cedbe79c54f9b16 Mon Sep 17 00:00:00 2001 From: Maksym Pavlenko Date: Thu, 25 Mar 2021 13:13:33 -0700 Subject: [PATCH] Migrate current TOML code to github.com/pelletier/go-toml Signed-off-by: Maksym Pavlenko --- cmd/containerd/command/config.go | 4 ++-- pkg/cri/config/config.go | 4 ++-- pkg/cri/config/config_unix.go | 4 +--- pkg/cri/server/helpers.go | 3 +-- pkg/cri/server/helpers_test.go | 11 ++++++++--- services/server/config/config.go | 16 +++++++++++----- snapshots/devmapper/config.go | 11 ++++++++--- snapshots/devmapper/config_test.go | 2 +- 8 files changed, 34 insertions(+), 21 deletions(-) diff --git a/cmd/containerd/command/config.go b/cmd/containerd/command/config.go index f843b8041..7282f476c 100644 --- a/cmd/containerd/command/config.go +++ b/cmd/containerd/command/config.go @@ -22,20 +22,20 @@ import ( "os" "path/filepath" - "github.com/BurntSushi/toml" "github.com/containerd/containerd/defaults" "github.com/containerd/containerd/images" "github.com/containerd/containerd/pkg/timeout" "github.com/containerd/containerd/services/server" srvconfig "github.com/containerd/containerd/services/server/config" ocispec "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/pelletier/go-toml" "github.com/urfave/cli" ) // Config is a wrapper of server config for printing out. type Config struct { *srvconfig.Config - // Plugins overrides `Plugins map[string]toml.Primitive` in server config. + // Plugins overrides `Plugins map[string]toml.Tree` in server config. Plugins map[string]interface{} `toml:"plugins"` } diff --git a/pkg/cri/config/config.go b/pkg/cri/config/config.go index ea3005736..4280d0a34 100644 --- a/pkg/cri/config/config.go +++ b/pkg/cri/config/config.go @@ -21,9 +21,9 @@ import ( "net/url" "time" - "github.com/BurntSushi/toml" "github.com/containerd/containerd/log" "github.com/containerd/containerd/plugin" + "github.com/pelletier/go-toml" "github.com/pkg/errors" ) @@ -49,7 +49,7 @@ type Runtime struct { Root string `toml:"runtime_root" json:"runtimeRoot"` // Options are config options for the runtime. If options is loaded // from toml config, it will be toml.Primitive. - Options *toml.Primitive `toml:"options" json:"options"` + Options *toml.Tree `toml:"options" json:"options"` // PrivilegedWithoutHostDevices overloads the default behaviour for adding host devices to the // runtime spec when the container is privileged. Defaults to false. PrivilegedWithoutHostDevices bool `toml:"privileged_without_host_devices" json:"privileged_without_host_devices"` diff --git a/pkg/cri/config/config_unix.go b/pkg/cri/config/config_unix.go index 07011c454..06dc483b0 100644 --- a/pkg/cri/config/config_unix.go +++ b/pkg/cri/config/config_unix.go @@ -19,7 +19,6 @@ package config import ( - "github.com/BurntSushi/toml" "github.com/containerd/containerd" "github.com/containerd/containerd/pkg/cri/streaming" ) @@ -39,8 +38,7 @@ func DefaultConfig() PluginConfig { NoPivot: false, Runtimes: map[string]Runtime{ "runc": { - Type: "io.containerd.runc.v2", - Options: new(toml.Primitive), + Type: "io.containerd.runc.v2", }, }, DisableSnapshotAnnotations: true, diff --git a/pkg/cri/server/helpers.go b/pkg/cri/server/helpers.go index 40145a23e..3c772d403 100644 --- a/pkg/cri/server/helpers.go +++ b/pkg/cri/server/helpers.go @@ -23,7 +23,6 @@ import ( "strconv" "strings" - "github.com/BurntSushi/toml" runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options" "github.com/containerd/containerd" "github.com/containerd/containerd/containers" @@ -310,7 +309,7 @@ func generateRuntimeOptions(r criconfig.Runtime, c criconfig.Config) (interface{ }, nil } options := getRuntimeOptionsType(r.Type) - if err := toml.PrimitiveDecode(*r.Options, options); err != nil { + if err := r.Options.Unmarshal(options); err != nil { return nil, err } return options, nil diff --git a/pkg/cri/server/helpers_test.go b/pkg/cri/server/helpers_test.go index 922695f09..4605ac9b2 100644 --- a/pkg/cri/server/helpers_test.go +++ b/pkg/cri/server/helpers_test.go @@ -21,7 +21,6 @@ import ( "io/ioutil" "testing" - "github.com/BurntSushi/toml" "github.com/containerd/containerd/oci" "github.com/containerd/containerd/plugin" "github.com/containerd/containerd/reference/docker" @@ -29,6 +28,7 @@ import ( runcoptions "github.com/containerd/containerd/runtime/v2/runc/options" imagedigest "github.com/opencontainers/go-digest" runtimespec "github.com/opencontainers/runtime-spec/specs-go" + "github.com/pelletier/go-toml" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -223,11 +223,16 @@ systemd_cgroup = true NoNewKeyring = true ` var nilOptsConfig, nonNilOptsConfig criconfig.Config - _, err := toml.Decode(nilOpts, &nilOptsConfig) + tree, err := toml.Load(nilOpts) require.NoError(t, err) - _, err = toml.Decode(nonNilOpts, &nonNilOptsConfig) + err = tree.Unmarshal(&nilOptsConfig) require.NoError(t, err) require.Len(t, nilOptsConfig.Runtimes, 3) + + tree, err = toml.Load(nonNilOpts) + require.NoError(t, err) + err = tree.Unmarshal(&nonNilOptsConfig) + require.NoError(t, err) require.Len(t, nonNilOptsConfig.Runtimes, 3) for desc, test := range map[string]struct { diff --git a/services/server/config/config.go b/services/server/config/config.go index 29ac6decb..f82cfb310 100644 --- a/services/server/config/config.go +++ b/services/server/config/config.go @@ -20,8 +20,8 @@ import ( "path/filepath" "strings" - "github.com/BurntSushi/toml" "github.com/imdario/mergo" + "github.com/pelletier/go-toml" "github.com/pkg/errors" "github.com/containerd/containerd/errdefs" @@ -55,7 +55,7 @@ type Config struct { // required plugin doesn't exist or fails to be initialized or started. RequiredPlugins []string `toml:"required_plugins"` // Plugins provides plugin specific configuration for the initialization of a plugin - Plugins map[string]toml.Primitive `toml:"plugins"` + Plugins map[string]toml.Tree `toml:"plugins"` // OOMScore adjust the containerd's oom score OOMScore int `toml:"oom_score"` // Cgroup specifies cgroup information for the containerd daemon process @@ -209,7 +209,7 @@ func (c *Config) Decode(p *plugin.Registration) (interface{}, error) { if !ok { return p.Config, nil } - if err := toml.PrimitiveDecode(data, p.Config); err != nil { + if err := data.Unmarshal(p.Config); err != nil { return nil, err } return p.Config, nil @@ -264,10 +264,16 @@ func LoadConfig(path string, out *Config) error { // loadConfigFile decodes a TOML file at the given path func loadConfigFile(path string) (*Config, error) { config := &Config{} - _, err := toml.DecodeFile(path, &config) + + file, err := toml.LoadFile(path) if err != nil { - return nil, err + return nil, errors.Wrapf(err, "failed to load TOML: %s", path) } + + if err := file.Unmarshal(config); err != nil { + return nil, errors.Wrap(err, "failed to unmarshal TOML") + } + return config, nil } diff --git a/snapshots/devmapper/config.go b/snapshots/devmapper/config.go index 918eb074e..24e36b856 100644 --- a/snapshots/devmapper/config.go +++ b/snapshots/devmapper/config.go @@ -22,9 +22,9 @@ import ( "fmt" "os" - "github.com/BurntSushi/toml" "github.com/docker/go-units" "github.com/hashicorp/go-multierror" + "github.com/pelletier/go-toml" "github.com/pkg/errors" ) @@ -56,8 +56,13 @@ func LoadConfig(path string) (*Config, error) { } config := Config{} - if _, err := toml.DecodeFile(path, &config); err != nil { - return nil, errors.Wrapf(err, "failed to unmarshal data at '%s'", path) + file, err := toml.LoadFile(path) + if err != nil { + return nil, errors.Wrapf(err, "failed to open devmapepr TOML: %s", path) + } + + if err := file.Unmarshal(&config); err != nil { + return nil, errors.Wrap(err, "failed to unmarshal devmapper TOML") } if err := config.parse(); err != nil { diff --git a/snapshots/devmapper/config_test.go b/snapshots/devmapper/config_test.go index bae0ed616..02ad8360e 100644 --- a/snapshots/devmapper/config_test.go +++ b/snapshots/devmapper/config_test.go @@ -23,8 +23,8 @@ import ( "os" "testing" - "github.com/BurntSushi/toml" "github.com/hashicorp/go-multierror" + "github.com/pelletier/go-toml" "gotest.tools/v3/assert" is "gotest.tools/v3/assert/cmp" )