Skip to content

Commit

Permalink
Update and the prometheus metrics matching utilities to properly deal…
Browse files Browse the repository at this point in the history
… with IPv6 addresses
  • Loading branch information
alpeb committed Apr 29, 2024
1 parent b5af4c0 commit 98eacf4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
32 changes: 17 additions & 15 deletions test/integration/deep/norelay/norelay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package norelay
import (
"context"
"fmt"
"net"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -48,18 +49,10 @@ func TestNoRelay(t *testing.T) {
}
}

ip, err := TestHelper.Kubectl(
"", "-n", ns, "get", "po", "-l", "app=server-relay",
"-o", "jsonpath='{range .items[*]}{@.status.podIP}{end}'",
)
if err != nil {
testutil.AnnotatedFatalf(t, "failed to retrieve server-relay IP",
"failed to retrieve server-relay IP: %s\n%s", err, ip)
}
relayIP := strings.Trim(ip, "'")
relayIPPort := getPodIPPort(t, ns, "app=server-relay", 4140)
o, err := TestHelper.Kubectl(
"", "-n", ns, "exec", "deploy/client",
"--", "curl", "-f", "-H", "l5d-dst-override: server-hello."+ns+".svc.cluster.local:8080", "http://"+relayIP+":4140",
"--", "curl", "-f", "-H", "l5d-dst-override: server-hello."+ns+".svc.cluster.local:8080", "http://"+relayIPPort,
)
if err == nil || err.Error() != "exit status 22" {
testutil.AnnotatedFatalf(t, "no error or unexpected error returned",
Expand All @@ -83,7 +76,7 @@ func TestRelay(t *testing.T) {
deployments["server-relay"] = strings.ReplaceAll(
deployments["server-relay"],
"127.0.0.1:4140,[::1]:4140",
"0.0.0.0:4140",
"'[::]:4140'",
)
TestHelper.WithDataPlaneNamespace(ctx, "relay-test", map[string]string{}, t, func(t *testing.T, ns string) {
for name, res := range deployments {
Expand All @@ -96,12 +89,12 @@ func TestRelay(t *testing.T) {
}
}
waitForPods(t, ctx, ns, deployments)
relayIP := getPodIp(t, ns, "app=server-relay")
relayIPPort := getPodIPPort(t, ns, "app=server-relay", 4140)

// Send a request to the outbound proxy port with a header that should route internally.
o, err := TestHelper.Kubectl(
"", "-n", ns, "exec", "deploy/client",
"--", "curl", "-fsv", "-H", "l5d-dst-override: server-hello."+ns+".svc.cluster.local:8080", "http://"+relayIP+":4140",
"--", "curl", "-fsv", "-H", "l5d-dst-override: server-hello."+ns+".svc.cluster.local:8080", "http://"+relayIPPort,
)
if err != nil {
log, err := TestHelper.Kubectl(
Expand Down Expand Up @@ -153,7 +146,7 @@ func getDeployments(t *testing.T) map[string]string {
return deploys
}

func getPodIp(t *testing.T, ns, selector string) string {
func getPodIPPort(t *testing.T, ns, selector string, port int) string {
t.Helper()
ip, err := TestHelper.Kubectl(
"", "-n", ns, "get", "po", "-l", selector,
Expand All @@ -163,7 +156,16 @@ func getPodIp(t *testing.T, ns, selector string) string {
testutil.AnnotatedFatalf(t, "failed to retrieve pod IP",
"failed to retrieve pod IP: %s", err)
}
return strings.Trim(ip, "'")
ip = strings.Trim(ip, "'")
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
testutil.AnnotatedFatalf(t, "invalid pod IP",
"invalid pod IP: %s", err)
}
if parsedIP.To4() != nil {
return fmt.Sprintf("%s:%d", ip, port)
}
return fmt.Sprintf("[%s]:%d", ip, port)
}

func waitForPods(t *testing.T, ctx context.Context, ns string, deployments map[string]string) {
Expand Down
8 changes: 3 additions & 5 deletions testutil/prommatch/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ package prommatch
import "regexp"

var (
addressRe = regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+`)
iPRe = regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`)
portRe = regexp.MustCompile(`\d+`)
portRe = regexp.MustCompile(`\d+`)
)

// TargetAddrLabels match series with proper target_addr, target_port, and target_ip.
func TargetAddrLabels() Labels {
return Labels{
"target_addr": Like(addressRe),
"target_ip": Like(iPRe),
"target_addr": IsAddr(),
"target_ip": IsIP(),
"target_port": Like(portRe),
}
}
20 changes: 20 additions & 0 deletions testutil/prommatch/prommatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package prommatch
import (
"bytes"
"fmt"
"net"
"net/netip"
"regexp"

dto "github.com/prometheus/client_model/go"
Expand Down Expand Up @@ -146,6 +148,24 @@ func HasPositiveValue() Expression {
})
}

// IsAddr is used to check if the value is an IP:port combo, where IP can be
// an IPv4 or an IPv6
func IsAddr() LabelMatcher {
return func(s string) bool {
if _, err := netip.ParseAddrPort(s); err != nil {
return false
}
return true
}
}

// IsIP use used to check if the value is an IPv4 or IPv6
func IsIP() LabelMatcher {
return func(s string) bool {
return net.ParseIP(s) != nil
}
}

func hasName(metricName string) Expression {
return funcMatcher(func(sp *model.Sample) bool {
return sp.Metric[model.MetricNameLabel] == model.LabelValue(metricName)
Expand Down

0 comments on commit 98eacf4

Please sign in to comment.