Skip to content

Commit a515234

Browse files
authored
componentParser: set correct target port (#3141)
* componentParser: set correct target port Signed-off-by: Benedikt Bongartz <[email protected]> * componentParser: Only set targetport for parsers with config Signed-off-by: Benedikt Bongartz <[email protected]> --------- Signed-off-by: Benedikt Bongartz <[email protected]>
1 parent d2a9337 commit a515234

File tree

7 files changed

+60
-28
lines changed

7 files changed

+60
-28
lines changed

.chloggen/fix_invalid_port.yaml

+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. collector, target allocator, auto-instrumentation, opamp, 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: set correct target port in services created based on the given otel config.
9+
10+
# One or more tracking issues related to the change
11+
issues: [3139]
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/components/component.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,18 @@ type ComponentPortParser interface {
107107
}
108108

109109
func ConstructServicePort(current *corev1.ServicePort, port int32) corev1.ServicePort {
110-
return corev1.ServicePort{
110+
svc := corev1.ServicePort{
111111
Name: current.Name,
112112
Port: port,
113-
TargetPort: current.TargetPort,
114113
NodePort: current.NodePort,
115114
AppProtocol: current.AppProtocol,
116115
Protocol: current.Protocol,
117116
}
117+
118+
if port > 0 && current.TargetPort.IntValue() > 0 {
119+
svc.TargetPort = intstr.FromInt32(port)
120+
}
121+
return svc
118122
}
119123

120124
func GetPortsForConfig(logger logr.Logger, config map[string]interface{}, retriever ParserRetriever) ([]corev1.ServicePort, error) {

internal/components/multi_endpoint_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestMultiPortReceiver_Ports(t *testing.T) {
175175
{
176176
Name: "receiver3-http",
177177
Port: 80,
178-
TargetPort: intstr.FromInt32(8080),
178+
TargetPort: intstr.FromInt(80),
179179
},
180180
},
181181
wantErr: assert.NoError,
@@ -239,7 +239,7 @@ func TestMultiPortReceiver_Ports(t *testing.T) {
239239
{
240240
Name: "receiver6-grpc",
241241
Port: 4317,
242-
TargetPort: intstr.FromInt32(4317),
242+
TargetPort: intstr.FromInt(4317),
243243
Protocol: corev1.ProtocolTCP,
244244
AppProtocol: &components.GrpcProtocol,
245245
},

internal/components/receivers/helpers.go

+16-12
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,21 @@ var (
6868
)),
6969
components.NewMultiPortReceiver("jaeger",
7070
components.WithPortMapping(components.GrpcProtocol, 14250,
71+
components.WithTargetPort(14250),
7172
components.WithProtocol(corev1.ProtocolTCP),
7273
components.WithAppProtocol(&components.GrpcProtocol),
7374
),
7475
components.WithPortMapping("thrift_http", 14268,
76+
components.WithTargetPort(14268),
7577
components.WithProtocol(corev1.ProtocolTCP),
7678
components.WithAppProtocol(&components.HttpProtocol),
7779
),
7880
components.WithPortMapping("thrift_compact", 6831,
81+
components.WithTargetPort(6831),
7982
components.WithProtocol(corev1.ProtocolUDP),
8083
),
8184
components.WithPortMapping("thrift_binary", 6832,
85+
components.WithTargetPort(6832),
8286
components.WithProtocol(corev1.ProtocolUDP),
8387
),
8488
),
@@ -92,20 +96,20 @@ var (
9296
components.WithAppProtocol(&components.HttpProtocol),
9397
),
9498
),
95-
components.NewSinglePortParser("awsxray", 2000),
96-
components.NewSinglePortParser("carbon", 2003),
97-
components.NewSinglePortParser("collectd", 8081),
98-
components.NewSinglePortParser("fluentforward", 8006),
99-
components.NewSinglePortParser("influxdb", 8086),
100-
components.NewSinglePortParser("opencensus", 55678, components.WithAppProtocol(nil)),
101-
components.NewSinglePortParser("sapm", 7276),
102-
components.NewSinglePortParser("signalfx", 9943),
103-
components.NewSinglePortParser("splunk_hec", 8088),
104-
components.NewSinglePortParser("statsd", 8125, components.WithProtocol(corev1.ProtocolUDP)),
99+
components.NewSinglePortParser("awsxray", 2000, components.WithTargetPort(2000)),
100+
components.NewSinglePortParser("carbon", 2003, components.WithTargetPort(2003)),
101+
components.NewSinglePortParser("collectd", 8081, components.WithTargetPort(8081)),
102+
components.NewSinglePortParser("fluentforward", 8006, components.WithTargetPort(8006)),
103+
components.NewSinglePortParser("influxdb", 8086, components.WithTargetPort(8086)),
104+
components.NewSinglePortParser("opencensus", 55678, components.WithAppProtocol(nil), components.WithTargetPort(55678)),
105+
components.NewSinglePortParser("sapm", 7276, components.WithTargetPort(7276)),
106+
components.NewSinglePortParser("signalfx", 9943, components.WithTargetPort(9943)),
107+
components.NewSinglePortParser("splunk_hec", 8088, components.WithTargetPort(8088)),
108+
components.NewSinglePortParser("statsd", 8125, components.WithProtocol(corev1.ProtocolUDP), components.WithTargetPort(8125)),
105109
components.NewSinglePortParser("tcplog", components.UnsetPort, components.WithProtocol(corev1.ProtocolTCP)),
106110
components.NewSinglePortParser("udplog", components.UnsetPort, components.WithProtocol(corev1.ProtocolUDP)),
107-
components.NewSinglePortParser("wavefront", 2003),
108-
components.NewSinglePortParser("zipkin", 9411, components.WithAppProtocol(&components.HttpProtocol), components.WithProtocol(corev1.ProtocolTCP)),
111+
components.NewSinglePortParser("wavefront", 2003, components.WithTargetPort(2003)),
112+
components.NewSinglePortParser("zipkin", 9411, components.WithAppProtocol(&components.HttpProtocol), components.WithProtocol(corev1.ProtocolTCP), components.WithTargetPort(3100)),
109113
NewScraperParser("prometheus"),
110114
NewScraperParser("kubeletstats"),
111115
NewScraperParser("sshcheck"),

internal/components/receivers/multi_endpoint_receiver_test.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func TestMultiEndpointReceiverParsers(t *testing.T) {
5858
{
5959
Name: "jaeger-grpc",
6060
Port: 14250,
61+
TargetPort: intstr.FromInt(14250),
6162
Protocol: corev1.ProtocolTCP,
6263
AppProtocol: &grpc,
6364
},
@@ -77,6 +78,7 @@ func TestMultiEndpointReceiverParsers(t *testing.T) {
7778
{
7879
Name: "jaeger-grpc",
7980
Port: 1234,
81+
TargetPort: intstr.FromInt(1234),
8082
Protocol: corev1.ProtocolTCP,
8183
AppProtocol: &grpc,
8284
},
@@ -97,24 +99,28 @@ func TestMultiEndpointReceiverParsers(t *testing.T) {
9799
{
98100
Name: "jaeger-grpc",
99101
Port: 14250,
102+
TargetPort: intstr.FromInt(14250),
100103
Protocol: corev1.ProtocolTCP,
101104
AppProtocol: &grpc,
102105
},
103106
{
104107
Name: "port-14268",
105108
Port: 14268,
109+
TargetPort: intstr.FromInt(14268),
106110
Protocol: corev1.ProtocolTCP,
107111
AppProtocol: &http,
108112
},
109113
{
110-
Name: "port-6831",
111-
Port: 6831,
112-
Protocol: corev1.ProtocolUDP,
114+
Name: "port-6831",
115+
Port: 6831,
116+
TargetPort: intstr.FromInt(6831),
117+
Protocol: corev1.ProtocolUDP,
113118
},
114119
{
115-
Name: "port-6832",
116-
Port: 6832,
117-
Protocol: corev1.ProtocolUDP,
120+
Name: "port-6832",
121+
Port: 6832,
122+
TargetPort: intstr.FromInt(6832),
123+
Protocol: corev1.ProtocolUDP,
118124
},
119125
},
120126
},
@@ -155,7 +161,7 @@ func TestMultiEndpointReceiverParsers(t *testing.T) {
155161
{
156162
Name: "otlp-grpc",
157163
Port: 1234,
158-
TargetPort: intstr.FromInt32(4317),
164+
TargetPort: intstr.FromInt(1234),
159165
AppProtocol: &grpc,
160166
},
161167
},
@@ -221,7 +227,7 @@ func TestMultiEndpointReceiverParsers(t *testing.T) {
221227
{
222228
Name: "otlp-test-grpc",
223229
Port: 1234,
224-
TargetPort: intstr.FromInt32(4317),
230+
TargetPort: intstr.FromInt32(1234),
225231
AppProtocol: &grpc,
226232
},
227233
},
@@ -287,7 +293,7 @@ func TestMultiEndpointReceiverParsers(t *testing.T) {
287293
{
288294
Name: "loki-grpc",
289295
Port: 1234,
290-
TargetPort: intstr.FromInt32(9095),
296+
TargetPort: intstr.FromInt(1234),
291297
AppProtocol: &grpc,
292298
},
293299
},
@@ -353,7 +359,7 @@ func TestMultiEndpointReceiverParsers(t *testing.T) {
353359
{
354360
Name: "skywalking-grpc",
355361
Port: 1234,
356-
TargetPort: intstr.FromInt32(11800),
362+
TargetPort: intstr.FromInt(1234),
357363
AppProtocol: &grpc,
358364
},
359365
},

internal/components/single_endpoint_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func TestSingleEndpointParser_Ports(t *testing.T) {
247247
{
248248
Name: "testparser",
249249
Port: 8080,
250-
TargetPort: intstr.FromInt32(4317),
250+
TargetPort: intstr.FromInt32(8080),
251251
Protocol: corev1.ProtocolTCP,
252252
AppProtocol: &components.GrpcProtocol,
253253
},
@@ -357,7 +357,7 @@ func TestNewSilentSinglePortParser_Ports(t *testing.T) {
357357
{
358358
Name: "testparser",
359359
Port: 8080,
360-
TargetPort: intstr.FromInt32(4317),
360+
TargetPort: intstr.FromInt32(8080),
361361
Protocol: corev1.ProtocolTCP,
362362
AppProtocol: &components.GrpcProtocol,
363363
},

internal/manifests/collector/service_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/stretchr/testify/assert"
2121
v1 "k8s.io/api/core/v1"
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apimachinery/pkg/util/intstr"
2324

2425
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
2526
"github.com/open-telemetry/opentelemetry-operator/internal/config"
@@ -331,6 +332,7 @@ func serviceWithInternalTrafficPolicy(name string, ports []v1beta1.PortsSpec, in
331332

332333
svcPorts := []v1.ServicePort{}
333334
for _, p := range ports {
335+
p.ServicePort.TargetPort = intstr.FromInt32(p.Port)
334336
svcPorts = append(svcPorts, p.ServicePort)
335337
}
336338

0 commit comments

Comments
 (0)