Skip to content

Commit

Permalink
Allow to use the public IPv4 address as the primary if enabled (#1275)
Browse files Browse the repository at this point in the history
  • Loading branch information
argha-c authored Feb 3, 2020
1 parent cefadab commit 615448d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ public String getIpAddress() {
return hostInfo.first();
}

public boolean shouldBroadcastPublicIpv4Addr () { return false; }

private static Pair<String, String> getHostInfo() {
Pair<String, String> pair;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,16 @@ public String getHostName(boolean refresh) {

@Override
public String getIpAddress() {
String ipAddr = amazonInfoHolder.get().get(MetaDataKey.localIpv4);
return ipAddr == null ? super.getIpAddress() : ipAddr;
return this.shouldBroadcastPublicIpv4Addr() ? getPublicIpv4Addr() : getPrivateIpv4Addr();
}

private String getPrivateIpv4Addr() {
String privateIpv4Addr = amazonInfoHolder.get().get(MetaDataKey.localIpv4);
return privateIpv4Addr == null ? super.getIpAddress() : privateIpv4Addr;
}
private String getPublicIpv4Addr() {
String publicIpv4Addr = amazonInfoHolder.get().get(MetaDataKey.publicIpv4);
return publicIpv4Addr == null ? super.getIpAddress() : publicIpv4Addr;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ public String[] getDefaultAddressResolutionOrder() {
return result == null ? new String[0] : result.split(",");
}

/**
* Indicates if the public ipv4 address of the instance should be advertised.
* @return true if the public ipv4 address of the instance should be advertised, false otherwise .
*/
public boolean shouldBroadcastPublicIpv4Addr() {
return configInstance.getBooleanProperty(namespace + BROADCAST_PUBLIC_IPV4_ADDR_KEY, super.shouldBroadcastPublicIpv4Addr()).get();
}

@Override
public String getNamespace() {
return this.namespace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ final class PropertyBasedInstanceConfigConstants {
static final String INSTANCE_METADATA_PREFIX = "metadata";

static final String DEFAULT_ADDRESS_RESOLUTION_ORDER_KEY = "defaultAddressResolutionOrder";
static final String BROADCAST_PUBLIC_IPV4_ADDR_KEY = "broadcastPublicIpv4";
static final String TRAFFIC_ENABLED_ON_INIT_KEY = "traffic.enabled";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import static com.netflix.appinfo.AmazonInfo.MetaDataKey.publicHostname;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertEquals;


/**
* @author David Liu
Expand Down Expand Up @@ -38,6 +40,16 @@ public void testResolveDefaultAddress() {
assertThat(config.resolveDefaultAddress(false), is(dummyDefault));
}

@Test
public void testBroadcastPublicIpv4Address() {
AmazonInfo info = (AmazonInfo) instanceInfo.getDataCenterInfo();

config = createConfig(info);

// this should work because the test utils class sets the ipAddr to the public IP of the instance
assertEquals(instanceInfo.getIPAddr(), config.getIpAddress());
}

private CloudInstanceConfig createConfig(AmazonInfo info) {

return new CloudInstanceConfig(info) {
Expand All @@ -53,6 +65,9 @@ public String[] getDefaultAddressResolutionOrder() {
public String getHostName(boolean refresh) {
return dummyDefault;
}

@Override
public boolean shouldBroadcastPublicIpv4Addr() { return true; }
};
}
}

0 comments on commit 615448d

Please sign in to comment.