Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/manifests/collector: switch internally to v1alpha2 #2532

Merged
merged 19 commits into from
Feb 5, 2024
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
apis/v1alpha2: config yaml method
Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
frzifus committed Feb 1, 2024
commit df8df4b3386ff0347fa83dd9161c8ef91d3924ee
14 changes: 14 additions & 0 deletions apis/v1alpha2/config.go
Original file line number Diff line number Diff line change
@@ -15,10 +15,13 @@
package v1alpha2

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"sort"

"gopkg.in/yaml.v3"
)

// AnyConfig represent parts of the config.
@@ -84,6 +87,17 @@ type Config struct {
Service Service `json:"service" yaml:"service"`
}

// Yaml encodes the current object and returns it as a string.
func (c Config) Yaml() (string, error) {
var buf bytes.Buffer
yamlEncoder := yaml.NewEncoder(&buf)
yamlEncoder.SetIndent(2)
if err := yamlEncoder.Encode(&c); err != nil {
return "", err
}
return buf.String(), nil
}

type Service struct {
Extensions *[]string `json:"extensions,omitempty" yaml:"extensions,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
74 changes: 74 additions & 0 deletions apis/v1alpha2/config_test.go
Original file line number Diff line number Diff line change
@@ -110,3 +110,77 @@ func TestNullObjects_go_yaml(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 TestConfigYaml(t *testing.T) {
cfg := &Config{
Receivers: AnyConfig{
Object: map[string]interface{}{
"otlp": nil,
},
},
Processors: &AnyConfig{
Object: map[string]interface{}{
"modify_2000": "enabled",
},
},
Exporters: AnyConfig{
Object: map[string]interface{}{
"otlp/exporter": nil,
},
},
Connectors: &AnyConfig{
Object: map[string]interface{}{
"con": "magic",
},
},
Extensions: &AnyConfig{
Object: map[string]interface{}{
"addon": "option1",
},
},
Service: Service{
Extensions: &[]string{"addon"},
Telemetry: &AnyConfig{
Object: map[string]interface{}{
"insights": "yeah!",
},
},
Pipelines: AnyConfig{
Object: map[string]interface{}{
"receivers": []string{"otlp"},
"processors": []string{"modify_2000"},
"exporters": []string{"otlp/exporter", "con"},
},
},
},
}
yamlCollector, err := cfg.Yaml()
require.NoError(t, err)

const expected = `receivers:
otlp: null
exporters:
otlp/exporter: null
processors:
modify_2000: enabled
connectors:
con: magic
extensions:
addon: option1
service:
extensions:
- addon
telemetry:
insights: yeah!
pipelines:
exporters:
- otlp/exporter
- con
processors:
- modify_2000
receivers:
- otlp
`

assert.Equal(t, expected, yamlCollector)
}