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 692ddc592c
commit a2589e7815
9 changed files with 21 additions and 12 deletions

View file

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

View file

@ -15,6 +15,7 @@ package consul
import ( import (
"fmt" "fmt"
"net"
"strconv" "strconv"
"strings" "strings"
"time" "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 // since the service may be registered remotely through a different node
var addr string var addr string
if node.ServiceAddress != "" { if node.ServiceAddress != "" {
addr = fmt.Sprintf("%s:%d", node.ServiceAddress, node.ServicePort) addr = net.JoinHostPort(node.ServiceAddress, fmt.Sprintf("%d", node.ServicePort))
} else { } 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{ 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{} 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 { for _, record := range response.Answer {
target := model.LabelValue("") 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. // Remove the final dot from rooted DNS names to make them look more usual.
addr.Target = strings.TrimRight(addr.Target, ".") 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: case *dns.A:
target = model.LabelValue(fmt.Sprintf("%s:%d", addr.A, dd.port)) target = hostPort(addr.A.String(), dd.port)
case *dns.AAAA: case *dns.AAAA:
target = model.LabelValue(fmt.Sprintf("%s:%d", addr.AAAA, dd.port)) target = hostPort(addr.AAAA.String(), dd.port)
default: default:
log.Warnf("%q is not a valid SRV record", record) log.Warnf("%q is not a valid SRV record", record)
continue continue

View file

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

View file

@ -92,7 +92,7 @@ func (d *nodeDiscovery) updateNodesTargetGroup() *config.TargetGroup {
kubeletPort := int(node.Status.DaemonEndpoints.KubeletEndpoint.Port) 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{ t := model.LabelSet{
model.AddressLabel: model.LabelValue(address), model.AddressLabel: model.LabelValue(address),

View file

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

View file

@ -18,6 +18,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"net"
"net/http" "net/http"
"time" "time"
@ -229,5 +230,5 @@ func targetsForApp(app *App) []model.LabelSet {
} }
func targetForTask(task *Task) string { 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 ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net"
"sync" "sync"
"time" "time"
@ -133,7 +134,7 @@ func parseNerveMember(data []byte, path string) (*model.LabelSet, error) {
labels := model.LabelSet{} labels := model.LabelSet{}
labels[nervePathLabel] = model.LabelValue(path) labels[nervePathLabel] = model.LabelValue(path)
labels[model.AddressLabel] = model.LabelValue( 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+"_host"] = model.LabelValue(member.Host)
labels[nerveEndpointLabelPrefix+"_port"] = model.LabelValue(fmt.Sprintf("%d", member.Port)) labels[nerveEndpointLabelPrefix+"_port"] = model.LabelValue(fmt.Sprintf("%d", member.Port))

View file

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