Skip to content

Commit 18e50b0

Browse files
dexter0195swiatekm
andauthored
Fix/config exporter endpoint panic (#2653)
* fix: exporter panic on malformed endpoint configuration * feat: adds tests for Ports in exporter.go * add chloggen file * adds header + fix test * Update internal/manifests/collector/parser/exporter/exporter_test.go Co-authored-by: Mikołaj Świątek <[email protected]> * fix: test exporter_test.go --------- Co-authored-by: Mikołaj Świątek <[email protected]>
1 parent 699e034 commit 18e50b0

File tree

3 files changed

+99
-3
lines changed

3 files changed

+99
-3
lines changed
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: "bug_fix"
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
5+
component: operator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: "Fixes a panic on exporter prometheus endpoint not valid"
9+
10+
# One or more tracking issues related to the change
11+
issues: [2628]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

internal/manifests/collector/parser/exporter/exporter_prometheus.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ func (o *PrometheusExporterParser) Ports() ([]corev1.ServicePort, error) {
5959
},
6060
)
6161
} else {
62-
ports = append(
63-
ports, *singlePortFromConfigEndpoint(o.logger, o.name, o.config),
64-
)
62+
if port := singlePortFromConfigEndpoint(o.logger, o.name, o.config); port != nil {
63+
ports = append(ports, *port)
64+
}
6565
}
6666

6767
return ports, nil
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package exporter
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/assert"
21+
22+
v1 "k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/util/intstr"
24+
)
25+
26+
func TestPorts(t *testing.T) {
27+
tests := []struct {
28+
testName string
29+
parser *PrometheusExporterParser
30+
want []v1.ServicePort
31+
}{
32+
{
33+
testName: "Valid Configuration",
34+
parser: &PrometheusExporterParser{
35+
name: "test-exporter",
36+
config: map[interface{}]interface{}{
37+
"endpoint": "http://myprometheus.io:9090",
38+
},
39+
},
40+
want: []v1.ServicePort{
41+
{
42+
Name: "test-exporter",
43+
Port: 9090,
44+
},
45+
},
46+
},
47+
{
48+
testName: "Empty Configuration",
49+
parser: &PrometheusExporterParser{
50+
name: "test-exporter",
51+
config: nil, // Simulate no configuration provided
52+
},
53+
want: []v1.ServicePort{
54+
{
55+
Name: "test-exporter",
56+
Port: defaultPrometheusPort,
57+
TargetPort: intstr.FromInt(int(defaultPrometheusPort)),
58+
Protocol: v1.ProtocolTCP,
59+
},
60+
},
61+
},
62+
{
63+
testName: "Invalid Endpoint No Port",
64+
parser: &PrometheusExporterParser{
65+
name: "test-exporter",
66+
config: map[interface{}]interface{}{
67+
"endpoint": "invalidendpoint",
68+
},
69+
},
70+
want: []v1.ServicePort{},
71+
},
72+
}
73+
74+
for _, tt := range tests {
75+
t.Run(tt.testName, func(t *testing.T) {
76+
ports, _ := tt.parser.Ports()
77+
assert.Equal(t, tt.want, ports)
78+
})
79+
}
80+
}

0 commit comments

Comments
 (0)