From 587fd740be6714b7f2c2edc43b29a612eb46d263 Mon Sep 17 00:00:00 2001 From: Muhammad Falak R Wani Date: Fri, 20 Nov 2020 22:43:52 +0530 Subject: [PATCH] discovery: ec2: expose IPv6 as label (#7086) * discovery: ec2: expose IPv6 as label Signed-off-by: Muhammad Falak R Wani --- discovery/ec2/ec2.go | 30 ++++++++++++++++++++--------- docs/configuration/configuration.md | 1 + 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/discovery/ec2/ec2.go b/discovery/ec2/ec2.go index f92da107c2..64ade7a9d9 100644 --- a/discovery/ec2/ec2.go +++ b/discovery/ec2/ec2.go @@ -42,21 +42,22 @@ const ( ec2LabelAMI = ec2Label + "ami" ec2LabelAZ = ec2Label + "availability_zone" ec2LabelArch = ec2Label + "architecture" + ec2LabelIPv6Addresses = ec2Label + "ipv6_addresses" ec2LabelInstanceID = ec2Label + "instance_id" + ec2LabelInstanceLifecycle = ec2Label + "instance_lifecycle" ec2LabelInstanceState = ec2Label + "instance_state" ec2LabelInstanceType = ec2Label + "instance_type" - ec2LabelInstanceLifecycle = ec2Label + "instance_lifecycle" ec2LabelOwnerID = ec2Label + "owner_id" ec2LabelPlatform = ec2Label + "platform" - ec2LabelPublicDNS = ec2Label + "public_dns_name" - ec2LabelPublicIP = ec2Label + "public_ip" + ec2LabelPrimarySubnetID = ec2Label + "primary_subnet_id" ec2LabelPrivateDNS = ec2Label + "private_dns_name" ec2LabelPrivateIP = ec2Label + "private_ip" - ec2LabelPrimarySubnetID = ec2Label + "primary_subnet_id" + ec2LabelPublicDNS = ec2Label + "public_dns_name" + ec2LabelPublicIP = ec2Label + "public_ip" ec2LabelSubnetID = ec2Label + "subnet_id" ec2LabelTag = ec2Label + "tag_" ec2LabelVPCID = ec2Label + "vpc_id" - subnetSeparator = "," + ec2LabelSeparator = "," ) // DefaultSDConfig is the default EC2 SD configuration. @@ -243,22 +244,33 @@ func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) { labels[ec2LabelVPCID] = model.LabelValue(*inst.VpcId) labels[ec2LabelPrimarySubnetID] = model.LabelValue(*inst.SubnetId) - // Deduplicate VPC Subnet IDs maintaining the order of the network interfaces returned by EC2. var subnets []string + var ipv6addrs []string subnetsMap := make(map[string]struct{}) for _, eni := range inst.NetworkInterfaces { if eni.SubnetId == nil { continue } + // Deduplicate VPC Subnet IDs maintaining the order of the subnets returned by EC2. if _, ok := subnetsMap[*eni.SubnetId]; !ok { subnetsMap[*eni.SubnetId] = struct{}{} subnets = append(subnets, *eni.SubnetId) } + + for _, ipv6addr := range eni.Ipv6Addresses { + ipv6addrs = append(ipv6addrs, *ipv6addr.Ipv6Address) + } } labels[ec2LabelSubnetID] = model.LabelValue( - subnetSeparator + - strings.Join(subnets, subnetSeparator) + - subnetSeparator) + ec2LabelSeparator + + strings.Join(subnets, ec2LabelSeparator) + + ec2LabelSeparator) + if len(ipv6addrs) > 0 { + labels[ec2LabelIPv6Addresses] = model.LabelValue( + ec2LabelSeparator + + strings.Join(ipv6addrs, ec2LabelSeparator) + + ec2LabelSeparator) + } } for _, t := range inst.Tags { diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index b9f35c4849..d80643e6d7 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -662,6 +662,7 @@ The following meta labels are available on targets during [relabeling](#relabel_ * `__meta_ec2_instance_lifecycle`: the lifecycle of the EC2 instance, set only for 'spot' or 'scheduled' instances, absent otherwise * `__meta_ec2_instance_state`: the state of the EC2 instance * `__meta_ec2_instance_type`: the type of the EC2 instance +* `__meta_ec2_ipv6_addresses`: comma seperated list of IPv6 addresses for the all the network interfaces of the instance, if available * `__meta_ec2_owner_id`: the ID of the AWS account that owns the EC2 instance * `__meta_ec2_platform`: the Operating System platform, set to 'windows' on Windows servers, absent otherwise * `__meta_ec2_primary_subnet_id`: the subnet ID of the primary network interface, if available