Skip to content

Commit

Permalink
Add support for EIPs associated with secondary ENIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Tucker committed Sep 14, 2023
1 parent efc1cf6 commit da18301
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
25 changes: 25 additions & 0 deletions eureka-client/src/main/java/com/netflix/appinfo/AmazonInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -63,6 +64,13 @@ public enum MetaDataKey {
availabilityZone("availability-zone", "placement/"),
publicHostname("public-hostname"),
publicIpv4("public-ipv4"),
macs("macs", "network/interfaces/"), // macs declared above public-ipv4s so will be found before publicIpv4s (where it is needed)
publicIpv4s("public-ipv4s", "network/interfaces/macs/") {
@Override
public URL getURL(String prepend, String mac) throws MalformedURLException {
return new URL(AWS_METADATA_URL + this.path + mac + "/" + this.name);
}
},
ipv6("ipv6"),
spotTerminationTime("termination-time", "spot/"),
spotInstanceAction("instance-action", "spot/"),
Expand Down Expand Up @@ -200,10 +208,27 @@ public AmazonInfo autoBuild(String namespace) {
int numOfRetries = config.getNumRetries();
while (numOfRetries-- > 0) {
try {
if (key == MetaDataKey.publicIpv4s) {
// macs should be read before publicIpv4s due to declaration order
String[] macs = result.metadata.get(MetaDataKey.macs.getName()).split("\n");
for (String mac : macs) {
URL url = key.getURL(null, mac);
String publicIpv4s = AmazonInfoUtils.readEc2MetadataUrl(key, url, config.getConnectTimeout(), config.getReadTimeout());

if (publicIpv4s != null) {
// only support registering the first found public IPv4 address
result.metadata.put(key.getName(), publicIpv4s.split("\n")[0]);
break;
}
}
break;
}

String mac = null;
if (key == MetaDataKey.vpcId) {
mac = result.metadata.get(MetaDataKey.mac.getName()); // mac should be read before vpcId due to declaration order
}

URL url = key.getURL(null, mac);
String value = AmazonInfoUtils.readEc2MetadataUrl(key, url, config.getConnectTimeout(), config.getReadTimeout());
if (value != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ private String getPrivateIpv4Addr() {
}
private String getPublicIpv4Addr() {
String publicIpv4Addr = amazonInfoHolder.get().get(MetaDataKey.publicIpv4);
if (publicIpv4Addr == null) {
// publicIpv4s is named as a plural to not conflict with the existing publicIpv4 key. In AmazonInfo.java,
// we filter out for the first found IPv4 address in any of the network interface IMDS keys. If it exists,
// the value for MetaDataKey.publicIpv4s will always be a string with at most 1 public IPv4 address.
publicIpv4Addr = amazonInfoHolder.get().get(MetaDataKey.publicIpv4s);
}
return publicIpv4Addr == null ? super.getIpAddress() : publicIpv4Addr;
}

Expand Down

0 comments on commit da18301

Please sign in to comment.