Skip to content

Commit 0359c85

Browse files
committed
chore: unify toml packages being used
Drop BurntSushi one, and use /v2 of pelletier package. There is indirect use of v1 which should hopefully go away once we move away from sonobouy. Signed-off-by: Andrey Smirnov <andrey.smirnov@siderolabs.com>
1 parent 4feb94c commit 0359c85

File tree

8 files changed

+68
-51
lines changed

8 files changed

+68
-51
lines changed

go.mod

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ require (
4343
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.5.2
4444
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azcertificates v1.1.0
4545
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azkeys v1.1.0
46-
github.com/BurntSushi/toml v1.3.2
4746
github.com/alexflint/go-filemutex v1.3.0
4847
github.com/aws/aws-sdk-go-v2/config v1.27.11
4948
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1
@@ -113,7 +112,7 @@ require (
113112
github.com/opencontainers/image-spec v1.1.0
114113
github.com/opencontainers/runtime-spec v1.2.0
115114
github.com/packethost/packngo v0.31.0
116-
github.com/pelletier/go-toml v1.9.5
115+
github.com/pelletier/go-toml/v2 v2.2.2
117116
github.com/pin/tftp/v3 v3.1.0
118117
github.com/pmorjan/kmod v1.1.1
119118
github.com/prometheus/procfs v0.14.0
@@ -302,7 +301,7 @@ require (
302301
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626 // indirect
303302
github.com/opencontainers/selinux v1.11.0 // indirect
304303
github.com/opentracing/opentracing-go v1.2.0 // indirect
305-
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
304+
github.com/pelletier/go-toml v1.9.5 // indirect
306305
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
307306
github.com/pierrec/lz4/v4 v4.1.15 // indirect
308307
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 h1:XHOnouVk1mx
6262
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2/go.mod h1:wP83P5OoQ5p6ip3ScPr0BAq0BvuPAvacpEuSzyouqAI=
6363
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
6464
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
65-
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
66-
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
6765
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
6866
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
6967
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=

internal/pkg/containers/cri/containerd/containerd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"bytes"
1010
"path/filepath"
1111

12-
"github.com/BurntSushi/toml"
12+
"github.com/pelletier/go-toml/v2"
1313

1414
"github.com/siderolabs/talos/pkg/machinery/config/config"
1515
"github.com/siderolabs/talos/pkg/machinery/constants"
@@ -39,7 +39,7 @@ func GenerateCRIConfig(r config.Registries) ([]byte, error) {
3939

4040
var buf bytes.Buffer
4141

42-
if err := toml.NewEncoder(&buf).Encode(&ctrdCfg); err != nil {
42+
if err := toml.NewEncoder(&buf).SetIndentTables(true).Encode(&ctrdCfg); err != nil {
4343
return nil, err
4444
}
4545

internal/pkg/containers/cri/containerd/hosts.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"strings"
1515

1616
"github.com/containerd/containerd/v2/core/remotes/docker"
17-
"github.com/pelletier/go-toml"
17+
"github.com/pelletier/go-toml/v2"
1818

1919
"github.com/siderolabs/talos/pkg/machinery/config/config"
2020
)
@@ -128,11 +128,14 @@ func GenerateHosts(cfg config.Registries, basePath string) (*HostsConfig, error)
128128

129129
configureEndpoint(u.Host, directoryName, hostsToml.HostConfigs[endpoint], directory)
130130

131-
tomlBytes, err := toml.Marshal(hostsToml)
132-
if err != nil {
131+
var tomlBuf bytes.Buffer
132+
133+
if err := toml.NewEncoder(&tomlBuf).SetIndentTables(true).Encode(hostsToml); err != nil {
133134
return nil, err
134135
}
135136

137+
tomlBytes := tomlBuf.Bytes()
138+
136139
// this is an ugly hack, and neither TOML format nor go-toml library make it easier
137140
//
138141
// we need to marshal each endpoint in the order they are specified in the config, but go-toml defines
@@ -146,7 +149,7 @@ func GenerateHosts(cfg config.Registries, basePath string) (*HostsConfig, error)
146149
// [host."bar.foo"]
147150
//
148151
// but this is invalid TOML, as `[host]' is repeated, so we do an ugly hack and remove it below
149-
const hostPrefix = "\n[host]\n"
152+
const hostPrefix = "[host]\n"
150153

151154
if i > 0 {
152155
if bytes.HasPrefix(tomlBytes, []byte(hostPrefix)) {
@@ -206,16 +209,17 @@ func GenerateHosts(cfg config.Registries, basePath string) (*HostsConfig, error)
206209

207210
configureEndpoint(hostname, directoryName, hostsToml.HostConfigs[defaultHost], directory)
208211

209-
marshaled, err := toml.Marshal(hostsToml)
210-
if err != nil {
212+
var tomlBuf bytes.Buffer
213+
214+
if err = toml.NewEncoder(&tomlBuf).SetIndentTables(true).Encode(hostsToml); err != nil {
211215
return nil, err
212216
}
213217

214218
directory.Files = append(directory.Files,
215219
&HostsFile{
216220
Name: "hosts.toml",
217221
Mode: 0o600,
218-
Contents: marshaled,
222+
Contents: tomlBuf.Bytes(),
219223
},
220224
)
221225

internal/pkg/containers/cri/containerd/hosts_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestGenerateHostsWithTLS(t *testing.T) {
5959
{
6060
Name: "hosts.toml",
6161
Mode: 0o600,
62-
Contents: []byte("\n[host]\n\n [host.\"https://registry-1.docker.io\"]\n capabilities = [\"pull\", \"resolve\"]\n\n [host.\"https://registry-2.docker.io\"]\n capabilities = [\"pull\", \"resolve\"]\n skip_verify = true\n"), //nolint:lll
62+
Contents: []byte("[host]\n [host.'https://registry-1.docker.io']\n capabilities = ['pull', 'resolve']\n [host.'https://registry-2.docker.io']\n capabilities = ['pull', 'resolve']\n skip_verify = true\n"), //nolint:lll
6363
},
6464
},
6565
},
@@ -83,7 +83,7 @@ func TestGenerateHostsWithTLS(t *testing.T) {
8383
{
8484
Name: "hosts.toml",
8585
Mode: 0o600,
86-
Contents: []byte("\n[host]\n\n [host.\"https://some.host:123\"]\n ca = \"/etc/cri/conf.d/hosts/some.host_123_/some.host:123-ca.crt\"\n client = [[\"/etc/cri/conf.d/hosts/some.host_123_/some.host:123-client.crt\", \"/etc/cri/conf.d/hosts/some.host_123_/some.host:123-client.key\"]]\n skip_verify = true\n"), //nolint:lll
86+
Contents: []byte("[host]\n [host.'https://some.host:123']\n ca = '/etc/cri/conf.d/hosts/some.host_123_/some.host:123-ca.crt'\n client = [['/etc/cri/conf.d/hosts/some.host_123_/some.host:123-client.crt', '/etc/cri/conf.d/hosts/some.host_123_/some.host:123-client.key']]\n skip_verify = true\n"), //nolint:lll
8787
},
8888
},
8989
},
@@ -92,7 +92,7 @@ func TestGenerateHostsWithTLS(t *testing.T) {
9292
{
9393
Name: "hosts.toml",
9494
Mode: 0o600,
95-
Contents: []byte("\n[host]\n\n [host.\"https://registry-2.docker.io\"]\n skip_verify = true\n"),
95+
Contents: []byte("[host]\n [host.'https://registry-2.docker.io']\n skip_verify = true\n"),
9696
},
9797
},
9898
},
@@ -132,7 +132,7 @@ func TestGenerateHostsWithoutTLS(t *testing.T) {
132132
{
133133
Name: "hosts.toml",
134134
Mode: 0o600,
135-
Contents: []byte("\n[host]\n\n [host.\"https://registry-1.docker.io\"]\n capabilities = [\"pull\", \"resolve\"]\n\n [host.\"https://registry-2.docker.io\"]\n capabilities = [\"pull\", \"resolve\"]\n"), //nolint:lll
135+
Contents: []byte("[host]\n [host.'https://registry-1.docker.io']\n capabilities = ['pull', 'resolve']\n [host.'https://registry-2.docker.io']\n capabilities = ['pull', 'resolve']\n"), //nolint:lll
136136
},
137137
},
138138
},
@@ -141,7 +141,7 @@ func TestGenerateHostsWithoutTLS(t *testing.T) {
141141
{
142142
Name: "hosts.toml",
143143
Mode: 0o600,
144-
Contents: []byte("\n[host]\n\n [host.\"https://some.host:123\"]\n"),
144+
Contents: []byte("[host]\n [host.'https://some.host:123']\n"),
145145
},
146146
},
147147
},
@@ -150,7 +150,7 @@ func TestGenerateHostsWithoutTLS(t *testing.T) {
150150
{
151151
Name: "hosts.toml",
152152
Mode: 0o600,
153-
Contents: []byte("\n[host]\n\n [host.\"https://my-registry\"]\n capabilities = [\"pull\", \"resolve\"]\n"),
153+
Contents: []byte("[host]\n [host.'https://my-registry']\n capabilities = ['pull', 'resolve']\n"),
154154
},
155155
},
156156
},
@@ -205,7 +205,7 @@ func TestGenerateHostsTLSWildcard(t *testing.T) {
205205
{
206206
Name: "hosts.toml",
207207
Mode: 0o600,
208-
Contents: []byte("\n[host]\n\n [host.\"https://my-registry1\"]\n ca = \"/etc/cri/conf.d/hosts/_default/my-registry1-ca.crt\"\n capabilities = [\"pull\", \"resolve\"]\n\n [host.\"https://my-registry2\"]\n capabilities = [\"pull\", \"resolve\"]\n"), //nolint:lll
208+
Contents: []byte("[host]\n [host.'https://my-registry1']\n capabilities = ['pull', 'resolve']\n ca = '/etc/cri/conf.d/hosts/_default/my-registry1-ca.crt'\n [host.'https://my-registry2']\n capabilities = ['pull', 'resolve']\n"), //nolint:lll
209209
},
210210
},
211211
},
@@ -219,7 +219,7 @@ func TestGenerateHostsTLSWildcard(t *testing.T) {
219219
{
220220
Name: "hosts.toml",
221221
Mode: 0o600,
222-
Contents: []byte("\n[host]\n\n [host.\"https://my-registry1\"]\n ca = \"/etc/cri/conf.d/hosts/my-registry1/my-registry1-ca.crt\"\n"),
222+
Contents: []byte("[host]\n [host.'https://my-registry1']\n ca = '/etc/cri/conf.d/hosts/my-registry1/my-registry1-ca.crt'\n"),
223223
},
224224
},
225225
},
@@ -269,7 +269,7 @@ func TestGenerateHostsWithHarbor(t *testing.T) {
269269
{
270270
Name: "hosts.toml",
271271
Mode: 0o600,
272-
Contents: []byte("\n[host]\n\n [host.\"https://harbor/v2/mirrors/proxy.docker.io\"]\n capabilities = [\"pull\", \"resolve\"]\n override_path = true\n skip_verify = true\n"),
272+
Contents: []byte("[host]\n [host.'https://harbor/v2/mirrors/proxy.docker.io']\n capabilities = ['pull', 'resolve']\n override_path = true\n skip_verify = true\n"),
273273
},
274274
},
275275
},
@@ -278,7 +278,7 @@ func TestGenerateHostsWithHarbor(t *testing.T) {
278278
{
279279
Name: "hosts.toml",
280280
Mode: 0o600,
281-
Contents: []byte("\n[host]\n\n [host.\"https://harbor/v2/mirrors/proxy.ghcr.io\"]\n capabilities = [\"pull\", \"resolve\"]\n override_path = true\n skip_verify = true\n"),
281+
Contents: []byte("[host]\n [host.'https://harbor/v2/mirrors/proxy.ghcr.io']\n capabilities = ['pull', 'resolve']\n override_path = true\n skip_verify = true\n"),
282282
},
283283
},
284284
},
@@ -287,7 +287,7 @@ func TestGenerateHostsWithHarbor(t *testing.T) {
287287
{
288288
Name: "hosts.toml",
289289
Mode: 0o600,
290-
Contents: []byte("\n[host]\n\n [host.\"https://harbor\"]\n skip_verify = true\n"),
290+
Contents: []byte("[host]\n [host.'https://harbor']\n skip_verify = true\n"),
291291
},
292292
},
293293
},
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[plugins]
2-
[plugins."io.containerd.grpc.v1.cri"]
3-
[plugins."io.containerd.grpc.v1.cri".registry]
4-
config_path = "/etc/cri/conf.d/hosts"
5-
[plugins."io.containerd.grpc.v1.cri".registry.configs]
6-
[plugins."io.containerd.grpc.v1.cri".registry.configs."some.host:123"]
7-
[plugins."io.containerd.grpc.v1.cri".registry.configs."some.host:123".auth]
8-
username = "root"
9-
password = "secret"
10-
auth = "auth"
11-
identitytoken = "token"
2+
[plugins.'io.containerd.grpc.v1.cri']
3+
[plugins.'io.containerd.grpc.v1.cri'.registry]
4+
config_path = '/etc/cri/conf.d/hosts'
5+
6+
[plugins.'io.containerd.grpc.v1.cri'.registry.configs]
7+
[plugins.'io.containerd.grpc.v1.cri'.registry.configs.'some.host:123']
8+
[plugins.'io.containerd.grpc.v1.cri'.registry.configs.'some.host:123'.auth]
9+
username = 'root'
10+
password = 'secret'
11+
auth = 'auth'
12+
identitytoken = 'token'

internal/pkg/toml/merge.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,36 @@ package toml
77
import (
88
"bytes"
99
"fmt"
10+
"os"
1011

11-
"github.com/BurntSushi/toml"
12+
"github.com/pelletier/go-toml/v2"
1213

1314
"github.com/siderolabs/talos/pkg/machinery/config/merge"
1415
)
1516

17+
func tomlDecodeFile(path string, dest any) error {
18+
f, err := os.Open(path)
19+
if err != nil {
20+
return err
21+
}
22+
23+
defer f.Close() //nolint:errcheck
24+
25+
return toml.NewDecoder(f).Decode(dest)
26+
}
27+
1628
// Merge several TOML documents in files into one.
1729
//
18-
// Merge process relies on generic map[string]interface{} merge which might not always be correct.
30+
// Merge process relies on generic map[string]any merge which might not always be correct.
1931
func Merge(parts []string) ([]byte, error) {
20-
merged := map[string]interface{}{}
32+
merged := map[string]any{}
2133

2234
var header []byte
2335

2436
for _, part := range parts {
25-
partial := map[string]interface{}{}
37+
partial := map[string]any{}
2638

27-
if _, err := toml.DecodeFile(part, &partial); err != nil {
39+
if err := tomlDecodeFile(part, &partial); err != nil {
2840
return nil, fmt.Errorf("error decoding %q: %w", part, err)
2941
}
3042

@@ -40,7 +52,7 @@ func Merge(parts []string) ([]byte, error) {
4052
_, _ = out.Write(header)
4153
_ = out.WriteByte('\n')
4254

43-
if err := toml.NewEncoder(&out).Encode(merged); err != nil {
55+
if err := toml.NewEncoder(&out).SetIndentTables(true).Encode(merged); err != nil {
4456
return nil, fmt.Errorf("error encoding merged config: %w", err)
4557
}
4658

internal/pkg/toml/testdata/expected.toml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55
version = 2
66

77
[metrics]
8-
address = "0.0.0.0:11234"
8+
address = '0.0.0.0:11234'
99

1010
[plugins]
11-
[plugins."io.containerd.grpc.v1.cri"]
12-
sandbox_image = "registry.k8s.io/pause:3.8"
13-
[plugins."io.containerd.grpc.v1.cri".containerd]
14-
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
15-
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
11+
[plugins.'io.containerd.grpc.v1.cri']
12+
sandbox_image = 'registry.k8s.io/pause:3.8'
13+
14+
[plugins.'io.containerd.grpc.v1.cri'.containerd]
15+
[plugins.'io.containerd.grpc.v1.cri'.containerd.runtimes]
16+
[plugins.'io.containerd.grpc.v1.cri'.containerd.runtimes.runc]
1617
discard_unpacked_layers = true
17-
runtime_type = "io.containerd.runc.v2"
18-
[plugins."io.containerd.grpc.v1.cri".registry]
19-
config_path = "/etc/cri/conf.d/hosts"
20-
[plugins."io.containerd.grpc.v1.cri".registry.configs]
18+
runtime_type = 'io.containerd.runc.v2'
19+
20+
[plugins.'io.containerd.grpc.v1.cri'.registry]
21+
config_path = '/etc/cri/conf.d/hosts'
22+
23+
[plugins.'io.containerd.grpc.v1.cri'.registry.configs]

0 commit comments

Comments
 (0)