Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netutil: consistently format ipv6 addresses #15153

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/netutil/netutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func resolveURL(ctx context.Context, lg *zap.Logger, u url.URL) (string, error)
)
return "", err
}
if host == "localhost" || net.ParseIP(host) != nil {
if host == "localhost" {
return "", nil
}
for ctx.Err() == nil {
Expand Down
29 changes: 20 additions & 9 deletions pkg/netutil/netutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,16 @@ func TestResolveTCPAddrs(t *testing.T) {
if err != nil {
return nil, err
}
if tt.hostMap[host] == "" {
return nil, errors.New("cannot resolve host")
}
i, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
if ip := net.ParseIP(host); ip != nil {
return &net.TCPAddr{IP: ip, Port: i, Zone: ""}, nil
}
if tt.hostMap[host] == "" {
return nil, errors.New("cannot resolve host")
}
return &net.TCPAddr{IP: net.ParseIP(tt.hostMap[host]), Port: i, Zone: ""}, nil
}
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
Expand All @@ -152,17 +155,20 @@ func TestURLsEqual(t *testing.T) {
"second.com": "10.0.11.2",
}
resolveTCPAddr = func(ctx context.Context, addr string) (*net.TCPAddr, error) {
host, port, herr := net.SplitHostPort(addr)
if herr != nil {
return nil, herr
}
if _, ok := hostm[host]; !ok {
return nil, errors.New("cannot resolve host.")
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
i, err := strconv.Atoi(port)
if err != nil {
return nil, err
}
if ip := net.ParseIP(host); ip != nil {
return &net.TCPAddr{IP: ip, Port: i, Zone: ""}, nil
}
if hostm[host] == "" {
return nil, errors.New("cannot resolve host")
}
return &net.TCPAddr{IP: net.ParseIP(hostm[host]), Port: i, Zone: ""}, nil
}

Expand Down Expand Up @@ -330,6 +336,11 @@ func TestURLStringsEqual(t *testing.T) {
"http://host1:8080",
"http://host2:8080",
}, errOnResolve},
{
urlsA: []string{"https://[c262:266f:fa53:0ee6:966e:e3f0:d68f:b046]:2380"},
urlsB: []string{"https://[c262:266f:fa53:ee6:966e:e3f0:d68f:b046]:2380"},
resolver: resolveTCPAddrDefault,
},
}
for idx, c := range cases {
t.Logf("TestURLStringsEqual, case #%d", idx)
Expand Down