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

fix host port handling for ipv6, fixes #16550 #16723

Merged
merged 4 commits into from
Apr 21, 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
3 changes: 3 additions & 0 deletions .changelog/16723.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
client: Fix address for ports in IPv6 networks
```
9 changes: 5 additions & 4 deletions client/taskenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
}
15 changes: 15 additions & 0 deletions client/taskenv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ func TestEnvironment_AsList(t *testing.T) {
},
},
}
a.AllocatedResources.Tasks["mail"] = &structs.AllocatedTaskResources{
Networks: []*structs.NetworkResource{
{
Device: "eth0",
IP: "fd12:3456:789a:1::1",
MBits: 50,
ReservedPorts: []structs.Port{
{Label: "ipv6", Value: 2222},
},
},
},
}
a.Namespace = "not-default"
task := a.Job.TaskGroups[0].Tasks[0]
task.Env = map[string]string{
Expand Down Expand Up @@ -220,6 +232,9 @@ func TestEnvironment_AsList(t *testing.T) {
"NOMAD_IP_ssh_ssh=192.168.0.100",
"NOMAD_PORT_ssh_other=1234",
"NOMAD_PORT_ssh_ssh=22",
"NOMAD_ADDR_mail_ipv6=[fd12:3456:789a:1::1]:2222",
"NOMAD_IP_mail_ipv6=fd12:3456:789a:1::1",
"NOMAD_PORT_mail_ipv6=2222",
"NOMAD_CPU_LIMIT=500",
"NOMAD_CPU_CORES=0,5-7",
"NOMAD_DC=dc1",
Expand Down
3 changes: 2 additions & 1 deletion command/agent/agent_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/url"
"os"
"reflect"
"strconv"
"strings"
"sync"
"syscall"
Expand Down Expand Up @@ -158,7 +159,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(member.Addr.String(), strconv.Itoa(int(member.Port)))

// Make the HTTP request
req, err := http.NewRequest("PUT",
Expand Down
4 changes: 3 additions & 1 deletion nomad/job_endpoint_hook_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package nomad
import (
"errors"
"fmt"
"net"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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)",
Expand Down
6 changes: 4 additions & 2 deletions nomad/structs/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package structs

import (
"fmt"
"net"
"reflect"
"sort"
"strconv"
"strings"

"github.com/hashicorp/nomad/helper/flatmap"
Expand Down Expand Up @@ -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)
Expand Down