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

Fix marshalling for Config in v1beta1 #3282

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 16 additions & 0 deletions .chloggen/3281-marshal-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Fix Configuration marshalling for v1beta1 OpenTelemetryCollector instances.

# One or more tracking issues related to the change
issues: [3281]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
34 changes: 31 additions & 3 deletions apis/v1beta1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,24 @@ func (c *AnyConfig) UnmarshalJSON(b []byte) error {
}

// MarshalJSON specifies how to convert this object into JSON.
func (c *AnyConfig) MarshalJSON() ([]byte, error) {
if c == nil {
func (c AnyConfig) MarshalJSON() ([]byte, error) {
if c.Object == nil {
return []byte("{}"), nil
}
return json.Marshal(c.Object)

nonNilMap := make(map[string]interface{})

for k, v := range c.Object {
if v == nil {
nonNilMap[k] = nil
} else if emptyMap, ok := v.(map[string]interface{}); ok && len(emptyMap) == 0 {
nonNilMap[k] = emptyMap
} else {
nonNilMap[k] = v
}
}

return json.Marshal(nonNilMap)
}

// Pipeline is a struct of component type to a list of component IDs.
Expand Down Expand Up @@ -198,6 +211,21 @@ func (c *Config) Yaml() (string, error) {
return buf.String(), nil
}

func (c Config) MarshalJSON() ([]byte, error) {
type Alias Config
receiversJSON, err := json.Marshal(c.Receivers)
if err != nil {
return nil, err
}
return json.Marshal(&struct {
*Alias
Receivers json.RawMessage `json:"receivers,omitempty"`
}{
Alias: (*Alias)(&c),
Receivers: receiversJSON,
})
}

// Returns null objects in the config.
func (c *Config) nullObjects() []string {
var nullKeys []string
Expand Down
67 changes: 67 additions & 0 deletions apis/v1beta1/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,70 @@ func TestConfig_GetExporterPorts(t *testing.T) {
})
}
}

func TestAnyConfig_MarshalJSON(t *testing.T) {
tests := []struct {
name string
config AnyConfig
want string
wantErr bool
}{
{
name: "nil Object",
config: AnyConfig{},
want: "{}",
},
{
name: "empty Object",
config: AnyConfig{
Object: map[string]interface{}{},
},
want: "{}",
},
{
name: "Object with nil value",
config: AnyConfig{
Object: map[string]interface{}{
"key": nil,
},
},
want: `{"key":null}`,
},
{
name: "Object with empty map value",
config: AnyConfig{
Object: map[string]interface{}{
"key": map[string]interface{}{},
},
},
want: `{"key":{}}`,
},
{
name: "Object with non-empty values",
config: AnyConfig{
Object: map[string]interface{}{
"string": "value",
"number": 42,
"bool": true,
"map": map[string]interface{}{
"nested": "data",
},
},
},
want: `{"bool":true,"map":{"nested":"data"},"number":42,"string":"value"}`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.config.MarshalJSON()
if (err != nil) != tt.wantErr {
t.Errorf("AnyConfig.MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
return
}
if string(got) != tt.want {
t.Errorf("AnyConfig.MarshalJSON() = %v, want %v", string(got), tt.want)
}
})
}
}
32 changes: 32 additions & 0 deletions apis/v1beta1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package v1beta1

import (
"encoding/json"

appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -52,6 +54,21 @@ type OpenTelemetryCollector struct {
// Hub exists to allow for conversion.
func (*OpenTelemetryCollector) Hub() {}

func (o *OpenTelemetryCollector) MarshalJSON() ([]byte, error) {
type Alias OpenTelemetryCollector
specJSON, err := json.Marshal(&o.Spec)
if err != nil {
return nil, err
}
return json.Marshal(&struct {
*Alias
Spec json.RawMessage `json:"spec,omitempty"`
}{
Alias: (*Alias)(o),
Spec: specJSON,
})
}

//+kubebuilder:object:root=true

// OpenTelemetryCollectorList contains a list of OpenTelemetryCollector.
Expand Down Expand Up @@ -143,6 +160,21 @@ type OpenTelemetryCollectorSpec struct {
DeploymentUpdateStrategy appsv1.DeploymentStrategy `json:"deploymentUpdateStrategy,omitempty"`
}

func (s *OpenTelemetryCollectorSpec) MarshalJSON() ([]byte, error) {
type Alias OpenTelemetryCollectorSpec
configJSON, err := json.Marshal(s.Config)
if err != nil {
return nil, err
}
return json.Marshal(&struct {
*Alias
Config json.RawMessage `json:"config,omitempty"`
}{
Alias: (*Alias)(s),
Config: configJSON,
})
}

// TargetAllocatorEmbedded defines the configuration for the Prometheus target allocator, embedded in the
// OpenTelemetryCollector spec.
type TargetAllocatorEmbedded struct {
Expand Down
147 changes: 147 additions & 0 deletions apis/v1beta1/opentelemetrycollector_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1beta1

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestOpenTelemetryCollector_MarshalJSON(t *testing.T) {
collector := &OpenTelemetryCollector{
TypeMeta: metav1.TypeMeta{
APIVersion: "opentelemetry.io/v1beta1",
Kind: "OpenTelemetryCollector",
},
ObjectMeta: metav1.ObjectMeta{
Name: "test-collector",
Namespace: "default",
},
Spec: OpenTelemetryCollectorSpec{
Config: Config{
Receivers: AnyConfig{
Object: map[string]interface{}{
"otlp": map[string]interface{}{
"protocols": map[string]interface{}{
"grpc": nil,
},
},
},
},
Processors: &AnyConfig{
Object: map[string]interface{}{
"batch": nil,
},
},
Exporters: AnyConfig{
Object: map[string]interface{}{
"otlp": map[string]interface{}{
"endpoint": "otelcol:4317",
},
},
},
Service: Service{
Pipelines: map[string]*Pipeline{
"traces": {
Receivers: []string{"otlp"},
Processors: []string{"batch"},
Exporters: []string{"otlp"},
},
},
},
},
},
}
marshaledJSON, err := json.Marshal(collector)
assert.NoError(t, err)

var result map[string]interface{}
err = json.Unmarshal(marshaledJSON, &result)
assert.NoError(t, err)

spec, ok := result["spec"].(map[string]interface{})
assert.True(t, ok, "spec should be a JSON object")

config, ok := spec["config"].(map[string]interface{})
assert.True(t, ok, "config should be a JSON object")

receivers, ok := config["receivers"].(map[string]interface{})
assert.True(t, ok, "receivers should be present in config")
assert.Contains(t, receivers, "otlp")

service, ok := config["service"].(map[string]interface{})
assert.True(t, ok, "service should be present in config")
assert.Contains(t, service, "pipelines")
}

func TestOpenTelemetryCollectorSpec_MarshalJSON(t *testing.T) {
spec := &OpenTelemetryCollectorSpec{
Mode: "deployment",
Config: Config{
Receivers: AnyConfig{
Object: map[string]interface{}{
"otlp": map[string]interface{}{
"protocols": map[string]interface{}{
"grpc": map[string]interface{}{},
},
},
},
},
},
}

jsonData, err := json.Marshal(spec)
if err != nil {
t.Fatalf("Failed to marshal OpenTelemetryCollectorSpec: %v", err)
}

var result map[string]interface{}
err = json.Unmarshal(jsonData, &result)
if err != nil {
t.Fatalf("Failed to unmarshal JSON data: %v", err)
}

if mode, ok := result["mode"].(string); !ok || mode != "deployment" {
t.Errorf("Expected mode to be 'deployment', got %v", result["mode"])
}

config, ok := result["config"].(map[string]interface{})
if !ok {
t.Fatalf("Expected config to be a map[string]interface{}, got %T", result["config"])
}

receivers, ok := config["receivers"].(map[string]interface{})
if !ok {
t.Fatalf("Expected receivers to be a map[string]interface{}, got %T", config["receivers"])
}

otlp, ok := receivers["otlp"].(map[string]interface{})
if !ok {
t.Fatalf("Expected otlp to be a map[string]interface{}, got %T", receivers["otlp"])
}

protocols, ok := otlp["protocols"].(map[string]interface{})
if !ok {
t.Fatalf("Expected protocols to be a map[string]interface{}, got %T", otlp["protocols"])
}

_, ok = protocols["grpc"].(map[string]interface{})
if !ok {
t.Fatalf("Expected grpc to be a map[string]interface{}, got %T", protocols["grpc"])
}
}
10 changes: 5 additions & 5 deletions controllers/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "2d266e55025628659355f1271b689d6fb53648ef6cd5595831f5835d18e59a25",
"opentelemetry-operator-config/sha256": "0e4f000d343bffbb2720617c6f76215a5cbb53ade9032e29372bdf7f22b52225",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand Down Expand Up @@ -430,7 +430,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "2d266e55025628659355f1271b689d6fb53648ef6cd5595831f5835d18e59a25",
"opentelemetry-operator-config/sha256": "0e4f000d343bffbb2720617c6f76215a5cbb53ade9032e29372bdf7f22b52225",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand Down Expand Up @@ -744,7 +744,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "2d266e55025628659355f1271b689d6fb53648ef6cd5595831f5835d18e59a25",
"opentelemetry-operator-config/sha256": "0e4f000d343bffbb2720617c6f76215a5cbb53ade9032e29372bdf7f22b52225",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand Down Expand Up @@ -1303,7 +1303,7 @@ service:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "42773025f65feaf30df59a306a9e38f1aaabe94c8310983beaddb7f648d699b0",
"opentelemetry-operator-config/sha256": "451c0c2de4a1081f7c4c636174a00b45a0df08a75968f657bcfa06b8fd98e0c6",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand Down Expand Up @@ -1760,7 +1760,7 @@ prometheus_cr:
"app.kubernetes.io/version": "latest",
},
Annotations: map[string]string{
"opentelemetry-operator-config/sha256": "42773025f65feaf30df59a306a9e38f1aaabe94c8310983beaddb7f648d699b0",
"opentelemetry-operator-config/sha256": "451c0c2de4a1081f7c4c636174a00b45a0df08a75968f657bcfa06b8fd98e0c6",
"prometheus.io/path": "/metrics",
"prometheus.io/port": "8888",
"prometheus.io/scrape": "true",
Expand Down
Loading
Loading