private LoadBalancerDescription checkForMatchingTag( List<LoadBalancerDescription> descriptions, String vpcID, String type) throws TooManyELBException { List<LoadBalancerDescription> found = new LinkedList<LoadBalancerDescription>(); for(LoadBalancerDescription desc : descriptions) { String loadBalancerName = desc.getLoadBalancerName(); logger.info(String.format("Checking LB for tag %s:%s, ELB name is %s", AwsFacade.TYPE_TAG, type, loadBalancerName)); List<Tag> tags = elbClient.getTagsFor(loadBalancerName); if (containsCorrectTag(tags, type)) { logger.info("LB matched " + loadBalancerName); found.add(desc); } } if (found.size()==1) { return found.get(0); } throw new TooManyELBException(found.size(), String.format("Found too many elbs for vpc (%s) that matched tag %s", vpcID, AwsFacade.TYPE_TAG)); }
@Test public void ShouldUseTagIfMoreThanOneELB() throws TooManyELBException { String typeTag = "expectedType"; List<Tag> lb1Tags = new LinkedList<>(); lb1Tags.add(new Tag().withKey(AwsFacade.TYPE_TAG).withValue("someNonMatchingTag")); List<Tag> lb2Tags = new LinkedList<>(); lb2Tags.add(new Tag().withKey(AwsFacade.TYPE_TAG).withValue(typeTag)); List<LoadBalancerDescription> lbs = new LinkedList<>(); lbs.add(new LoadBalancerDescription().withLoadBalancerName("lb1Name").withVPCId("vpcId")); lbs.add(new LoadBalancerDescription().withLoadBalancerName("lb2Name").withVPCId("vpcId")); Vpc vpc = new Vpc().withVpcId("vpcId"); EasyMock.expect(vpcRepository.getCopyOfVpc(projAndEnv)).andReturn(vpc); EasyMock.expect(elbClient.describeLoadBalancers()).andReturn(lbs); EasyMock.expect(elbClient.getTagsFor("lb1Name")).andReturn(lb1Tags); EasyMock.expect(elbClient.getTagsFor("lb2Name")).andReturn(lb2Tags); replayAll(); LoadBalancerDescription result = elbRepository.findELBFor(projAndEnv, typeTag); verifyAll(); assertEquals("lb2Name", result.getLoadBalancerName()); }
@Test public void ShouldThrowIfMoreThanOneELBAndNoMatchingTags() { List<Tag> tags = new LinkedList<>(); tags.add(new Tag().withKey("someOtherTag").withValue("someOtherValue")); List<LoadBalancerDescription> lbs = new LinkedList<>(); lbs.add(new LoadBalancerDescription().withLoadBalancerName("lb1Name").withVPCId("vpcId")); lbs.add(new LoadBalancerDescription().withLoadBalancerName("lb2Name").withVPCId("vpcId")); Vpc vpc = new Vpc().withVpcId("vpcId"); EasyMock.expect(vpcRepository.getCopyOfVpc(projAndEnv)).andReturn(vpc); EasyMock.expect(elbClient.describeLoadBalancers()).andReturn(lbs); EasyMock.expect(elbClient.getTagsFor("lb1Name")).andReturn(new LinkedList<>()); EasyMock.expect(elbClient.getTagsFor("lb2Name")).andReturn(tags); replayAll(); try { elbRepository.findELBFor(projAndEnv,"notMatchingAnLB"); fail("should have thrown"); } catch(TooManyELBException expectedException) { // no op } verifyAll(); }
private Map<String, List<Tag>> getElbTags(AmazonElasticLoadBalancingClient elbClient, List<String> elbNames) { if (isEmpty(elbNames)) { return emptyMap(); } else { final Map<String, List<Tag>> result = newHashMapWithExpectedSize(elbNames.size()); // http://docs.aws.amazon.com/elasticloadbalancing/2012-06-01/APIReference/API_DescribeTags.html // describeTags expects a maximum of 20 load balancer names per call for (List<String> elbNamePartition : partition(elbNames, ELB_NAMES_MAX_SIZE)) { elbClient.describeTags(new DescribeTagsRequest().withLoadBalancerNames(elbNamePartition)) .getTagDescriptions() .forEach(tagDescription -> result.put(tagDescription.getLoadBalancerName(), tagDescription.getTags())); } return result; } }
private boolean hasKubernetesTag(List<Tag> elbTags) { for (final Tag tag : elbTags) { if (StringUtils.equals(tag.getValue(), "owned") && startsWith(tag.getKey(), "kubernetes.io/cluster/")) { return true; } } return false; }
@Override public void execute(Context context) throws Exception { CreateLoadBalancerRequest request = new CreateLoadBalancerRequest() .withLoadBalancerName(resource.name) .withScheme(resource.scheme.orElse(null)) .withTags(new Tag().withKey("cloud-manager:env").withValue(context.env.name)); if (resource.subnet != null) { request.withSecurityGroups(resource.securityGroup.remoteSecurityGroup.getGroupId()) .withSubnets(resource.subnet.remoteSubnets.stream().map(Subnet::getSubnetId).collect(Collectors.toList())); } else { List<String> zones = AWS.ec2.availabilityZones(); request.withAvailabilityZones(zones.get(0)); } if (resource.listenHTTP) { request.getListeners().add(new Listener("HTTP", 80, 80)); } if (resource.listenHTTPS) { String certARN = resource.amazonCertARN != null ? resource.amazonCertARN : resource.cert.remoteCert.getServerCertificateMetadata().getArn(); request.getListeners().add(new Listener() .withProtocol("HTTPS") .withLoadBalancerPort(443) .withInstanceProtocol("HTTP") .withInstancePort(80) .withSSLCertificateId(certARN)); } resource.remoteELB = AWS.elb.createELB(request); configureELB(context.env.region); configureHealthCheck(); context.output(String.format("elb/%s/DNS", resource.id), resource.remoteELB.getDNSName()); }
public List<Tag> getTagsFor(String loadBalancerName) { DescribeTagsRequest describeTagsRequest = new DescribeTagsRequest().withLoadBalancerNames(loadBalancerName); DescribeTagsResult result = elbClient.describeTags(describeTagsRequest); List<TagDescription> descriptions = result.getTagDescriptions(); logger.info(String.format("Fetching %s tags for LB %s ", descriptions.size(), loadBalancerName)); return descriptions.get(0).getTags(); }
private boolean containsCorrectTag(List<Tag> tags, String type) { for(Tag tag : tags) { if (tag.getKey().equals(AwsFacade.TYPE_TAG)) { return tag.getValue().equals(type); } } return false; }
@Before public void setUp() throws Exception { this.violationSinkMock = mock(ViolationSink.class); this.clientProviderMock = mock(ClientProvider.class); this.accountIdSupplierMock = mock(AccountIdSupplier.class); this.jobsPropertiesMock = mock(JobsProperties.class); this.portsChecker = mock(PortsChecker.class); this.securityGroupsChecker = mock(SecurityGroupsChecker.class); this.mockAwsELBClient = mock(AmazonElasticLoadBalancingClient.class); this.mockAwsApplications = mock(AwsApplications.class); this.mockViolationService = mock(ViolationService.class); this.fetchTaupageYamlMock = mock(FetchTaupageYaml.class); this.mockAmiDetailsProvider = mock(AmiDetailsProvider.class); this.mockEC2InstanceProvider = mock(EC2InstanceProvider.class); final Listener listener = new Listener("HTTPS", 80, 80); final ListenerDescription listenerDescription = new ListenerDescription(); listenerDescription.setListener(listener); final ArrayList<LoadBalancerDescription> elbs = newArrayList(); final ArrayList<TagDescription> tagDescriptions = newArrayList(); final LoadBalancerDescription publicELB = new LoadBalancerDescription(); publicELB.setScheme("internet-facing"); publicELB.setListenerDescriptions(newArrayList(listenerDescription)); publicELB.setCanonicalHostedZoneName("test.com"); publicELB.setInstances(asList(new Instance("i1"), new Instance("i2"))); publicELB.setLoadBalancerName("publicELB"); elbs.add(publicELB); tagDescriptions.add( new TagDescription() .withLoadBalancerName("publicELB") .withTags(newArrayList( new Tag().withKey("someTag").withValue("someValue")))); final LoadBalancerDescription privateELB = new LoadBalancerDescription(); privateELB.setScheme("internal"); privateELB.setCanonicalHostedZoneName("internal.org"); privateELB.setLoadBalancerName("privateELB"); elbs.add(privateELB); for (int i = 1; i <= 20; i++) { final String loadBalancerName = "kubeELB" + i; final LoadBalancerDescription kubeELB = new LoadBalancerDescription(); kubeELB.setScheme("internet-facing"); kubeELB.setCanonicalHostedZoneName("test" + i + ".com"); kubeELB.setLoadBalancerName(loadBalancerName); elbs.add(kubeELB); tagDescriptions.add( new TagDescription() .withLoadBalancerName(loadBalancerName) .withTags(newArrayList( new Tag().withKey("someTag").withValue("someValue"), new Tag().withKey("kubernetes.io/cluster/").withValue("owned")))); } mockDescribeELBResult = new DescribeLoadBalancersResult(); mockDescribeELBResult.setLoadBalancerDescriptions(elbs); mockDescribeTagsResult = new DescribeTagsResult(); mockDescribeTagsResult.setTagDescriptions(tagDescriptions); regions.add(REGION1); when(clientProviderMock.getClient(any(), any(String.class), any(Region.class))).thenReturn(mockAwsELBClient); when(mockEC2InstanceProvider.getById(anyString(), any(Region.class), anyString())) .thenReturn(Optional.of(new com.amazonaws.services.ec2.model.Instance().withInstanceId("foo").withImageId("bar"))); when(mockAmiDetailsProvider.getAmiDetails(anyString(), any(Region.class), anyString())) .thenReturn(ImmutableMap.of("ami_id", "bar")); }