Skip to content
This repository has been archived by the owner on Oct 23, 2024. It is now read-only.

Remove limit of one IP address per A record, modify Task.IPs behavior… #553

Merged
merged 2 commits into from
Aug 19, 2020
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
9 changes: 9 additions & 0 deletions factories/fake.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@
"value": "da87d8aa-98f1-4fc3-a260-f92c116fc926"
},
"network_infos": [
{
"ip_addresses": [
{
"protocol": "IPv4",
"ip_address": "12.0.1.3"
}
],
"name": "dcos6"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically not a problem, but lulz. ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The network name? That's actually the real default ipv6 network name in dcos :-) https://github.com/dcos/dcos/blob/270cb17ec173dc52e69892c1c3ba796414f46936/gen/calc.py#L1140

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but that's not an ipv6 address ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, missed that!

},
{
"ip_addresses": [
{
Expand Down
37 changes: 4 additions & 33 deletions records/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion records/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand Down
8 changes: 6 additions & 2 deletions records/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions records/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down