retrieval: correctly handle IPv6 addresses

This updates all service discoveries to correctly
build the __address__ label for IPv6 addresses.
This commit is contained in:
Fabian Reinartz 2016-09-05 14:40:28 +02:00
parent 17cdd4f966
commit a15237a0b8
9 changed files with 21 additions and 12 deletions

View File

@ -15,6 +15,7 @@ package discovery
import (
"fmt"
"net"
"strings"
"time"
@ -217,7 +218,7 @@ func (ad *AzureDiscovery) refresh() (*config.TargetGroup, error) {
for _, ip := range *networkInterface.Properties.IPConfigurations {
if ip.Properties.PrivateIPAddress != nil {
labels[azureLabelMachinePrivateIP] = model.LabelValue(*ip.Properties.PrivateIPAddress)
address := fmt.Sprintf("%s:%d", *ip.Properties.PrivateIPAddress, ad.port)
address := net.JoinHostPort(*ip.Properties.PrivateIPAddress, fmt.Sprintf("%d", ad.port))
labels[model.AddressLabel] = model.LabelValue(address)
ch <- target{labelSet: labels, err: nil}
return

View File

@ -15,6 +15,7 @@ package consul
import (
"fmt"
"net"
"strconv"
"strings"
"time"
@ -238,9 +239,9 @@ func (srv *consulService) watch(ctx context.Context, ch chan<- []*config.TargetG
// since the service may be registered remotely through a different node
var addr string
if node.ServiceAddress != "" {
addr = fmt.Sprintf("%s:%d", node.ServiceAddress, node.ServicePort)
addr = net.JoinHostPort(node.ServiceAddress, fmt.Sprintf("%d", node.ServicePort))
} else {
addr = fmt.Sprintf("%s:%d", node.Address, node.ServicePort)
addr = net.JoinHostPort(node.Address, fmt.Sprintf("%d", node.ServicePort))
}
tgroup.Targets = append(tgroup.Targets, model.LabelSet{

View File

@ -133,6 +133,9 @@ func (dd *Discovery) refresh(ctx context.Context, name string, ch chan<- []*conf
}
tg := &config.TargetGroup{}
hostPort := func(a string, p int) model.LabelValue {
return model.LabelValue(net.JoinHostPort(a, fmt.Sprintf("%d", p)))
}
for _, record := range response.Answer {
target := model.LabelValue("")
@ -141,11 +144,11 @@ func (dd *Discovery) refresh(ctx context.Context, name string, ch chan<- []*conf
// Remove the final dot from rooted DNS names to make them look more usual.
addr.Target = strings.TrimRight(addr.Target, ".")
target = model.LabelValue(fmt.Sprintf("%s:%d", addr.Target, addr.Port))
target = hostPort(addr.Target, int(addr.Port))
case *dns.A:
target = model.LabelValue(fmt.Sprintf("%s:%d", addr.A, dd.port))
target = hostPort(addr.A.String(), dd.port)
case *dns.AAAA:
target = model.LabelValue(fmt.Sprintf("%s:%d", addr.AAAA, dd.port))
target = hostPort(addr.AAAA.String(), dd.port)
default:
log.Warnf("%q is not a valid SRV record", record)
continue

View File

@ -15,6 +15,7 @@ package discovery
import (
"fmt"
"net"
"strings"
"time"
@ -112,7 +113,7 @@ func (ed *EC2Discovery) refresh() (*config.TargetGroup, error) {
ec2LabelInstanceID: model.LabelValue(*inst.InstanceId),
}
labels[ec2LabelPrivateIP] = model.LabelValue(*inst.PrivateIpAddress)
addr := fmt.Sprintf("%s:%d", *inst.PrivateIpAddress, ed.port)
addr := net.JoinHostPort(*inst.PrivateIpAddress, fmt.Sprintf("%d", ed.port))
labels[model.AddressLabel] = model.LabelValue(addr)
if inst.PublicIpAddress != nil {

View File

@ -92,7 +92,7 @@ func (d *nodeDiscovery) updateNodesTargetGroup() *config.TargetGroup {
kubeletPort := int(node.Status.DaemonEndpoints.KubeletEndpoint.Port)
address := fmt.Sprintf("%s:%d", defaultNodeAddress.String(), kubeletPort)
address := net.JoinHostPort(defaultNodeAddress.String(), fmt.Sprintf("%d", kubeletPort))
t := model.LabelSet{
model.AddressLabel: model.LabelValue(address),

View File

@ -337,7 +337,7 @@ func (d *serviceDiscovery) updateServiceTargetGroup(service *Service, eps *Endpo
if len(ipAddr) == net.IPv6len {
ipAddr = "[" + ipAddr + "]"
}
address := fmt.Sprintf("%s:%d", ipAddr, epPort)
address := net.JoinHostPort(ipAddr, fmt.Sprintf("%d", epPort))
t := model.LabelSet{
model.AddressLabel: model.LabelValue(address),

View File

@ -18,6 +18,7 @@ import (
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/http"
"time"
@ -229,5 +230,5 @@ func targetsForApp(app *App) []model.LabelSet {
}
func targetForTask(task *Task) string {
return fmt.Sprintf("%s:%d", task.Host, task.Ports[0])
return net.JoinHostPort(task.Host, fmt.Sprintf("%d", task.Ports[0]))
}

View File

@ -16,6 +16,7 @@ package discovery
import (
"encoding/json"
"fmt"
"net"
"sync"
"time"
@ -133,7 +134,7 @@ func parseNerveMember(data []byte, path string) (*model.LabelSet, error) {
labels := model.LabelSet{}
labels[nervePathLabel] = model.LabelValue(path)
labels[model.AddressLabel] = model.LabelValue(
fmt.Sprintf("%s:%d", member.Host, member.Port))
net.JoinHostPort(member.Host, fmt.Sprintf("%d", member.Port)))
labels[nerveEndpointLabelPrefix+"_host"] = model.LabelValue(member.Host)
labels[nerveEndpointLabelPrefix+"_port"] = model.LabelValue(fmt.Sprintf("%d", member.Port))

View File

@ -16,6 +16,7 @@ package discovery
import (
"encoding/json"
"fmt"
"net"
"strconv"
"sync"
"time"
@ -143,7 +144,7 @@ func parseServersetMember(data []byte, path string) (*model.LabelSet, error) {
labels := model.LabelSet{}
labels[serversetPathLabel] = model.LabelValue(path)
labels[model.AddressLabel] = model.LabelValue(
fmt.Sprintf("%s:%d", member.ServiceEndpoint.Host, member.ServiceEndpoint.Port))
net.JoinHostPort(member.ServiceEndpoint.Host, fmt.Sprintf("%d", member.ServiceEndpoint.Port)))
labels[serversetEndpointLabelPrefix+"_host"] = model.LabelValue(member.ServiceEndpoint.Host)
labels[serversetEndpointLabelPrefix+"_port"] = model.LabelValue(fmt.Sprintf("%d", member.ServiceEndpoint.Port))