Skip to content

Commit 1387db7

Browse files
authored
Validate port from the collector config in the webhook (#4399)
* Validate portt from the collector config in the webhook Signed-off-by: Pavol Loffay <[email protected]> * Validate portt from the collector config in the webhook Signed-off-by: Pavol Loffay <[email protected]> * Validate portt from the collector config in the webhook Signed-off-by: Pavol Loffay <[email protected]> * chore: fix tests Signed-off-by: Pavol Loffay <[email protected]> --------- Signed-off-by: Pavol Loffay <[email protected]>
1 parent c48adec commit 1387db7

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed

.chloggen/validate-ports.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
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: collector
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Validate collector ports from the config to ensure they are within the valid range (1-65535).
9+
10+
# One or more tracking issues related to the change
11+
issues: [4399]
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: |
17+
- This change adds validation for collector ports specified in the configuration file.
18+
- Ports must be within the range of 1 to 65535; otherwise, an error will be raised during CR creation.
19+
- This helps prevent misconfigurations that could lead to runtime errors.

apis/v1beta1/collector_webhook.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import (
2323
"github.com/open-telemetry/opentelemetry-operator/pkg/featuregate"
2424
)
2525

26+
const (
27+
maxPortLen = 15
28+
)
29+
2630
var (
2731
_ admission.CustomValidator = &CollectorWebhook{}
2832
_ admission.CustomDefaulter = &CollectorWebhook{}
@@ -225,6 +229,21 @@ func (c CollectorWebhook) Validate(ctx context.Context, r *OpenTelemetryCollecto
225229
if err := ValidatePorts(r.Spec.Ports); err != nil {
226230
return warnings, err
227231
}
232+
ports, errPorts := r.Spec.Config.GetAllPorts(c.logger)
233+
if errPorts != nil {
234+
return warnings, fmt.Errorf("the OpenTelemetry config is incorrect. The port numbers are invalid: %w", errPorts)
235+
}
236+
for _, p := range ports {
237+
truncName := naming.Truncate(p.Name, maxPortLen)
238+
if truncName != p.Name {
239+
warnings = append(warnings, fmt.Sprintf("the OpenTelemetry config port name '%s' exceeds the maximum length of 15 characters and has been truncated to '%s'", p.Name, truncName))
240+
}
241+
nameErrs := validation.IsValidPortName(truncName)
242+
numErrs := validation.IsValidPortNum(int(p.Port))
243+
if len(nameErrs) > 0 || len(numErrs) > 0 {
244+
return warnings, fmt.Errorf("the OpenTelemetry config is incorrect. The port name '%s' errors: %s, num '%d' errors: %s", p.Name, nameErrs, p.Port, numErrs)
245+
}
246+
}
228247

229248
var maxReplicas *int32
230249
if r.Spec.Autoscaler != nil && r.Spec.Autoscaler.MaxReplicas != nil {

apis/v1beta1/collector_webhook_test.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,138 @@ func TestOTELColValidatingWebhook(t *testing.T) {
13951395
},
13961396
expectedErr: "the OpenTelemetry Spec Ports configuration is incorrect",
13971397
},
1398+
{
1399+
name: "invalid port number in config - too large",
1400+
otelcol: v1beta1.OpenTelemetryCollector{
1401+
Spec: v1beta1.OpenTelemetryCollectorSpec{
1402+
Config: v1beta1.Config{
1403+
Receivers: v1beta1.AnyConfig{
1404+
Object: map[string]interface{}{
1405+
"otlp": map[string]interface{}{
1406+
"protocols": map[string]interface{}{
1407+
"grpc": map[string]interface{}{
1408+
"endpoint": "0.0.0.0:65536",
1409+
},
1410+
},
1411+
},
1412+
},
1413+
},
1414+
Exporters: v1beta1.AnyConfig{
1415+
Object: map[string]interface{}{
1416+
"debug": map[string]interface{}{},
1417+
},
1418+
},
1419+
Service: v1beta1.Service{
1420+
Pipelines: map[string]*v1beta1.Pipeline{
1421+
"traces": {
1422+
Receivers: []string{"otlp"},
1423+
Exporters: []string{"debug"},
1424+
},
1425+
},
1426+
},
1427+
},
1428+
},
1429+
},
1430+
expectedErr: "the OpenTelemetry config is incorrect. The port",
1431+
},
1432+
{
1433+
name: "port validation - zero port currently not validated",
1434+
otelcol: v1beta1.OpenTelemetryCollector{
1435+
Spec: v1beta1.OpenTelemetryCollectorSpec{
1436+
Config: v1beta1.Config{
1437+
Receivers: v1beta1.AnyConfig{
1438+
Object: map[string]interface{}{
1439+
"otlp": map[string]interface{}{
1440+
"protocols": map[string]interface{}{
1441+
"grpc": map[string]interface{}{
1442+
"endpoint": "0.0.0.0:0",
1443+
},
1444+
},
1445+
},
1446+
},
1447+
},
1448+
Exporters: v1beta1.AnyConfig{
1449+
Object: map[string]interface{}{
1450+
"debug": map[string]interface{}{},
1451+
},
1452+
},
1453+
Service: v1beta1.Service{
1454+
Pipelines: map[string]*v1beta1.Pipeline{
1455+
"traces": {
1456+
Receivers: []string{"otlp"},
1457+
Exporters: []string{"debug"},
1458+
},
1459+
},
1460+
},
1461+
},
1462+
},
1463+
},
1464+
// TODO: This should fail validation when port validation is fully implemented
1465+
},
1466+
{
1467+
name: "port validation - negative port currently not validated",
1468+
otelcol: v1beta1.OpenTelemetryCollector{
1469+
Spec: v1beta1.OpenTelemetryCollectorSpec{
1470+
Config: v1beta1.Config{
1471+
Receivers: v1beta1.AnyConfig{
1472+
Object: map[string]interface{}{
1473+
"otlp": map[string]interface{}{
1474+
"protocols": map[string]interface{}{
1475+
"grpc": map[string]interface{}{
1476+
"endpoint": "0.0.0.0:-1",
1477+
},
1478+
},
1479+
},
1480+
},
1481+
},
1482+
Exporters: v1beta1.AnyConfig{
1483+
Object: map[string]interface{}{
1484+
"debug": map[string]interface{}{},
1485+
},
1486+
},
1487+
Service: v1beta1.Service{
1488+
Pipelines: map[string]*v1beta1.Pipeline{
1489+
"traces": {
1490+
Receivers: []string{"otlp"},
1491+
Exporters: []string{"debug"},
1492+
},
1493+
},
1494+
},
1495+
},
1496+
},
1497+
},
1498+
// TODO: This should fail validation when port validation is fully implemented
1499+
},
1500+
{
1501+
name: "invalid port number in zipkin receiver config - too large",
1502+
otelcol: v1beta1.OpenTelemetryCollector{
1503+
Spec: v1beta1.OpenTelemetryCollectorSpec{
1504+
Config: v1beta1.Config{
1505+
Receivers: v1beta1.AnyConfig{
1506+
Object: map[string]interface{}{
1507+
"zipkin": map[string]interface{}{
1508+
"endpoint": "0.0.0.0:65537",
1509+
},
1510+
},
1511+
},
1512+
Exporters: v1beta1.AnyConfig{
1513+
Object: map[string]interface{}{
1514+
"debug": map[string]interface{}{},
1515+
},
1516+
},
1517+
Service: v1beta1.Service{
1518+
Pipelines: map[string]*v1beta1.Pipeline{
1519+
"traces": {
1520+
Receivers: []string{"zipkin"},
1521+
Exporters: []string{"debug"},
1522+
},
1523+
},
1524+
},
1525+
},
1526+
},
1527+
},
1528+
expectedErr: "the OpenTelemetry config is incorrect. The port",
1529+
},
13981530
}
13991531

14001532
bv := func(_ context.Context, collector v1beta1.OpenTelemetryCollector) admission.Warnings {

0 commit comments

Comments
 (0)