From fce7bc2eb37dd6a9c01c7ddd72261f16010caa8d Mon Sep 17 00:00:00 2001 From: Kumar Atish Date: Wed, 18 Oct 2023 16:22:48 +0530 Subject: [PATCH] Fix antctl traceflow using IPv6 address (#5598) Fixes #5541 Signed-off-by: Kumar Atish --- pkg/antctl/raw/traceflow/command.go | 3 +++ pkg/antctl/raw/traceflow/command_test.go | 34 +++++++++++++++++------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/pkg/antctl/raw/traceflow/command.go b/pkg/antctl/raw/traceflow/command.go index 352db47a4b2..af929944b7d 100644 --- a/pkg/antctl/raw/traceflow/command.go +++ b/pkg/antctl/raw/traceflow/command.go @@ -460,6 +460,9 @@ func jsonOutput(r *Response, writer io.Writer) error { } func getTFName(prefix string) string { + // prefix may contain IPv6 address. Replace "::" and ":" to make it a valid RFC 1123 subdomain. + prefix = strings.ReplaceAll(prefix, "::", "-") + prefix = strings.ReplaceAll(prefix, ":", "-") if option.nowait { return prefix } diff --git a/pkg/antctl/raw/traceflow/command_test.go b/pkg/antctl/raw/traceflow/command_test.go index 97a15aaf3d6..89ca2e8aa0b 100644 --- a/pkg/antctl/raw/traceflow/command_test.go +++ b/pkg/antctl/raw/traceflow/command_test.go @@ -525,18 +525,32 @@ func TestNewTraceflow(t *testing.T) { func TestGetTFName(t *testing.T) { tests := []struct { - name string - prefix string - nowait string + name string + prefix string + nowait string + expected string }{ { - name: "nowait is true", - prefix: "traceflow", - nowait: "1", + name: "nowait is true", + prefix: "default-pod1-to-default-pod2", + nowait: "1", + expected: "default-pod1-to-default-pod2", }, { - name: "nowait is false", - prefix: "traceflow", + name: "nowait is true and prefix contains IPv6", + prefix: "default-pod1-to-fc00:f853:ccd:e793::2", + nowait: "1", + expected: "default-pod1-to-fc00-f853-ccd-e793-2", + }, + { + name: "nowait is false", + prefix: "default-pod1-to-default-pod2", + expected: "default-pod1-to-default-pod2", + }, + { + name: "nowait is false and prefix contains IPv6", + prefix: "default-pod1-to-fc00:f853:ccd:e793::2", + expected: "default-pod1-to-fc00-f853-ccd-e793-2", }, } @@ -547,9 +561,9 @@ func TestGetTFName(t *testing.T) { got := getTFName(tc.prefix) if tc.nowait != "" { - assert.Equal(t, tc.prefix, got) + assert.Equal(t, tc.expected, got) } else { - assert.Regexp(t, fmt.Sprintf("^%s-.{8}$", tc.prefix), got) + assert.Regexp(t, fmt.Sprintf("^%s-.{8}$", tc.expected), got) } }) }