Skip to content

Commit

Permalink
apis/v1alpha2: fix yaml marshalling (#2553)
Browse files Browse the repository at this point in the history
* apis/v1alpha2: add yaml marshal and unmarshal methods to config

Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>

* apis/v1alpha2: bump go-yaml version from v2 to v3

Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>

* apis/v1alpha2: remove unnecessary marshal methods from config

Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>

* remove changelog

Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>

---------

Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
  • Loading branch information
frzifus committed Jan 23, 2024
1 parent c76462e commit 2f0006d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
26 changes: 15 additions & 11 deletions apis/v1alpha2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

// AnyConfig represent parts of the config.
type AnyConfig struct {
Object map[string]interface{} `json:"-"`
Object map[string]interface{} `json:"-" yaml:",inline"`
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
Expand Down Expand Up @@ -72,24 +72,24 @@ func (c *AnyConfig) MarshalJSON() ([]byte, error) {
// Config encapsulates collector config.
type Config struct {
// +kubebuilder:pruning:PreserveUnknownFields
Receivers AnyConfig `json:"receivers"`
Receivers AnyConfig `json:"receivers" yaml:"receivers"`
// +kubebuilder:pruning:PreserveUnknownFields
Exporters AnyConfig `json:"exporters"`
Exporters AnyConfig `json:"exporters" yaml:"exporters"`
// +kubebuilder:pruning:PreserveUnknownFields
Processors *AnyConfig `json:"processors,omitempty"`
Processors *AnyConfig `json:"processors,omitempty" yaml:"processors,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Connectors *AnyConfig `json:"connectors,omitempty"`
Connectors *AnyConfig `json:"connectors,omitempty" yaml:"connectors,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Extensions *AnyConfig `json:"extensions,omitempty"`
Service Service `json:"service"`
Extensions *AnyConfig `json:"extensions,omitempty" yaml:"extensions,omitempty"`
Service Service `json:"service" yaml:"service"`
}

type Service struct {
Extensions *[]string `json:"extensions,omitempty"`
Extensions *[]string `json:"extensions,omitempty" yaml:"extensions,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Telemetry *AnyConfig `json:"telemetry,omitempty"`
Telemetry *AnyConfig `json:"telemetry,omitempty" yaml:"telemetry,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Pipelines AnyConfig `json:"pipelines"`
Pipelines AnyConfig `json:"pipelines" yaml:"pipelines"`
}

// Returns null objects in the config.
Expand Down Expand Up @@ -128,7 +128,11 @@ func hasNullValue(cfg map[string]interface{}) []string {
nullKeys = append(nullKeys, fmt.Sprintf("%s:", k))
}
if reflect.ValueOf(v).Kind() == reflect.Map {
nulls := hasNullValue(v.(map[string]interface{}))
var nulls []string
val, ok := v.(map[string]interface{})
if ok {
nulls = hasNullValue(val)
}
if len(nulls) > 0 {
prefixed := addPrefix(k+".", nulls)
nullKeys = append(nullKeys, prefixed...)
Expand Down
39 changes: 39 additions & 0 deletions apis/v1alpha2/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
go_yaml "gopkg.in/yaml.v3"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -71,3 +72,41 @@ func TestNullObjects(t *testing.T) {
nullObjects := cfg.nullObjects()
assert.Equal(t, []string{"connectors.spanmetrics:", "exporters.otlp.endpoint:", "extensions.health_check:", "processors.batch:", "receivers.otlp.protocols.grpc:", "receivers.otlp.protocols.http:"}, nullObjects)
}

func TestConfigFiles_go_yaml(t *testing.T) {
files, err := os.ReadDir("./testdata")
require.NoError(t, err)

for _, file := range files {
if !strings.HasPrefix(file.Name(), "otelcol-") {
continue
}

testFile := path.Join("./testdata", file.Name())
t.Run(testFile, func(t *testing.T) {
collectorYaml, err := os.ReadFile(testFile)
require.NoError(t, err)

cfg := &Config{}
err = go_yaml.Unmarshal(collectorYaml, cfg)
require.NoError(t, err)
yamlCfg, err := go_yaml.Marshal(cfg)
require.NoError(t, err)

require.NoError(t, err)
assert.YAMLEq(t, string(collectorYaml), string(yamlCfg))
})
}
}

func TestNullObjects_go_yaml(t *testing.T) {
collectorYaml, err := os.ReadFile("./testdata/otelcol-null-values.yaml")
require.NoError(t, err)

cfg := &Config{}
err = go_yaml.Unmarshal(collectorYaml, cfg)
require.NoError(t, err)

nullObjects := cfg.nullObjects()
assert.Equal(t, []string{"connectors.spanmetrics:", "exporters.otlp.endpoint:", "extensions.health_check:", "processors.batch:", "receivers.otlp.protocols.grpc:", "receivers.otlp.protocols.http:"}, nullObjects)
}

0 comments on commit 2f0006d

Please sign in to comment.