From 5e4a33c890521eeb50fa448ee24d1d809be13be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Mart=C3=ADnez?= Date: Wed, 25 Jul 2018 09:36:46 +0200 Subject: [PATCH] discovery/ec2: Maintain order of subnet_id label MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Martínez --- discovery/ec2/ec2.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/discovery/ec2/ec2.go b/discovery/ec2/ec2.go index c6fadd821..f382e2bea 100644 --- a/discovery/ec2/ec2.go +++ b/discovery/ec2/ec2.go @@ -257,13 +257,14 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) { if inst.VpcId != nil { labels[ec2LabelVPCID] = model.LabelValue(*inst.VpcId) + // Deduplicate VPC Subnet IDs maintaining the order of the network interfaces returned by EC2. + var subnets []string subnetsMap := make(map[string]struct{}) for _, eni := range inst.NetworkInterfaces { - subnetsMap[*eni.SubnetId] = struct{}{} - } - subnets := []string{} - for k := range subnetsMap { - subnets = append(subnets, k) + if _, ok := subnetsMap[*eni.SubnetId]; !ok { + subnetsMap[*eni.SubnetId] = struct{}{} + subnets = append(subnets, *eni.SubnetId) + } } labels[ec2LabelSubnetID] = model.LabelValue( subnetSeparator +