Merge pull request #1401 from metamx/ec2-public-ip

flag to enable public IP in EC2 autoscaling
This commit is contained in:
Fangjin Yang 2015-05-28 20:21:32 -07:00
commit ac9057c00e
5 changed files with 84 additions and 2 deletions

View File

@ -22,6 +22,7 @@ import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
import com.amazonaws.services.ec2.model.DescribeInstancesResult; import com.amazonaws.services.ec2.model.DescribeInstancesResult;
import com.amazonaws.services.ec2.model.Filter; import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.Instance; import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.InstanceNetworkInterfaceSpecification;
import com.amazonaws.services.ec2.model.Placement; import com.amazonaws.services.ec2.model.Placement;
import com.amazonaws.services.ec2.model.Reservation; import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.ec2.model.RunInstancesRequest; import com.amazonaws.services.ec2.model.RunInstancesRequest;
@ -123,6 +124,13 @@ public class EC2AutoScaler implements AutoScaler<EC2EnvironmentConfig>
? null ? null
: workerConfig.getIamProfile().toIamInstanceProfileSpecification() : workerConfig.getIamProfile().toIamInstanceProfileSpecification()
) )
.withNetworkInterfaces(
workerConfig.getAssociatePublicIpAddress() == null
? null
: new InstanceNetworkInterfaceSpecification().withAssociatePublicIpAddress(
workerConfig.getAssociatePublicIpAddress()
)
)
.withUserData(userDataBase64) .withUserData(userDataBase64)
); );

View File

@ -34,6 +34,7 @@ public class EC2NodeData
private final String keyName; private final String keyName;
private final String subnetId; private final String subnetId;
private final EC2IamProfileData iamProfile; private final EC2IamProfileData iamProfile;
private final Boolean associatePublicIpAddress;
@JsonCreator @JsonCreator
public EC2NodeData( public EC2NodeData(
@ -44,7 +45,8 @@ public class EC2NodeData
@JsonProperty("securityGroupIds") List<String> securityGroupIds, @JsonProperty("securityGroupIds") List<String> securityGroupIds,
@JsonProperty("keyName") String keyName, @JsonProperty("keyName") String keyName,
@JsonProperty("subnetId") String subnetId, @JsonProperty("subnetId") String subnetId,
@JsonProperty("iamProfile") EC2IamProfileData iamProfile @JsonProperty("iamProfile") EC2IamProfileData iamProfile,
@JsonProperty("associatePublicIpAddress") Boolean associatePublicIpAddress
) )
{ {
this.amiId = amiId; this.amiId = amiId;
@ -55,6 +57,7 @@ public class EC2NodeData
this.keyName = keyName; this.keyName = keyName;
this.subnetId = subnetId; this.subnetId = subnetId;
this.iamProfile = iamProfile; this.iamProfile = iamProfile;
this.associatePublicIpAddress = associatePublicIpAddress;
} }
@JsonProperty @JsonProperty
@ -105,6 +108,12 @@ public class EC2NodeData
return iamProfile; return iamProfile;
} }
@JsonProperty
public Boolean getAssociatePublicIpAddress()
{
return associatePublicIpAddress;
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -92,7 +92,7 @@ public class EC2AutoScalerTest
1, 1,
new EC2EnvironmentConfig( new EC2EnvironmentConfig(
"us-east-1a", "us-east-1a",
new EC2NodeData(AMI_ID, INSTANCE_ID, 1, 1, Lists.<String>newArrayList(), "foo", "mySubnet", null), new EC2NodeData(AMI_ID, INSTANCE_ID, 1, 1, Lists.<String>newArrayList(), "foo", "mySubnet", null, null),
new GalaxyEC2UserData(new DefaultObjectMapper(), "env", "version", "type") new GalaxyEC2UserData(new DefaultObjectMapper(), "env", "version", "type")
), ),
amazonEC2Client, amazonEC2Client,

View File

@ -0,0 +1,64 @@
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.druid.indexing.overlord.autoscaling.ec2;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.druid.jackson.DefaultObjectMapper;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
public class EC2NodeDataTest
{
@Test
public void testSerde() throws Exception
{
final ObjectMapper objectMapper = new DefaultObjectMapper();
final String json = "{ \"amiId\" : \"abc123\", \"instanceType\" : \"k2.9xsmall\", \"minInstances\" : 1, \"maxInstances\" : 2,"
+ " \"securityGroupIds\" : [\"sg-abc321\"], \"keyName\" : \"opensesame\", \"subnetId\" : \"darknet2\","
+ " \"associatePublicIpAddress\" : true, \"iamProfile\" : { \"name\" : \"john\", \"arn\" : \"xxx:abc:1234/xyz\" } }";
EC2NodeData nodeData = objectMapper.readValue(json, EC2NodeData.class);
Assert.assertEquals("abc123", nodeData.getAmiId());
Assert.assertEquals("k2.9xsmall", nodeData.getInstanceType());
Assert.assertEquals(2, nodeData.getMaxInstances());
Assert.assertEquals(1, nodeData.getMinInstances());
Assert.assertEquals(Arrays.asList("sg-abc321"), nodeData.getSecurityGroupIds());
Assert.assertEquals("opensesame", nodeData.getKeyName());
Assert.assertEquals("darknet2", nodeData.getSubnetId());
Assert.assertEquals("john", nodeData.getIamProfile().getName());
Assert.assertEquals("xxx:abc:1234/xyz", nodeData.getIamProfile().getArn());
Assert.assertEquals(true, nodeData.getAssociatePublicIpAddress());
EC2NodeData nodeData2 = objectMapper.readValue("{}", EC2NodeData.class);
// default is not always false, null has to be a valid value
Assert.assertNull(nodeData2.getAssociatePublicIpAddress());
// round trip
Assert.assertEquals(
nodeData,
objectMapper.readValue(
objectMapper.writeValueAsBytes(nodeData),
EC2NodeData.class
)
);
}
}

View File

@ -56,6 +56,7 @@ public class WorkerBehaviorConfigTest
Arrays.asList("securityGroupIds"), Arrays.asList("securityGroupIds"),
"keyNames", "keyNames",
"subnetId", "subnetId",
null,
null null
), ),
new StringEC2UserData( new StringEC2UserData(