util/addr: Fixes findIP to return the correct public IP #2673
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When run on a host with no private IP, but with a public IP available,
go-micro
incorrectly figures out its service's address and uses the loopback address. This is due to thefindIP
function inutils/addr
returning the wrong IP address when run on a host with a Public IP and no Private IP, leading togo-micro
advertising the wrong address for its service (e.g. when usingetcd
as the registry).What was done
util/addr_test.go
with a test forfindIP
.findIP
to return the first public IP (if available) if no private IP is available.findIP
to return the first loopback IP (if available) if no public IP is available.Extract
function documentation.How to reproduce the initial issue
To reproduce the initial issue, you can setup a docker-compose with
etcd
and ago-micro
service and check the information available inetcd
.Details
Step 1. Create a
go-micro
app with the followingmain.go
:Step 2. Use the following Dockerfile to build the application's image with the tag
helloworld:latest
:Step 3. Use the following
docker-compose.yaml
to deployetcd
alongside the app:Step 4. Use the following command to get the address advertised by the service:
docker exec -it etcd-test "/opt/bitnami/etcd/bin/etcdctl" get "" --prefix | sed -n '2 p' | jq '.nodes | first.address'
With the docker network setup with a public IP range, it will be:
"127.0.0.1:8080"
, while without, it will use the container private IP.