Skip to content

Commit 7dd500a

Browse files
committed
feat: update YAML library
Stop using `gopkg.in/yaml.v3` which is archived, and switch to a newer maintained fork. Drop `RawYAML` hacks and replace them with an implementation which doesn't need to have a forked version of the library. Signed-off-by: Andrey Smirnov <[email protected]>
1 parent 10412cb commit 7dd500a

File tree

16 files changed

+52
-29
lines changed

16 files changed

+52
-29
lines changed

go.mod

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ module github.com/cosi-project/runtime
22

33
go 1.24.0
44

5-
// forked yaml that introduces RawYAML interface that can be used to provide YAML encoder bytes
6-
// which are then encoded as a valid YAML block with proper indentiation
7-
replace gopkg.in/yaml.v3 => github.com/unix4ever/yaml v0.0.0-20220527175918-f17b0f05cf2c
8-
95
require (
106
github.com/ProtonMail/gopenpgp/v2 v2.9.0
117
github.com/cenkalti/backoff/v4 v4.3.0
@@ -22,11 +18,11 @@ require (
2218
go.etcd.io/bbolt v1.4.3
2319
go.uber.org/goleak v1.3.0
2420
go.uber.org/zap v1.27.0
21+
go.yaml.in/yaml/v4 v4.0.0-rc.2
2522
golang.org/x/sync v0.16.0
2623
golang.org/x/time v0.12.0
2724
google.golang.org/grpc v1.75.0
2825
google.golang.org/protobuf v1.36.8
29-
gopkg.in/yaml.v3 v3.0.1
3026
)
3127

3228
require (
@@ -45,6 +41,7 @@ require (
4541
golang.org/x/text v0.28.0 // indirect
4642
google.golang.org/genproto/googleapis/api v0.0.0-20250818200422-3122310a409c // indirect
4743
google.golang.org/genproto/googleapis/rpc v0.0.0-20250818200422-3122310a409c // indirect
44+
gopkg.in/yaml.v3 v3.0.1 // indirect
4845
)
4946

5047
retract (

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ github.com/siderolabs/protoenc v0.2.3 h1:Mxyav+L6hK01MrUCaVYuwfECThHZwmu2a6Fiu+S
5555
github.com/siderolabs/protoenc v0.2.3/go.mod h1:MkwsDoyCceIpVXScDe/nIGJovh6hwln2lqlsFGjoacw=
5656
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
5757
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
58-
github.com/unix4ever/yaml v0.0.0-20220527175918-f17b0f05cf2c h1:Vn6nVVu9MdOYvXPkJP83iX5jVIfvxFC9v9xIKb+DlaQ=
59-
github.com/unix4ever/yaml v0.0.0-20220527175918-f17b0f05cf2c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6058
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
6159
go.etcd.io/bbolt v1.4.3 h1:dEadXpI6G79deX5prL3QRNP6JB8UxVkqo4UPnHaNXJo=
6260
go.etcd.io/bbolt v1.4.3/go.mod h1:tKQlpPaYCVFctUIgFKFnAlvbmB3tpy1vkTnDWohtc0E=
@@ -78,6 +76,8 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
7876
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
7977
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
8078
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
79+
go.yaml.in/yaml/v4 v4.0.0-rc.2 h1:/FrI8D64VSr4HtGIlUtlFMGsm7H7pWTbj6vOLVZcA6s=
80+
go.yaml.in/yaml/v4 v4.0.0-rc.2/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=
8181
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
8282
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
8383
golang.org/x/crypto v0.39.0 h1:SHs+kF4LP+f+p14esP5jAoDpHU8Gu/v9lFRK6IT5imM=
@@ -133,3 +133,5 @@ google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXn
133133
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
134134
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
135135
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
136+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
137+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/resource/any.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package resource
66

77
import (
8-
"gopkg.in/yaml.v3"
8+
"errors"
9+
10+
"go.yaml.in/yaml/v4"
911
)
1012

1113
// Any can hold data from any resource type.
@@ -19,9 +21,20 @@ type anySpec struct {
1921
yaml []byte
2022
}
2123

22-
// MarshalYAMLBytes implements RawYAML interface.
23-
func (s anySpec) MarshalYAMLBytes() ([]byte, error) {
24-
return s.yaml, nil
24+
// MarshalYAML implements yaml.Marshaler interface.
25+
func (s anySpec) MarshalYAML() (any, error) {
26+
var node yaml.Node
27+
28+
if err := yaml.Unmarshal(s.yaml, &node); err != nil {
29+
return nil, err
30+
}
31+
32+
// the `node` is expected to be a document node
33+
if node.Kind != yaml.DocumentNode || len(node.Content) == 0 {
34+
return nil, errors.New("invalid YAML content")
35+
}
36+
37+
return &node.Content[0], nil
2538
}
2639

2740
// SpecProto is a protobuf interface of resource spec.

pkg/resource/any_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"testing"
1010

1111
"github.com/stretchr/testify/assert"
12-
"gopkg.in/yaml.v3"
12+
"go.yaml.in/yaml/v4"
1313

1414
"github.com/cosi-project/runtime/pkg/resource"
1515
)

pkg/resource/handle/handle.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"errors"
1212
"reflect"
1313

14-
"gopkg.in/yaml.v3"
14+
"go.yaml.in/yaml/v4"
1515
)
1616

1717
// Spec should be yaml.Marshaler.

pkg/resource/handle/handle_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
"github.com/siderolabs/gen/xtesting/must"
1515
"github.com/stretchr/testify/require"
16-
"gopkg.in/yaml.v3"
16+
"go.yaml.in/yaml/v4"
1717

1818
"github.com/cosi-project/runtime/pkg/resource"
1919
"github.com/cosi-project/runtime/pkg/resource/handle"

pkg/resource/internal/kv/kv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"maps"
1010
"sort"
1111

12-
"gopkg.in/yaml.v3"
12+
"go.yaml.in/yaml/v4"
1313

1414
"github.com/cosi-project/runtime/pkg/resource/kvutils"
1515
)

pkg/resource/metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"time"
1111

1212
"github.com/siderolabs/gen/xslices"
13+
"go.yaml.in/yaml/v4"
1314
"google.golang.org/protobuf/types/known/timestamppb"
14-
"gopkg.in/yaml.v3"
1515
)
1616

1717
var _ Reference = Metadata{}

pkg/resource/metadata_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212

1313
"github.com/siderolabs/gen/ensure"
1414
"github.com/stretchr/testify/assert"
15+
"go.yaml.in/yaml/v4"
1516
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
16-
"gopkg.in/yaml.v3"
1717

1818
"github.com/cosi-project/runtime/pkg/resource"
1919
)

pkg/resource/protobuf/resource.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
package protobuf
66

77
import (
8+
"errors"
89
"fmt"
910
"slices"
1011

12+
"go.yaml.in/yaml/v4"
1113
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
12-
"gopkg.in/yaml.v3"
1314

1415
"github.com/cosi-project/runtime/api/v1alpha1"
1516
"github.com/cosi-project/runtime/pkg/resource"
@@ -28,13 +29,24 @@ type protoSpec struct {
2829
protobuf []byte
2930
}
3031

31-
// MarshalYAMLBytes implements RawYAML interface.
32-
func (s protoSpec) MarshalYAMLBytes() ([]byte, error) {
32+
// MarshalYAML implements yaml.Marshaler interface.
33+
func (s protoSpec) MarshalYAML() (any, error) {
3334
if s.yaml == "" {
3435
return nil, fmt.Errorf("YAML spec is not specified")
3536
}
3637

37-
return []byte(s.yaml), nil
38+
var node yaml.Node
39+
40+
if err := yaml.Unmarshal([]byte(s.yaml), &node); err != nil {
41+
return nil, err
42+
}
43+
44+
// the `node` is expected to be a document node
45+
if node.Kind != yaml.DocumentNode || len(node.Content) == 0 {
46+
return nil, errors.New("invalid YAML content")
47+
}
48+
49+
return &node.Content[0], nil
3850
}
3951

4052
func (r *Resource) String() string {

0 commit comments

Comments
 (0)