discovery: ec2: expose IPv6 as label (#7086)

* discovery: ec2: expose IPv6 as label

Signed-off-by: Muhammad Falak R Wani <falakreyaz@gmail.com>
This commit is contained in:
Muhammad Falak R Wani 2020-11-20 22:43:52 +05:30 committed by GitHub
parent 5f103aaecc
commit 587fd740be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 9 deletions

View file

@ -42,21 +42,22 @@ const (
ec2LabelAMI = ec2Label + "ami" ec2LabelAMI = ec2Label + "ami"
ec2LabelAZ = ec2Label + "availability_zone" ec2LabelAZ = ec2Label + "availability_zone"
ec2LabelArch = ec2Label + "architecture" ec2LabelArch = ec2Label + "architecture"
ec2LabelIPv6Addresses = ec2Label + "ipv6_addresses"
ec2LabelInstanceID = ec2Label + "instance_id" ec2LabelInstanceID = ec2Label + "instance_id"
ec2LabelInstanceLifecycle = ec2Label + "instance_lifecycle"
ec2LabelInstanceState = ec2Label + "instance_state" ec2LabelInstanceState = ec2Label + "instance_state"
ec2LabelInstanceType = ec2Label + "instance_type" ec2LabelInstanceType = ec2Label + "instance_type"
ec2LabelInstanceLifecycle = ec2Label + "instance_lifecycle"
ec2LabelOwnerID = ec2Label + "owner_id" ec2LabelOwnerID = ec2Label + "owner_id"
ec2LabelPlatform = ec2Label + "platform" ec2LabelPlatform = ec2Label + "platform"
ec2LabelPublicDNS = ec2Label + "public_dns_name" ec2LabelPrimarySubnetID = ec2Label + "primary_subnet_id"
ec2LabelPublicIP = ec2Label + "public_ip"
ec2LabelPrivateDNS = ec2Label + "private_dns_name" ec2LabelPrivateDNS = ec2Label + "private_dns_name"
ec2LabelPrivateIP = ec2Label + "private_ip" ec2LabelPrivateIP = ec2Label + "private_ip"
ec2LabelPrimarySubnetID = ec2Label + "primary_subnet_id" ec2LabelPublicDNS = ec2Label + "public_dns_name"
ec2LabelPublicIP = ec2Label + "public_ip"
ec2LabelSubnetID = ec2Label + "subnet_id" ec2LabelSubnetID = ec2Label + "subnet_id"
ec2LabelTag = ec2Label + "tag_" ec2LabelTag = ec2Label + "tag_"
ec2LabelVPCID = ec2Label + "vpc_id" ec2LabelVPCID = ec2Label + "vpc_id"
subnetSeparator = "," ec2LabelSeparator = ","
) )
// DefaultSDConfig is the default EC2 SD configuration. // 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[ec2LabelVPCID] = model.LabelValue(*inst.VpcId)
labels[ec2LabelPrimarySubnetID] = model.LabelValue(*inst.SubnetId) labels[ec2LabelPrimarySubnetID] = model.LabelValue(*inst.SubnetId)
// Deduplicate VPC Subnet IDs maintaining the order of the network interfaces returned by EC2.
var subnets []string var subnets []string
var ipv6addrs []string
subnetsMap := make(map[string]struct{}) subnetsMap := make(map[string]struct{})
for _, eni := range inst.NetworkInterfaces { for _, eni := range inst.NetworkInterfaces {
if eni.SubnetId == nil { if eni.SubnetId == nil {
continue continue
} }
// Deduplicate VPC Subnet IDs maintaining the order of the subnets returned by EC2.
if _, ok := subnetsMap[*eni.SubnetId]; !ok { if _, ok := subnetsMap[*eni.SubnetId]; !ok {
subnetsMap[*eni.SubnetId] = struct{}{} subnetsMap[*eni.SubnetId] = struct{}{}
subnets = append(subnets, *eni.SubnetId) subnets = append(subnets, *eni.SubnetId)
} }
for _, ipv6addr := range eni.Ipv6Addresses {
ipv6addrs = append(ipv6addrs, *ipv6addr.Ipv6Address)
}
} }
labels[ec2LabelSubnetID] = model.LabelValue( labels[ec2LabelSubnetID] = model.LabelValue(
subnetSeparator + ec2LabelSeparator +
strings.Join(subnets, subnetSeparator) + strings.Join(subnets, ec2LabelSeparator) +
subnetSeparator) ec2LabelSeparator)
if len(ipv6addrs) > 0 {
labels[ec2LabelIPv6Addresses] = model.LabelValue(
ec2LabelSeparator +
strings.Join(ipv6addrs, ec2LabelSeparator) +
ec2LabelSeparator)
}
} }
for _, t := range inst.Tags { for _, t := range inst.Tags {

View file

@ -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_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_state`: the state of the EC2 instance
* `__meta_ec2_instance_type`: the type 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_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_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 * `__meta_ec2_primary_subnet_id`: the subnet ID of the primary network interface, if available