From 4f29f94e5ae3120f94cdc6fa7f449b63b9863183 Mon Sep 17 00:00:00 2001 From: Pavel Valodzka Date: Thu, 30 Mar 2023 10:00:18 +0300 Subject: [PATCH] fix host port handling for ipv6, fixes #16550 --- client/taskenv/env.go | 9 +++++---- command/agent/agent_endpoint_test.go | 2 +- nomad/job_endpoint_hook_connect.go | 4 +++- nomad/structs/diff.go | 6 ++++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/client/taskenv/env.go b/client/taskenv/env.go index 12bb4cafc30..1bec377c87e 100644 --- a/client/taskenv/env.go +++ b/client/taskenv/env.go @@ -1042,7 +1042,7 @@ func (b *Builder) SetWorkloadToken(token string, inject bool) *Builder { // addPort keys and values for other tasks to an env var map func addPort(m map[string]string, taskName, ip, portLabel string, port int) { key := fmt.Sprintf("%s%s_%s", AddrPrefix, taskName, portLabel) - m[key] = fmt.Sprintf("%s:%d", ip, port) + m[key] = net.JoinHostPort(ip, strconv.Itoa(port)) key = fmt.Sprintf("%s%s_%s", IpPrefix, taskName, portLabel) m[key] = ip key = fmt.Sprintf("%s%s_%s", PortPrefix, taskName, portLabel) @@ -1063,8 +1063,9 @@ func addGroupPort(m map[string]string, port structs.Port) { func addPorts(m map[string]string, ports structs.AllocatedPorts) { for _, p := range ports { - m[AddrPrefix+p.Label] = fmt.Sprintf("%s:%d", p.HostIP, p.Value) - m[HostAddrPrefix+p.Label] = fmt.Sprintf("%s:%d", p.HostIP, p.Value) + port := strconv.Itoa(p.Value) + m[AddrPrefix+p.Label] = net.JoinHostPort(p.HostIP, port) + m[HostAddrPrefix+p.Label] = net.JoinHostPort(p.HostIP, port) m[IpPrefix+p.Label] = p.HostIP m[HostIpPrefix+p.Label] = p.HostIP if p.To > 0 { @@ -1077,6 +1078,6 @@ func addPorts(m map[string]string, ports structs.AllocatedPorts) { m[AllocPortPrefix+p.Label] = val } - m[HostPortPrefix+p.Label] = strconv.Itoa(p.Value) + m[HostPortPrefix+p.Label] = port } } diff --git a/command/agent/agent_endpoint_test.go b/command/agent/agent_endpoint_test.go index 8aa39cae0b3..a22b456df24 100644 --- a/command/agent/agent_endpoint_test.go +++ b/command/agent/agent_endpoint_test.go @@ -158,7 +158,7 @@ func TestHTTP_AgentJoin(t *testing.T) { httpTest(t, nil, func(s *TestAgent) { // Determine the join address member := s.Agent.Server().LocalMember() - addr := fmt.Sprintf("%s:%d", member.Addr, member.Port) + addr := net.JoinHostPort("%s:%d", member.Addr, member.Port) // Make the HTTP request req, err := http.NewRequest("PUT", diff --git a/nomad/job_endpoint_hook_connect.go b/nomad/job_endpoint_hook_connect.go index 864891475f1..6cf3667c333 100644 --- a/nomad/job_endpoint_hook_connect.go +++ b/nomad/job_endpoint_hook_connect.go @@ -6,6 +6,8 @@ package nomad import ( "errors" "fmt" + "net" + "strconv" "strings" "time" @@ -530,7 +532,7 @@ func groupConnectUpstreamsValidate(group string, services []*structs.Service) er for _, service := range services { if service.Connect.HasSidecar() && service.Connect.SidecarService.Proxy != nil { for _, up := range service.Connect.SidecarService.Proxy.Upstreams { - listener := fmt.Sprintf("%s:%d", up.LocalBindAddress, up.LocalBindPort) + listener := net.JoinHostPort(up.LocalBindAddress, strconv.Itoa(up.LocalBindPort)) if s, exists := listeners[listener]; exists { return fmt.Errorf( "Consul Connect services %q and %q in group %q using same address for upstreams (%s)", diff --git a/nomad/structs/diff.go b/nomad/structs/diff.go index 1363f528905..0312b2b4a18 100644 --- a/nomad/structs/diff.go +++ b/nomad/structs/diff.go @@ -5,8 +5,10 @@ package structs import ( "fmt" + "net" "reflect" "sort" + "strconv" "strings" "github.com/hashicorp/nomad/helper/flatmap" @@ -1369,11 +1371,11 @@ func connectGatewayProxyEnvoyBindAddrsDiff(prev, next map[string]*ConsulGatewayB nextMap := make(map[string]string, len(next)) for k, v := range prev { - prevMap[k] = fmt.Sprintf("%s:%d", v.Address, v.Port) + prevMap[k] = net.JoinHostPort(v.Address, strconv.Itoa(v.Port)) } for k, v := range next { - nextMap[k] = fmt.Sprintf("%s:%d", v.Address, v.Port) + nextMap[k] = net.JoinHostPort(v.Address, strconv.Itoa(v.Port)) } oldPrimitiveFlat := flatmap.Flatten(prevMap, nil, false)