diff --git a/factories/fake.json b/factories/fake.json index c219f05e..af7e1957 100644 --- a/factories/fake.json +++ b/factories/fake.json @@ -79,6 +79,15 @@ "value": "da87d8aa-98f1-4fc3-a260-f92c116fc926" }, "network_infos": [ + { + "ip_addresses": [ + { + "protocol": "IPv4", + "ip_address": "12.0.1.3" + } + ], + "name": "dcos6" + }, { "ip_addresses": [ { diff --git a/records/generator.go b/records/generator.go index 8700b363..774e0c79 100644 --- a/records/generator.go +++ b/records/generator.go @@ -434,14 +434,12 @@ func (rg *RecordGenerator) taskContextRecord(ctx context, task state.Task, f sta canonical := ctx.taskName + "-" + ctx.taskID + "-" + ctx.slaveID + "." + fname arec := ctx.taskName + "." + fname - // Only use the first ipv4 and first ipv6 found in sources - tIPs := ipsTo4And6(ctx.taskIPs) - for _, tIP := range tIPs { + // Use the IPs from the first populated source + for _, tIP := range ctx.taskIPs { rg.insertTaskRR(arec+tail, tIP.String(), rrsKindForIP(tIP), enumTask) rg.insertTaskRR(canonical+tail, tIP.String(), rrsKindForIP(tIP), enumTask) } - // slaveIPs already only has at most one ipv4 and one ipv6 for _, sIPStr := range ctx.slaveIPs { if sIP := net.ParseIP(sIPStr); sIP != nil { rg.insertTaskRR(arec+".slave"+tail, sIP.String(), rrsKindForIP(sIP), enumTask) @@ -567,41 +565,14 @@ func rrsKindForIPStr(ip string) rrsKind { panic("unable to parse ip: " + ip) } -// ipsTo4And6 returns a list with at most 1 ipv4 and 1 ipv6 -// from a list of IPs -func ipsTo4And6(allIPs []net.IP) (ips []net.IP) { - var ipv4, ipv6 net.IP - for _, ip := range allIPs { - if ipv4 != nil && ipv6 != nil { - break - } else if t4 := ip.To4(); t4 != nil { - if ipv4 == nil { - ipv4 = t4 - } - } else if t6 := ip.To16(); t6 != nil { - if ipv6 == nil { - ipv6 = t6 - } - } - } - ips = []net.IP{} - if ipv4 != nil { - ips = append(ips, ipv4) - } - if ipv6 != nil { - ips = append(ips, ipv6) - } - return -} - // hostToIPs attempts to parse a hostname into an ip. // If that doesn't work it will perform a lookup and try to -// find one ipv4 and one ipv6 in the results. +// find all ipv4 and ipv6 addresses for the hostname. func hostToIPs(hostname string) (ips []net.IP) { if ip := net.ParseIP(hostname); ip != nil { ips = []net.IP{ip} } else if allIPs, err := net.LookupIP(hostname); err == nil { - ips = ipsTo4And6(allIPs) + ips = allIPs } if len(ips) == 0 { logging.VeryVerbose.Printf("cannot translate hostname %q into an ipv4 or ipv6 address", hostname) diff --git a/records/generator_test.go b/records/generator_test.go index e17346a3..d4615d20 100644 --- a/records/generator_test.go +++ b/records/generator_test.go @@ -328,7 +328,7 @@ func TestInsertState(t *testing.T) { {rgMesos.AAAAs, "toy-store.ipv6-framework.mesos.", []string{"2001:db8::1"}}, {rgMesos.AAAAs, "toy-store.ipv6-framework.slave.mesos.", []string{"2001:db8::1"}}, - {rgNetinfo.As, "toy-store.ipv6-framework.mesos.", []string{"12.0.1.2"}}, + {rgNetinfo.As, "toy-store.ipv6-framework.mesos.", []string{"12.0.1.3", "12.0.1.2"}}, {rgNetinfo.AAAAs, "toy-store.ipv6-framework.mesos.", []string{"fd01:b::1:8000:2"}}, {rgNetinfo.AAAAs, "toy-store.ipv6-framework.slave.mesos.", []string{"2001:db8::1"}}, diff --git a/records/state/state.go b/records/state/state.go index c1907e84..9111f3ac 100644 --- a/records/state/state.go +++ b/records/state/state.go @@ -111,7 +111,7 @@ func (t *Task) IP(srcs ...string) string { } // IPs returns a slice of IPs sourced from the given sources with ascending -// priority. +// priority. Returns only the IPs from the first populated source. func (t *Task) IPs(srcs ...string) (ips []net.IP) { if t == nil { return nil @@ -124,6 +124,10 @@ func (t *Task) IPs(srcs ...string) (ips []net.IP) { } } } + // Return IPs from first populated source + if len(ips) > 0 { + return ips + } } return ips } @@ -144,7 +148,7 @@ func hostIPs(t *Task) []string { return t.SlaveIPs } // []Status.ContainerStatus.[]NetworkInfos.[]IPAddresses.IPAddress func networkInfoIPs(t *Task) []string { return statusIPs(t.Statuses, func(s *Status) []string { - ips := make([]string, len(s.ContainerStatus.NetworkInfos)) + ips := []string{} for _, netinfo := range s.ContainerStatus.NetworkInfos { if len(netinfo.IPAddresses) > 0 { // In v0.26, we use the IPAddresses field. diff --git a/records/state/state_test.go b/records/state/state_test.go index 498d01b1..2ee7509d 100644 --- a/records/state/state_test.go +++ b/records/state/state_test.go @@ -86,13 +86,21 @@ func TestTask_IPs(t *testing.T) { srcs: []string{"netinfo"}, want: ips("1.2.4.8"), }, - { // source order + { // source order host Task: task( slaveIPs("2.3.4.5"), statuses(status(state("TASK_RUNNING"), netinfos(netinfo("1.2.3.4", "fd01:b::1:8000:2")))), ), srcs: []string{"host", "netinfo"}, - want: ips("2.3.4.5", "1.2.3.4", "fd01:b::1:8000:2"), + want: ips("2.3.4.5"), + }, + { // source order netinfo + Task: task( + slaveIPs("2.3.4.5"), + statuses(status(state("TASK_RUNNING"), netinfos(netinfo("1.2.3.4", "fd01:b::1:8000:2")))), + ), + srcs: []string{"netinfo", "host"}, + want: ips("1.2.3.4", "fd01:b::1:8000:2"), }, { // statuses state Task: task(