Skip to content

Commit dbd276e

Browse files
committed
Add more tests to Service.MetricsEndpoint and fix them
1 parent 8fbfa7c commit dbd276e

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

apis/v1beta1/config.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"reflect"
22+
"regexp"
2223
"sort"
2324
"strconv"
2425
"strings"
@@ -440,26 +441,29 @@ const (
440441
// MetricsEndpoint attempts gets the host and port number from the host address without doing any validation regarding the
441442
// address itself.
442443
// It works even before env var expansion happens, when a simple `net.SplitHostPort` would fail because of the extra colon
443-
// from the env var, i.e. the address looks like "${env:POD_IP}:4317" or "${env:POD_IP}".
444+
// from the env var, i.e. the address looks like "${env:POD_IP}:4317", "${env:POD_IP}", or "${POD_IP}".
444445
// It does not work in cases which the port itself is a variable, i.e. "${env:POD_IP}:${env:PORT}".
445-
// It does not work for IPv6 hostnames/addresses.
446+
// It does not work for IPv6 addresses.
446447
func (s *Service) MetricsEndpoint(logger logr.Logger) (string, int32) {
447448
telemetry := s.GetTelemetry()
448-
if telemetry == nil {
449+
if telemetry == nil || telemetry.Metrics.Address == "" {
449450
return defaultServiceHost, defaultServicePort
450451
}
451-
splitAddress := strings.Split(telemetry.Metrics.Address, ":")
452-
if len(splitAddress) == 1 {
453-
return defaultServiceHost, defaultServicePort
452+
453+
explicitPortMatches := regexp.MustCompile(`:(\d+$)`).FindStringSubmatch(telemetry.Metrics.Address)
454+
if len(explicitPortMatches) <= 1 {
455+
return telemetry.Metrics.Address, defaultServicePort
454456
}
455-
port, err := strconv.ParseInt(splitAddress[len(splitAddress)-1], 10, 32)
457+
458+
port, err := strconv.ParseInt(explicitPortMatches[1], 10, 32)
456459
if err != nil {
457460
errMsg := fmt.Sprintf("couldn't determine metrics port from configuration, using default: %s:%d",
458461
defaultServiceHost, defaultServicePort)
459462
logger.Info(errMsg, "error", err)
460463
return defaultServiceHost, defaultServicePort
461464
}
462-
host := strings.Join(splitAddress[0:len(splitAddress)-1], ":")
465+
466+
host, _, _ := strings.Cut(telemetry.Metrics.Address, explicitPortMatches[0])
463467
return host, int32(port)
464468
}
465469

apis/v1beta1/config_test.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -225,27 +225,55 @@ func TestConfigMetricsEndpoint(t *testing.T) {
225225
}{
226226
{
227227
"custom port",
228-
"0.0.0.0",
228+
"localhost",
229229
9090,
230230
Service{
231231
Telemetry: &AnyConfig{
232232
Object: map[string]interface{}{
233233
"metrics": map[string]interface{}{
234-
"address": "0.0.0.0:9090",
234+
"address": "localhost:9090",
235235
},
236236
},
237237
},
238238
},
239239
},
240240
{
241241
"missing port",
242-
"0.0.0.0",
242+
"localhost",
243243
8888,
244244
Service{
245245
Telemetry: &AnyConfig{
246246
Object: map[string]interface{}{
247247
"metrics": map[string]interface{}{
248-
"address": "0.0.0.0",
248+
"address": "localhost",
249+
},
250+
},
251+
},
252+
},
253+
},
254+
{
255+
"env var and missing port",
256+
"${env:POD_IP}",
257+
8888,
258+
Service{
259+
Telemetry: &AnyConfig{
260+
Object: map[string]interface{}{
261+
"metrics": map[string]interface{}{
262+
"address": "${env:POD_IP}",
263+
},
264+
},
265+
},
266+
},
267+
},
268+
{
269+
"env var and with port",
270+
"${POD_IP}",
271+
1234,
272+
Service{
273+
Telemetry: &AnyConfig{
274+
Object: map[string]interface{}{
275+
"metrics": map[string]interface{}{
276+
"address": "${POD_IP}:1234",
249277
},
250278
},
251279
},

0 commit comments

Comments
 (0)