X Tutup
Skip to content

Commit ddd4298

Browse files
committed
Migrate current TOML code to github.com/pelletier/go-toml
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
1 parent 499c2f7 commit ddd4298

File tree

8 files changed

+34
-21
lines changed

8 files changed

+34
-21
lines changed

cmd/containerd/command/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ import (
2222
"os"
2323
"path/filepath"
2424

25-
"github.com/BurntSushi/toml"
2625
"github.com/containerd/containerd/defaults"
2726
"github.com/containerd/containerd/images"
2827
"github.com/containerd/containerd/pkg/timeout"
2928
"github.com/containerd/containerd/services/server"
3029
srvconfig "github.com/containerd/containerd/services/server/config"
3130
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
31+
"github.com/pelletier/go-toml"
3232
"github.com/urfave/cli"
3333
)
3434

3535
// Config is a wrapper of server config for printing out.
3636
type Config struct {
3737
*srvconfig.Config
38-
// Plugins overrides `Plugins map[string]toml.Primitive` in server config.
38+
// Plugins overrides `Plugins map[string]toml.Tree` in server config.
3939
Plugins map[string]interface{} `toml:"plugins"`
4040
}
4141

pkg/cri/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"net/url"
2222
"time"
2323

24-
"github.com/BurntSushi/toml"
2524
"github.com/containerd/containerd/log"
2625
"github.com/containerd/containerd/plugin"
26+
"github.com/pelletier/go-toml"
2727
"github.com/pkg/errors"
2828
)
2929

@@ -49,7 +49,7 @@ type Runtime struct {
4949
Root string `toml:"runtime_root" json:"runtimeRoot"`
5050
// Options are config options for the runtime. If options is loaded
5151
// from toml config, it will be toml.Primitive.
52-
Options *toml.Primitive `toml:"options" json:"options"`
52+
Options *toml.Tree `toml:"options" json:"options"`
5353
// PrivilegedWithoutHostDevices overloads the default behaviour for adding host devices to the
5454
// runtime spec when the container is privileged. Defaults to false.
5555
PrivilegedWithoutHostDevices bool `toml:"privileged_without_host_devices" json:"privileged_without_host_devices"`

pkg/cri/config/config_unix.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package config
2020

2121
import (
22-
"github.com/BurntSushi/toml"
2322
"github.com/containerd/containerd"
2423
"github.com/containerd/containerd/pkg/cri/streaming"
2524
)
@@ -39,8 +38,7 @@ func DefaultConfig() PluginConfig {
3938
NoPivot: false,
4039
Runtimes: map[string]Runtime{
4140
"runc": {
42-
Type: "io.containerd.runc.v2",
43-
Options: new(toml.Primitive),
41+
Type: "io.containerd.runc.v2",
4442
},
4543
},
4644
DisableSnapshotAnnotations: true,

pkg/cri/server/helpers.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"strconv"
2424
"strings"
2525

26-
"github.com/BurntSushi/toml"
2726
runhcsoptions "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options"
2827
"github.com/containerd/containerd"
2928
"github.com/containerd/containerd/containers"
@@ -310,7 +309,7 @@ func generateRuntimeOptions(r criconfig.Runtime, c criconfig.Config) (interface{
310309
}, nil
311310
}
312311
options := getRuntimeOptionsType(r.Type)
313-
if err := toml.PrimitiveDecode(*r.Options, options); err != nil {
312+
if err := r.Options.Unmarshal(options); err != nil {
314313
return nil, err
315314
}
316315
return options, nil

pkg/cri/server/helpers_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import (
2121
"io/ioutil"
2222
"testing"
2323

24-
"github.com/BurntSushi/toml"
2524
"github.com/containerd/containerd/oci"
2625
"github.com/containerd/containerd/plugin"
2726
"github.com/containerd/containerd/reference/docker"
2827
"github.com/containerd/containerd/runtime/linux/runctypes"
2928
runcoptions "github.com/containerd/containerd/runtime/v2/runc/options"
3029
imagedigest "github.com/opencontainers/go-digest"
3130
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
31+
"github.com/pelletier/go-toml"
3232
"github.com/stretchr/testify/assert"
3333
"github.com/stretchr/testify/require"
3434

@@ -223,11 +223,16 @@ systemd_cgroup = true
223223
NoNewKeyring = true
224224
`
225225
var nilOptsConfig, nonNilOptsConfig criconfig.Config
226-
_, err := toml.Decode(nilOpts, &nilOptsConfig)
226+
tree, err := toml.Load(nilOpts)
227227
require.NoError(t, err)
228-
_, err = toml.Decode(nonNilOpts, &nonNilOptsConfig)
228+
err = tree.Unmarshal(&nilOptsConfig)
229229
require.NoError(t, err)
230230
require.Len(t, nilOptsConfig.Runtimes, 3)
231+
232+
tree, err = toml.Load(nonNilOpts)
233+
require.NoError(t, err)
234+
err = tree.Unmarshal(&nonNilOptsConfig)
235+
require.NoError(t, err)
231236
require.Len(t, nonNilOptsConfig.Runtimes, 3)
232237

233238
for desc, test := range map[string]struct {

services/server/config/config.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
"path/filepath"
2121
"strings"
2222

23-
"github.com/BurntSushi/toml"
2423
"github.com/imdario/mergo"
24+
"github.com/pelletier/go-toml"
2525
"github.com/pkg/errors"
2626

2727
"github.com/containerd/containerd/errdefs"
@@ -55,7 +55,7 @@ type Config struct {
5555
// required plugin doesn't exist or fails to be initialized or started.
5656
RequiredPlugins []string `toml:"required_plugins"`
5757
// Plugins provides plugin specific configuration for the initialization of a plugin
58-
Plugins map[string]toml.Primitive `toml:"plugins"`
58+
Plugins map[string]toml.Tree `toml:"plugins"`
5959
// OOMScore adjust the containerd's oom score
6060
OOMScore int `toml:"oom_score"`
6161
// Cgroup specifies cgroup information for the containerd daemon process
@@ -209,7 +209,7 @@ func (c *Config) Decode(p *plugin.Registration) (interface{}, error) {
209209
if !ok {
210210
return p.Config, nil
211211
}
212-
if err := toml.PrimitiveDecode(data, p.Config); err != nil {
212+
if err := data.Unmarshal(p.Config); err != nil {
213213
return nil, err
214214
}
215215
return p.Config, nil
@@ -264,10 +264,16 @@ func LoadConfig(path string, out *Config) error {
264264
// loadConfigFile decodes a TOML file at the given path
265265
func loadConfigFile(path string) (*Config, error) {
266266
config := &Config{}
267-
_, err := toml.DecodeFile(path, &config)
267+
268+
file, err := toml.LoadFile(path)
268269
if err != nil {
269-
return nil, err
270+
return nil, errors.Wrapf(err, "failed to load TOML: %s", path)
270271
}
272+
273+
if err := file.Unmarshal(config); err != nil {
274+
return nil, errors.Wrap(err, "failed to unmarshal TOML")
275+
}
276+
271277
return config, nil
272278
}
273279

snapshots/devmapper/config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import (
2222
"fmt"
2323
"os"
2424

25-
"github.com/BurntSushi/toml"
2625
"github.com/docker/go-units"
2726
"github.com/hashicorp/go-multierror"
27+
"github.com/pelletier/go-toml"
2828
"github.com/pkg/errors"
2929
)
3030

@@ -56,8 +56,13 @@ func LoadConfig(path string) (*Config, error) {
5656
}
5757

5858
config := Config{}
59-
if _, err := toml.DecodeFile(path, &config); err != nil {
60-
return nil, errors.Wrapf(err, "failed to unmarshal data at '%s'", path)
59+
file, err := toml.LoadFile(path)
60+
if err != nil {
61+
return nil, errors.Wrapf(err, "failed to open devmapepr TOML: %s", path)
62+
}
63+
64+
if err := file.Unmarshal(&config); err != nil {
65+
return nil, errors.Wrap(err, "failed to unmarshal devmapper TOML")
6166
}
6267

6368
if err := config.parse(); err != nil {

snapshots/devmapper/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"os"
2424
"testing"
2525

26-
"github.com/BurntSushi/toml"
2726
"github.com/hashicorp/go-multierror"
27+
"github.com/pelletier/go-toml"
2828
"gotest.tools/v3/assert"
2929
is "gotest.tools/v3/assert/cmp"
3030
)

0 commit comments

Comments
 (0)
X Tutup