From a2589e78157afdf35798a3e53bce28c7b270915e Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Mon, 5 Sep 2016 14:40:28 +0200 Subject: [PATCH] retrieval: correctly handle IPv6 addresses This updates all service discoveries to correctly build the __address__ label for IPv6 addresses. --- retrieval/discovery/azure.go | 3 ++- retrieval/discovery/consul/consul.go | 5 +++-- retrieval/discovery/dns/dns.go | 9 ++++++--- retrieval/discovery/ec2.go | 3 ++- retrieval/discovery/kubernetes/node.go | 2 +- retrieval/discovery/kubernetes/service.go | 2 +- retrieval/discovery/marathon/marathon.go | 3 ++- retrieval/discovery/nerve.go | 3 ++- retrieval/discovery/serverset.go | 3 ++- 9 files changed, 21 insertions(+), 12 deletions(-) diff --git a/retrieval/discovery/azure.go b/retrieval/discovery/azure.go index 629d21b2a..0f1334eb8 100644 --- a/retrieval/discovery/azure.go +++ b/retrieval/discovery/azure.go @@ -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 diff --git a/retrieval/discovery/consul/consul.go b/retrieval/discovery/consul/consul.go index e11d4acb5..28536b59c 100644 --- a/retrieval/discovery/consul/consul.go +++ b/retrieval/discovery/consul/consul.go @@ -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{ diff --git a/retrieval/discovery/dns/dns.go b/retrieval/discovery/dns/dns.go index 432f1c042..960507a0c 100644 --- a/retrieval/discovery/dns/dns.go +++ b/retrieval/discovery/dns/dns.go @@ -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 diff --git a/retrieval/discovery/ec2.go b/retrieval/discovery/ec2.go index e39016948..6d6e340ac 100644 --- a/retrieval/discovery/ec2.go +++ b/retrieval/discovery/ec2.go @@ -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 { diff --git a/retrieval/discovery/kubernetes/node.go b/retrieval/discovery/kubernetes/node.go index e64bc2eb1..26b173b65 100644 --- a/retrieval/discovery/kubernetes/node.go +++ b/retrieval/discovery/kubernetes/node.go @@ -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), diff --git a/retrieval/discovery/kubernetes/service.go b/retrieval/discovery/kubernetes/service.go index 3c0c6d04b..ac3bac33c 100644 --- a/retrieval/discovery/kubernetes/service.go +++ b/retrieval/discovery/kubernetes/service.go @@ -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), diff --git a/retrieval/discovery/marathon/marathon.go b/retrieval/discovery/marathon/marathon.go index 4c94d6a8d..d6bf7b444 100644 --- a/retrieval/discovery/marathon/marathon.go +++ b/retrieval/discovery/marathon/marathon.go @@ -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])) } diff --git a/retrieval/discovery/nerve.go b/retrieval/discovery/nerve.go index 8422ce38b..ed9c13acf 100644 --- a/retrieval/discovery/nerve.go +++ b/retrieval/discovery/nerve.go @@ -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)) diff --git a/retrieval/discovery/serverset.go b/retrieval/discovery/serverset.go index 48748518f..1b75c70da 100644 --- a/retrieval/discovery/serverset.go +++ b/retrieval/discovery/serverset.go @@ -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))