hashicorp / go-sockaddr

IP Address/UNIX Socket convenience functions for Go
Mozilla Public License 2.0
266 stars 53 forks source link

Bind consul to correct private IP when GetPrivateInterfaces returns more than one. #33

Closed jserpapinto closed 4 years ago

jserpapinto commented 5 years ago

Hey,

I don't know GO. I'm running consul on DigitalOcean and I need to bind it to a private address. My issue is:

DigitalOcean has two private IP's because of it's Floating IP functionality, so all Droplets come with two private IP's.

[root@xxx-consul-server-0 ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 eth0
10.19.0.0       0.0.0.0         255.255.0.0     U     0      0        0 eth0
10.135.0.0      0.0.0.0         255.255.0.0     U     0      0        0 eth1
link-local      0.0.0.0         255.255.0.0     U     1002   0        0 eth0
link-local      0.0.0.0         255.255.0.0     U     1003   0        0 eth1
xxxxxxxxx     0.0.0.0         255.255.240.0   U     0      0        0 eth0

I was trying to run this with a command I checked in consul's page:

'GetPrivateInterfaces | include "network" "10.0.0.0/8" | attr "address"'

But it returns the "Floating IP" private IP instead of the Droplet's private IP.

So, I understood that with this command I was returning both IP addresses, filtering the cidr block and fetching one of the IP's. But the one it was being returned is the wrong one.

I got to this dumb solution:

'GetPrivateInterfaces | include "network" "10.0.0.0/8" | sort "-name" | attr "address"'

Where I just invert the sort by name and it returns me the one I want.

I'm wondering how should this be correctly done.

Thank you all.

karl-tpio commented 4 years ago

10.19.0.0 and 10.135.0.0 both have 255.255.0.0 as their netmask.

You're correct when you say filtering the cidr block but you're not using the filter as you need to as both 10.135/16 and 10.19/16 fall under 10/8. Change the filter so that it's scoped to which ever IP address you want.