Skip to content

Commit bd41d8c

Browse files
author
Israel Blancas
committed
Apply changes requested in code review
Signed-off-by: Israel Blancas <[email protected]>
1 parent b9bce9a commit bd41d8c

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

apis/v1beta1/config.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,9 @@ func (c AnyConfig) MarshalJSON() ([]byte, error) {
9393
return []byte("{}"), nil
9494
}
9595

96-
nonNilMap := make(map[string]interface{})
97-
96+
nonNilMap := make(map[string]interface{}, len(c.Object))
9897
for k, v := range c.Object {
99-
if v == nil {
100-
nonNilMap[k] = nil
101-
} else if emptyMap, ok := v.(map[string]interface{}); ok && len(emptyMap) == 0 {
102-
nonNilMap[k] = emptyMap
103-
} else {
104-
nonNilMap[k] = v
105-
}
98+
nonNilMap[k] = v
10699
}
107100

108101
return json.Marshal(nonNilMap)
@@ -251,8 +244,14 @@ func (c *Config) Yaml() (string, error) {
251244
return buf.String(), nil
252245
}
253246

247+
// MarshalJSON marshalls the Config field.
254248
func (c Config) MarshalJSON() ([]byte, error) {
249+
// When you marshal a struct that embeds itself or a type that directly or indirectly refers back to itself,
250+
// Go's JSON marshaling can lead to infinite recursion. To avoid this, we create an alias of the struct
251+
// (Config), so Go no longer considers it the same type.
252+
// This allows you to embed the struct safely within itself without triggering that infinite loop.
255253
type Alias Config
254+
// Ensure we call the custom marshaller for AnyConfig for the Receivers
256255
receiversJSON, err := json.Marshal(c.Receivers)
257256
if err != nil {
258257
return nil, err

apis/v1beta1/config_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ func TestCreateConfigInKubernetesEmptyValues(t *testing.T) {
8585
},
8686
},
8787
},
88+
Processors: &AnyConfig{
89+
Object: map[string]interface{}{
90+
"resourcedetection": map[string]interface{}{},
91+
},
92+
},
8893
Service: Service{
8994
Pipelines: map[string]*Pipeline{
9095
"traces": {
@@ -119,6 +124,7 @@ func TestCreateConfigInKubernetesEmptyValues(t *testing.T) {
119124
}
120125

121126
require.Contains(t, string(jsonData), "{\"grpc\":{},\"http\":{}}")
127+
require.Contains(t, string(jsonData), "\"resourcedetection\":{}")
122128

123129
unmarshalledCollector := &OpenTelemetryCollector{}
124130
err = json.Unmarshal(jsonData, &unmarshalledCollector.Spec)
@@ -138,6 +144,11 @@ func TestCreateConfigInKubernetesEmptyValues(t *testing.T) {
138144
http, ok := protocols["http"]
139145
require.True(t, ok, "http protocol should exist")
140146
require.NotNil(t, http, "http protocol should be nil")
147+
148+
require.NotNil(t, unmarshalledCollector.Spec.Config.Receivers.Object["otlp"])
149+
150+
_, ok = unmarshalledCollector.Spec.Config.Processors.Object["resourcedetection"].(map[string]interface{})
151+
require.True(t, ok, "resourcedetector processor should be a map")
141152
}
142153

143154
func TestCreateConfigInKubernetesNullValues(t *testing.T) {

apis/v1beta1/opentelemetrycollector_types.go

+12
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,14 @@ type OpenTelemetryCollector struct {
5454
// Hub exists to allow for conversion.
5555
func (*OpenTelemetryCollector) Hub() {}
5656

57+
// MarshalJSON marshalls the OpenTelemetry Collector to JSON.
5758
func (o *OpenTelemetryCollector) MarshalJSON() ([]byte, error) {
59+
// When you marshal a struct that embeds itself or a type that directly or indirectly refers back to itself,
60+
// Go's JSON marshaling can lead to infinite recursion. To avoid this, we create an alias of the struct
61+
// (Config), so Go no longer considers it the same type.
62+
// This allows you to embed the struct safely within itself without triggering that infinite loop.
5863
type Alias OpenTelemetryCollector
64+
// Ensure we call the custom marshaller for Spec
5965
specJSON, err := json.Marshal(&o.Spec)
6066
if err != nil {
6167
return nil, err
@@ -160,8 +166,14 @@ type OpenTelemetryCollectorSpec struct {
160166
DeploymentUpdateStrategy appsv1.DeploymentStrategy `json:"deploymentUpdateStrategy,omitempty"`
161167
}
162168

169+
// MarshalJSON marshalls the OpenTelemetryCollectorSpec field.
163170
func (s *OpenTelemetryCollectorSpec) MarshalJSON() ([]byte, error) {
171+
// When you marshal a struct that embeds itself or a type that directly or indirectly refers back to itself,
172+
// Go's JSON marshaling can lead to infinite recursion. To avoid this, we create an alias of the struct
173+
// (Config), so Go no longer considers it the same type.
174+
// This allows you to embed the struct safely within itself without triggering that infinite loop.
164175
type Alias OpenTelemetryCollectorSpec
176+
// Ensure we call the custom marshaller for Config
165177
configJSON, err := json.Marshal(s.Config)
166178
if err != nil {
167179
return nil, err

0 commit comments

Comments
 (0)