Search in sources :

Example 6 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo in project eureka by Netflix.

the class BaseDiscoveryClientTester method createInstance.

protected static InstanceInfo createInstance(String appName, String vipAddress, String instanceHostName, String zone) {
    InstanceInfo.Builder instanceBuilder = InstanceInfo.Builder.newBuilder();
    instanceBuilder.setAppName(appName);
    instanceBuilder.setVIPAddress(vipAddress);
    instanceBuilder.setHostName(instanceHostName);
    instanceBuilder.setIPAddr("10.10.101.1");
    AmazonInfo amazonInfo = getAmazonInfo(zone, instanceHostName);
    instanceBuilder.setDataCenterInfo(amazonInfo);
    instanceBuilder.setMetadata(amazonInfo.getMetadata());
    instanceBuilder.setLeaseInfo(LeaseInfo.Builder.newBuilder().build());
    return instanceBuilder.build();
}
Also used : AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 7 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo in project eureka by Netflix.

the class ElasticNetworkInterfaceBinder method bind.

/**
     * Binds an ENI to the instance.
     *
     * The candidate ENI's are deduced in the same wa the EIP binder works: Via dns records or via service urls,
     * depending on configuration.
     *
     * It will try to attach the first ENI that is:
     *      Available
     *      For this subnet
     *      In the list of candidate ENI's
     *
     * @throws MalformedURLException
     */
public void bind() throws MalformedURLException {
    InstanceInfo myInfo = ApplicationInfoManager.getInstance().getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
    String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.availabilityZone);
    final List<String> ips = getCandidateIps();
    Ordering<NetworkInterface> ipsOrder = Ordering.natural().onResultOf(new Function<NetworkInterface, Integer>() {

        public Integer apply(NetworkInterface networkInterface) {
            return ips.indexOf(networkInterface.getPrivateIpAddress());
        }
    });
    AmazonEC2 ec2Service = getEC2Service();
    String subnetId = instanceData(myInstanceId, ec2Service).getSubnetId();
    DescribeNetworkInterfacesResult result = ec2Service.describeNetworkInterfaces(new DescribeNetworkInterfacesRequest().withFilters(new Filter("private-ip-address", ips)).withFilters(new Filter("status", Lists.newArrayList("available"))).withFilters(new Filter("subnet-id", Lists.newArrayList(subnetId))));
    if (result.getNetworkInterfaces().isEmpty()) {
        logger.info("No ip is free to be associated with this instance. Candidate ips are: {} for zone: ", ips, myZone);
    } else {
        NetworkInterface selected = ipsOrder.min(result.getNetworkInterfaces());
        ec2Service.attachNetworkInterface(new AttachNetworkInterfaceRequest().withNetworkInterfaceId(selected.getNetworkInterfaceId()).withDeviceIndex(1).withInstanceId(myInstanceId));
    }
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) InstanceInfo(com.netflix.appinfo.InstanceInfo) AmazonInfo(com.netflix.appinfo.AmazonInfo)

Example 8 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo in project eureka by Netflix.

the class ElasticNetworkInterfaceBinder method alreadyBound.

public boolean alreadyBound() throws MalformedURLException {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.instanceId);
    AmazonEC2 ec2Service = getEC2Service();
    List<InstanceNetworkInterface> instanceNetworkInterfaces = instanceData(myInstanceId, ec2Service).getNetworkInterfaces();
    List<String> candidateIPs = getCandidateIps();
    for (String ip : candidateIPs) {
        for (InstanceNetworkInterface ini : instanceNetworkInterfaces) {
            if (ip.equals(ini.getPrivateIpAddress())) {
                logger.info("My instance {} seems to be already associated with the ip {}", myInstanceId, ip);
                return true;
            }
        }
    }
    return false;
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 9 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo in project eureka by Netflix.

the class ElasticNetworkInterfaceBinder method getCandidateIps.

/**
     * Based on shouldUseDnsForFetchingServiceUrls configuration, either retrieves candidates from dns records or from
     * configuration properties.
     *
     *
     */
public List<String> getCandidateIps() throws MalformedURLException {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(AmazonInfo.MetaDataKey.availabilityZone);
    Collection<String> candidates = clientConfig.shouldUseDnsForFetchingServiceUrls() ? getIPsForZoneFromDNS(myZone) : getIPsForZoneFromConfig(myZone);
    if (candidates == null || candidates.size() == 0) {
        throw new RuntimeException("Could not get any ips from the pool for zone :" + myZone);
    }
    List<String> ips = Lists.newArrayList();
    for (String candidate : candidates) {
        String host = new URL(candidate).getHost();
        if (InetAddresses.isInetAddress(host)) {
            ips.add(host);
        } else {
            // ip-172-31-55-172.ec2.internal -> ip-172-31-55-172
            String firstPartOfHost = Splitter.on(".").splitToList(host).get(0);
            // ip-172-31-55-172 -> [172,31,55,172]
            List<String> noIpPrefix = Splitter.on("-").splitToList(firstPartOfHost).subList(1, 5);
            // [172,31,55,172] -> 172.31.55.172
            String ip = Joiner.on(".").join(noIpPrefix);
            if (InetAddresses.isInetAddress(ip)) {
                ips.add(ip);
            } else {
                throw new IllegalArgumentException("Illegal internal hostname " + host + " translated to '" + ip + "'");
            }
        }
    }
    return ips;
}
Also used : AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo) URL(java.net.URL)

Example 10 with AmazonInfo

use of com.netflix.appinfo.AmazonInfo in project eureka by Netflix.

the class ApplicationResource method addInstance.

/**
     * Registers information about a particular instance for an
     * {@link com.netflix.discovery.shared.Application}.
     *
     * @param info
     *            {@link InstanceInfo} information of the instance.
     * @param isReplication
     *            a header parameter containing information whether this is
     *            replicated from other nodes.
     */
@POST
@Consumes({ "application/json", "application/xml" })
public Response addInstance(InstanceInfo info, @HeaderParam(PeerEurekaNode.HEADER_REPLICATION) String isReplication) {
    logger.debug("Registering instance {} (replication={})", info.getId(), isReplication);
    // validate that the instanceinfo contains all the necessary required fields
    if (isBlank(info.getId())) {
        return Response.status(400).entity("Missing instanceId").build();
    } else if (isBlank(info.getHostName())) {
        return Response.status(400).entity("Missing hostname").build();
    } else if (isBlank(info.getAppName())) {
        return Response.status(400).entity("Missing appName").build();
    } else if (!appName.equals(info.getAppName())) {
        return Response.status(400).entity("Mismatched appName, expecting " + appName + " but was " + info.getAppName()).build();
    } else if (info.getDataCenterInfo() == null) {
        return Response.status(400).entity("Missing dataCenterInfo").build();
    } else if (info.getDataCenterInfo().getName() == null) {
        return Response.status(400).entity("Missing dataCenterInfo Name").build();
    }
    // handle cases where clients may be registering with bad DataCenterInfo with missing data
    DataCenterInfo dataCenterInfo = info.getDataCenterInfo();
    if (dataCenterInfo instanceof UniqueIdentifier) {
        String dataCenterInfoId = ((UniqueIdentifier) dataCenterInfo).getId();
        if (isBlank(dataCenterInfoId)) {
            boolean experimental = "true".equalsIgnoreCase(serverConfig.getExperimental("registration.validation.dataCenterInfoId"));
            if (experimental) {
                String entity = "DataCenterInfo of type " + dataCenterInfo.getClass() + " must contain a valid id";
                return Response.status(400).entity(entity).build();
            } else if (dataCenterInfo instanceof AmazonInfo) {
                AmazonInfo amazonInfo = (AmazonInfo) dataCenterInfo;
                String effectiveId = amazonInfo.get(AmazonInfo.MetaDataKey.instanceId);
                if (effectiveId == null) {
                    amazonInfo.getMetadata().put(AmazonInfo.MetaDataKey.instanceId.getName(), info.getId());
                }
            } else {
                logger.warn("Registering DataCenterInfo of type {} without an appropriate id", dataCenterInfo.getClass());
            }
        }
    }
    registry.register(info, "true".equals(isReplication));
    // 204 to be backwards compatible
    return Response.status(204).build();
}
Also used : UniqueIdentifier(com.netflix.appinfo.UniqueIdentifier) DataCenterInfo(com.netflix.appinfo.DataCenterInfo) AmazonInfo(com.netflix.appinfo.AmazonInfo) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

AmazonInfo (com.netflix.appinfo.AmazonInfo)30 InstanceInfo (com.netflix.appinfo.InstanceInfo)21 Test (org.junit.Test)7 DataCenterInfo (com.netflix.appinfo.DataCenterInfo)6 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)4 ApplicationInfoManager (com.netflix.appinfo.ApplicationInfoManager)2 Builder (com.netflix.appinfo.InstanceInfo.Builder)2 DiscoveryClient (com.netflix.discovery.DiscoveryClient)2 Address (com.amazonaws.services.ec2.model.Address)1 AssociateAddressRequest (com.amazonaws.services.ec2.model.AssociateAddressRequest)1 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)1 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)1 Ec2EurekaArchaius2InstanceConfig (com.netflix.appinfo.Ec2EurekaArchaius2InstanceConfig)1 EurekaInstanceConfig (com.netflix.appinfo.EurekaInstanceConfig)1 LeaseInfo (com.netflix.appinfo.LeaseInfo)1 MyDataCenterInstanceConfig (com.netflix.appinfo.MyDataCenterInstanceConfig)1 UniqueIdentifier (com.netflix.appinfo.UniqueIdentifier)1 Archaius2VipAddressResolver (com.netflix.appinfo.providers.Archaius2VipAddressResolver)1 VipAddressResolver (com.netflix.appinfo.providers.VipAddressResolver)1 DefaultEurekaClientConfig (com.netflix.discovery.DefaultEurekaClientConfig)1