Skip to content

Commit

Permalink
avoid using mapstructure
Browse files Browse the repository at this point in the history
Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
  • Loading branch information
frzifus committed Oct 18, 2024
1 parent 509a1a4 commit 910ca50
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
19 changes: 10 additions & 9 deletions apis/v1beta1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

"dario.cat/mergo"
"github.com/go-logr/logr"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
Expand Down Expand Up @@ -401,10 +400,12 @@ func (s *Service) ApplyDefaults() error {
if err != nil {
return err
}
tele := Telemetry{Metrics: MetricsConfig{Address: fmt.Sprintf("%s:%d", telemetryAddr, telemetryPort)}}
tm := &AnyConfig{}
if err := mapstructure.Decode(tele, &tm.Object); err != nil {
return fmt.Errorf("telemetry config decoding failed: %w", err)
tm := &AnyConfig{
Object: map[string]interface{}{
"metrics": map[string]interface{}{
"address": fmt.Sprintf("%s:%d", telemetryAddr, telemetryPort),
},
},
}

if s.Telemetry == nil {
Expand All @@ -424,21 +425,21 @@ type MetricsConfig struct {
// - "basic" is the recommended and covers the basics of the service telemetry.
// - "normal" adds some other indicators on top of basic.
// - "detailed" adds dimensions and views to the previous levels.
Level string `json:"level,omitempty" yaml:"level,omitempty" mapstructure:"level,omitempty"`
Level string `json:"level,omitempty" yaml:"level,omitempty"`

// Address is the [address]:port that metrics exposition should be bound to.
Address string `json:"address,omitempty" yaml:"address,omitempty" mapstructure:"address,omitempty"`
Address string `json:"address,omitempty" yaml:"address,omitempty"`
}

// Telemetry is an intermediary type that allows for easy access to the collector's telemetry settings.
type Telemetry struct {
Metrics MetricsConfig `json:"metrics,omitempty" yaml:"metrics,omitempty" mapstructure:"metrics,omitempty"`
Metrics MetricsConfig `json:"metrics,omitempty" yaml:"metrics,omitempty"`

// Resource specifies user-defined attributes to include with all emitted telemetry.
// Note that some attributes are added automatically (e.g. service.version) even
// if they are not specified here. In order to suppress such attributes the
// attribute must be specified in this map with null YAML value (nil string pointer).
Resource map[string]*string `json:"resource,omitempty" yaml:"resource,omitempty" mapstructure:"resource,omitempty"`
Resource map[string]*string `json:"resource,omitempty" yaml:"resource,omitempty"`
}

// GetTelemetry serves as a helper function to access the fields we care about in the underlying telemetry struct.
Expand Down
23 changes: 13 additions & 10 deletions pkg/collector/upgrade/v0_111_0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"context"
"testing"

"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
Expand Down Expand Up @@ -48,11 +47,14 @@ func Test0_111_0Upgrade(t *testing.T) {
}

defaultCollectorWithConfig := defaultCollector.DeepCopy()
tm := &v1beta1.AnyConfig{}
if err := mapstructure.Decode(v1beta1.Telemetry{Metrics: v1beta1.MetricsConfig{Address: "1.2.3.4:8888"}}, &tm.Object); err != nil {
t.Fatal(err)

defaultCollectorWithConfig.Spec.Config.Service.Telemetry = &v1beta1.AnyConfig{
Object: map[string]interface{}{
"metrics": map[string]interface{}{
"address": "1.2.3.4:8888",
},
},
}
defaultCollectorWithConfig.Spec.Config.Service.Telemetry = tm

tt := []struct {
name string
Expand All @@ -69,12 +71,13 @@ func Test0_111_0Upgrade(t *testing.T) {
input: *defaultCollector.DeepCopy(),
expected: func() v1beta1.OpenTelemetryCollector {
col := defaultCollector.DeepCopy()
tele := v1beta1.Telemetry{Metrics: v1beta1.MetricsConfig{Address: "0.0.0.0:8888"}}
tm := &v1beta1.AnyConfig{}
if err := mapstructure.Decode(tele, &tm.Object); err != nil {
t.Fatal(err)
col.Spec.Config.Service.Telemetry = &v1beta1.AnyConfig{
Object: map[string]interface{}{
"metrics": map[string]interface{}{
"address": "0.0.0.0:8888",
},
},
}
col.Spec.Config.Service.Telemetry = tm
return *col
}(),
},
Expand Down

0 comments on commit 910ca50

Please sign in to comment.