Skip to content

Commit a419080

Browse files
authored
[chore]: migrate from sigs.k8s.io/yaml to goccy/go-yaml (#3997)
* feat: migrate from sigs.k8s.io/yaml to goccy/go-yaml * Alias on import and fix e2e tests * fix yaml indentation in tests
1 parent 2bc65d6 commit a419080

File tree

20 files changed

+63
-60
lines changed

20 files changed

+63
-60
lines changed

apis/v1alpha1/convert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"errors"
88
"fmt"
99

10-
"gopkg.in/yaml.v3"
10+
go_yaml "github.com/goccy/go-yaml"
1111
appsv1 "k8s.io/api/apps/v1"
1212
v1 "k8s.io/api/core/v1"
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -55,7 +55,7 @@ func (dst *OpenTelemetryCollector) ConvertFrom(srcRaw conversion.Hub) error {
5555
func tov1beta1(in OpenTelemetryCollector) (v1beta1.OpenTelemetryCollector, error) {
5656
copy := in.DeepCopy()
5757
cfg := &v1beta1.Config{}
58-
if err := yaml.Unmarshal([]byte(copy.Spec.Config), cfg); err != nil {
58+
if err := go_yaml.Unmarshal([]byte(copy.Spec.Config), cfg); err != nil {
5959
return v1beta1.OpenTelemetryCollector{}, errors.New("could not convert config json to v1beta1.Config")
6060
}
6161

apis/v1alpha1/convert_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"testing"
88
"time"
99

10+
go_yaml "github.com/goccy/go-yaml"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
12-
"gopkg.in/yaml.v3"
1313
appsv1 "k8s.io/api/apps/v1"
1414
autoscalingv2 "k8s.io/api/autoscaling/v2"
1515
v1 "k8s.io/api/core/v1"
@@ -56,12 +56,12 @@ func Test_tov1beta1_config(t *testing.T) {
5656
assert.NotNil(t, cfgV2)
5757
assert.Equal(t, cfgV1.Spec.Args, cfgV2.Spec.Args)
5858

59-
yamlCfg, err := yaml.Marshal(&cfgV2.Spec.Config)
59+
yamlCfg, err := go_yaml.Marshal(&cfgV2.Spec.Config)
6060
assert.Nil(t, err)
6161
assert.YAMLEq(t, collectorCfg, string(yamlCfg))
6262
})
6363
t.Run("invalid config", func(t *testing.T) {
64-
config := `!!!`
64+
config := `@@@`
6565
cfgV1 := OpenTelemetryCollector{
6666
Spec: OpenTelemetryCollectorSpec{
6767
Config: config,
@@ -75,7 +75,7 @@ func Test_tov1beta1_config(t *testing.T) {
7575

7676
func Test_tov1alpha1_config(t *testing.T) {
7777
cfg := v1beta1.Config{}
78-
err := yaml.Unmarshal([]byte(collectorCfg), &cfg)
78+
err := go_yaml.Unmarshal([]byte(collectorCfg), &cfg)
7979
require.NoError(t, err)
8080

8181
beta1Col := v1beta1.OpenTelemetryCollector{

apis/v1beta1/collector_webhook_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
"testing"
1212

1313
"github.com/go-logr/logr"
14+
go_yaml "github.com/goccy/go-yaml"
1415
"github.com/google/go-cmp/cmp"
1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
17-
"gopkg.in/yaml.v3"
1818
appsv1 "k8s.io/api/apps/v1"
1919
authv1 "k8s.io/api/authorization/v1"
2020
autoscalingv2 "k8s.io/api/autoscaling/v2"
@@ -142,7 +142,7 @@ func TestCollectorDefaultingWebhook(t *testing.T) {
142142
Config: func() v1beta1.Config {
143143
const input = `{"receivers":{"otlp":{"protocols":{"grpc":null,"http":null}}},"exporters":{"debug":null},"service":{"pipelines":{"traces":{"receivers":["otlp"],"exporters":["debug"]}}}}`
144144
var cfg v1beta1.Config
145-
require.NoError(t, yaml.Unmarshal([]byte(input), &cfg))
145+
require.NoError(t, go_yaml.Unmarshal([]byte(input), &cfg))
146146
return cfg
147147
}(),
148148
},
@@ -161,7 +161,7 @@ func TestCollectorDefaultingWebhook(t *testing.T) {
161161
Config: func() v1beta1.Config {
162162
const input = `{"receivers":{"otlp":{"protocols":{"grpc":{"endpoint":"0.0.0.0:4317"},"http":{"endpoint":"0.0.0.0:4318"}}}},"exporters":{"debug":null},"service":{"telemetry":{"metrics":{"readers":[{"pull":{"exporter":{"prometheus":{"host":"0.0.0.0","port":8888}}}}]}},"pipelines":{"traces":{"receivers":["otlp"],"exporters":["debug"]}}}}`
163163
var cfg v1beta1.Config
164-
require.NoError(t, yaml.Unmarshal([]byte(input), &cfg))
164+
require.NoError(t, go_yaml.Unmarshal([]byte(input), &cfg))
165165
// This is a workaround to avoid the type mismatch because how go-yaml unmarshals
166166
cfg.Service.Telemetry.Object["metrics"].(map[string]interface{})["readers"].([]interface{})[0].(map[string]interface{})["pull"].(map[string]interface{})["exporter"].(map[string]interface{})["prometheus"].(map[string]interface{})["port"] = int32(8888)
167167
return cfg
@@ -176,7 +176,7 @@ func TestCollectorDefaultingWebhook(t *testing.T) {
176176
Config: func() v1beta1.Config {
177177
const input = `{"receivers":{"otlp":{"protocols":{"grpc":{"headers":{"example":"another"}},"http":{"endpoint":"0.0.0.0:4000"}}}},"exporters":{"debug":null},"service":{"telemetry":{"metrics":{"readers":[{"pull":{"exporter":{"prometheus":{"host":"localhost","port":9999}}}}]}},"pipelines":{"traces":{"receivers":["otlp"],"exporters":["debug"]}}}}`
178178
var cfg v1beta1.Config
179-
require.NoError(t, yaml.Unmarshal([]byte(input), &cfg))
179+
require.NoError(t, go_yaml.Unmarshal([]byte(input), &cfg))
180180
return cfg
181181
}(),
182182
},
@@ -195,7 +195,7 @@ func TestCollectorDefaultingWebhook(t *testing.T) {
195195
Config: func() v1beta1.Config {
196196
const input = `{"receivers":{"otlp":{"protocols":{"grpc":{"endpoint":"0.0.0.0:4317","headers":{"example":"another"}},"http":{"endpoint":"0.0.0.0:4000"}}}},"exporters":{"debug":null},"service":{"telemetry":{"metrics":{"readers":[{"pull":{"exporter":{"prometheus":{"host":"localhost","port":9999}}}}]}},"pipelines":{"traces":{"receivers":["otlp"],"exporters":["debug"]}}}}`
197197
var cfg v1beta1.Config
198-
require.NoError(t, yaml.Unmarshal([]byte(input), &cfg))
198+
require.NoError(t, go_yaml.Unmarshal([]byte(input), &cfg))
199199
return cfg
200200
}(),
201201
},
@@ -604,7 +604,7 @@ func TestOTELColValidatingWebhook(t *testing.T) {
604604
},
605605
},
606606
}
607-
err := yaml.Unmarshal([]byte(cfgYaml), &cfg)
607+
err := go_yaml.Unmarshal([]byte(cfgYaml), &cfg)
608608
require.NoError(t, err)
609609

610610
tests := []struct { //nolint:govet

apis/v1beta1/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010

1111
"dario.cat/mergo"
1212
"github.com/go-logr/logr"
13+
go_yaml "github.com/goccy/go-yaml"
1314
otelConfig "go.opentelemetry.io/contrib/otelconf/v0.3.0"
14-
"gopkg.in/yaml.v3"
1515
corev1 "k8s.io/api/core/v1"
1616
rbacv1 "k8s.io/api/rbac/v1"
1717

@@ -382,8 +382,7 @@ func (c *Config) GetReadinessProbe(logger logr.Logger) (*corev1.Probe, error) {
382382
// Yaml encodes the current object and returns it as a string.
383383
func (c *Config) Yaml() (string, error) {
384384
var buf bytes.Buffer
385-
yamlEncoder := yaml.NewEncoder(&buf)
386-
yamlEncoder.SetIndent(2)
385+
yamlEncoder := go_yaml.NewEncoder(&buf, go_yaml.IndentSequence(true), go_yaml.AutoInt())
387386
if err := yamlEncoder.Encode(&c); err != nil {
388387
return "", err
389388
}

apis/v1beta1/config_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ import (
1111
"testing"
1212

1313
"github.com/go-logr/logr"
14+
go_yaml "github.com/goccy/go-yaml"
1415
"github.com/google/go-cmp/cmp"
1516
"github.com/stretchr/testify/assert"
1617
"github.com/stretchr/testify/require"
17-
go_yaml "gopkg.in/yaml.v3"
1818
v1 "k8s.io/api/core/v1"
1919
"k8s.io/apimachinery/pkg/util/intstr"
2020
"k8s.io/utils/ptr"
21-
"sigs.k8s.io/yaml"
2221
)
2322

2423
func TestConfigFiles(t *testing.T) {
@@ -35,7 +34,7 @@ func TestConfigFiles(t *testing.T) {
3534
collectorYaml, err := os.ReadFile(testFile)
3635
require.NoError(t, err)
3736

38-
collectorJson, err := yaml.YAMLToJSON(collectorYaml)
37+
collectorJson, err := go_yaml.YAMLToJSON(collectorYaml)
3938
require.NoError(t, err)
4039

4140
cfg := &Config{}
@@ -45,7 +44,7 @@ func TestConfigFiles(t *testing.T) {
4544
require.NoError(t, err)
4645

4746
assert.JSONEq(t, string(collectorJson), string(jsonCfg))
48-
yamlCfg, err := yaml.JSONToYAML(jsonCfg)
47+
yamlCfg, err := go_yaml.JSONToYAML(jsonCfg)
4948
require.NoError(t, err)
5049
assert.YAMLEq(t, string(collectorYaml), string(yamlCfg))
5150
})
@@ -56,7 +55,7 @@ func TestNullObjects(t *testing.T) {
5655
collectorYaml, err := os.ReadFile("./testdata/otelcol-null-values.yaml")
5756
require.NoError(t, err)
5857

59-
collectorJson, err := yaml.YAMLToJSON(collectorYaml)
58+
collectorJson, err := go_yaml.YAMLToJSON(collectorYaml)
6059
require.NoError(t, err)
6160

6261
cfg := &Config{}
@@ -71,7 +70,7 @@ func TestNullObjects_issue_3445(t *testing.T) {
7170
collectorYaml, err := os.ReadFile("./testdata/issue-3452.yaml")
7271
require.NoError(t, err)
7372

74-
collectorJson, err := yaml.YAMLToJSON(collectorYaml)
73+
collectorJson, err := go_yaml.YAMLToJSON(collectorYaml)
7574
require.NoError(t, err)
7675

7776
cfg := &Config{}

cmd/otel-allocator/internal/target/discovery.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/go-logr/logr"
13+
go_yaml "github.com/goccy/go-yaml"
1314
"github.com/prometheus/client_golang/prometheus"
1415
"github.com/prometheus/client_golang/prometheus/promauto"
1516
"github.com/prometheus/common/model"
@@ -19,7 +20,6 @@ import (
1920
"github.com/prometheus/prometheus/model/labels"
2021
"github.com/prometheus/prometheus/model/relabel"
2122
"go.uber.org/zap/zapcore"
22-
"gopkg.in/yaml.v3"
2323

2424
allocatorWatcher "github.com/open-telemetry/opentelemetry-operator/cmd/otel-allocator/internal/watcher"
2525
)
@@ -250,7 +250,7 @@ func (m *Discoverer) Close() {
250250
// This is done by marshaling to YAML because it's the most straightforward and doesn't run into problems with unexported fields.
251251
func getScrapeConfigHash(jobToScrapeConfig map[string]*promconfig.ScrapeConfig) (hash.Hash64, error) {
252252
hash := fnv.New64()
253-
yamlEncoder := yaml.NewEncoder(hash)
253+
yamlEncoder := go_yaml.NewEncoder(hash)
254254
for jobName, scrapeConfig := range jobToScrapeConfig {
255255
_, err := hash.Write([]byte(jobName))
256256
if err != nil {

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ require (
4444
go.uber.org/multierr v1.11.0
4545
go.uber.org/zap v1.27.0
4646
gopkg.in/yaml.v2 v2.4.0
47-
gopkg.in/yaml.v3 v3.0.1
47+
gopkg.in/yaml.v3 v3.0.1 // indirect
4848
k8s.io/api v0.32.3
4949
k8s.io/apiextensions-apiserver v0.32.3
5050
k8s.io/apimachinery v0.32.3
@@ -226,7 +226,10 @@ require (
226226
sigs.k8s.io/structured-merge-diff/v4 v4.5.0 // indirect
227227
)
228228

229-
require go.opentelemetry.io/contrib/otelconf v0.15.0
229+
require (
230+
github.com/goccy/go-yaml v1.17.1
231+
go.opentelemetry.io/contrib/otelconf v0.15.0
232+
)
230233

231234
require (
232235
github.com/envoyproxy/go-control-plane/envoy v1.32.4 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ github.com/go-zookeeper/zk v1.0.4 h1:DPzxraQx7OrPyXq2phlGlNSIyWEsAox0RJmjTseMV6I
196196
github.com/go-zookeeper/zk v1.0.4/go.mod h1:nOB03cncLtlp4t+UAkGSV+9beXP/akpekBwL+UX1Qcw=
197197
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
198198
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
199+
github.com/goccy/go-yaml v1.17.1 h1:LI34wktB2xEE3ONG/2Ar54+/HJVBriAGJ55PHls4YuY=
200+
github.com/goccy/go-yaml v1.17.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
199201
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
200202
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
201203
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=

internal/controllers/builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
cmv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
1010
cmmetav1 "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
1111
"github.com/go-logr/logr"
12+
go_yaml "github.com/goccy/go-yaml"
1213
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
1314
"github.com/stretchr/testify/require"
1415
colfeaturegate "go.opentelemetry.io/collector/featuregate"
15-
go_yaml "gopkg.in/yaml.v3"
1616
appsv1 "k8s.io/api/apps/v1"
1717
corev1 "k8s.io/api/core/v1"
1818
networkingv1 "k8s.io/api/networking/v1"

internal/manifests/collector/config_replace.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ package collector
66
import (
77
"time"
88

9+
go_yaml "github.com/goccy/go-yaml"
910
promconfig "github.com/prometheus/prometheus/config"
1011
_ "github.com/prometheus/prometheus/discovery/install" // Package install has the side-effect of registering all builtin.
11-
"gopkg.in/yaml.v3"
1212

1313
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
1414
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
@@ -68,7 +68,7 @@ func ReplaceConfig(otelcol v1beta1.OpenTelemetryCollector, targetAllocator *v1al
6868
// type coercion checks are handled in the AddTAConfigToPromConfig method above
6969
config["receivers"].(map[interface{}]interface{})["prometheus"] = updPromCfgMap
7070

71-
out, updCfgMarshalErr := yaml.Marshal(config)
71+
out, updCfgMarshalErr := go_yaml.MarshalWithOptions(config, go_yaml.Indent(4), go_yaml.IndentSequence(true), go_yaml.AutoInt())
7272
if updCfgMarshalErr != nil {
7373
return "", updCfgMarshalErr
7474
}

0 commit comments

Comments
 (0)