2021-04-28 02:29:12 -07:00
|
|
|
// Copyright 2021 The Prometheus Authors
|
2015-09-21 11:49:19 -07:00
|
|
|
// Licensed 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.
|
|
|
|
|
2021-04-28 02:29:12 -07:00
|
|
|
package aws
|
2015-09-21 11:49:19 -07:00
|
|
|
|
|
|
|
import (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2022-06-03 04:47:14 -07:00
|
|
|
"errors"
|
2015-09-21 11:49:19 -07:00
|
|
|
"fmt"
|
2016-09-05 05:40:28 -07:00
|
|
|
"net"
|
2024-05-13 08:36:19 -07:00
|
|
|
"strconv"
|
2015-10-20 05:36:53 -07:00
|
|
|
"strings"
|
2015-09-21 11:49:19 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2021-01-26 03:27:30 -08:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-09-21 11:49:19 -07:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2017-07-03 23:40:55 -07:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
|
2017-10-25 06:15:39 -07:00
|
|
|
"github.com/aws/aws-sdk-go/aws/ec2metadata"
|
2016-11-03 07:54:27 -07:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
2019-03-25 16:01:12 -07:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
2021-07-23 00:42:03 -07:00
|
|
|
"github.com/go-kit/log/level"
|
2023-10-23 06:55:36 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-08-20 05:48:26 -07:00
|
|
|
"github.com/prometheus/common/config"
|
2015-09-21 11:49:19 -07:00
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
2020-08-20 05:48:26 -07:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2019-03-25 03:54:22 -07:00
|
|
|
"github.com/prometheus/prometheus/discovery/refresh"
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2015-09-21 11:49:19 -07:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-03-03 00:03:16 -08:00
|
|
|
ec2Label = model.MetaLabelPrefix + "ec2_"
|
2020-06-11 10:25:58 -07:00
|
|
|
ec2LabelAMI = ec2Label + "ami"
|
2020-03-03 00:03:16 -08:00
|
|
|
ec2LabelAZ = ec2Label + "availability_zone"
|
2021-07-23 00:42:03 -07:00
|
|
|
ec2LabelAZID = ec2Label + "availability_zone_id"
|
2020-03-28 13:41:37 -07:00
|
|
|
ec2LabelArch = ec2Label + "architecture"
|
2020-11-20 09:13:52 -08:00
|
|
|
ec2LabelIPv6Addresses = ec2Label + "ipv6_addresses"
|
2020-03-03 00:03:16 -08:00
|
|
|
ec2LabelInstanceID = ec2Label + "instance_id"
|
2020-11-20 09:13:52 -08:00
|
|
|
ec2LabelInstanceLifecycle = ec2Label + "instance_lifecycle"
|
2020-03-03 00:03:16 -08:00
|
|
|
ec2LabelInstanceState = ec2Label + "instance_state"
|
|
|
|
ec2LabelInstanceType = ec2Label + "instance_type"
|
|
|
|
ec2LabelOwnerID = ec2Label + "owner_id"
|
|
|
|
ec2LabelPlatform = ec2Label + "platform"
|
2020-11-20 09:13:52 -08:00
|
|
|
ec2LabelPrimarySubnetID = ec2Label + "primary_subnet_id"
|
2020-03-03 00:03:16 -08:00
|
|
|
ec2LabelPrivateDNS = ec2Label + "private_dns_name"
|
|
|
|
ec2LabelPrivateIP = ec2Label + "private_ip"
|
2020-11-20 09:13:52 -08:00
|
|
|
ec2LabelPublicDNS = ec2Label + "public_dns_name"
|
|
|
|
ec2LabelPublicIP = ec2Label + "public_ip"
|
2022-09-19 13:14:55 -07:00
|
|
|
ec2LabelRegion = ec2Label + "region"
|
2020-03-03 00:03:16 -08:00
|
|
|
ec2LabelSubnetID = ec2Label + "subnet_id"
|
|
|
|
ec2LabelTag = ec2Label + "tag_"
|
|
|
|
ec2LabelVPCID = ec2Label + "vpc_id"
|
2020-11-20 09:13:52 -08:00
|
|
|
ec2LabelSeparator = ","
|
2015-09-21 11:49:19 -07:00
|
|
|
)
|
|
|
|
|
2021-10-22 01:06:44 -07:00
|
|
|
// DefaultEC2SDConfig is the default EC2 SD configuration.
|
|
|
|
var DefaultEC2SDConfig = EC2SDConfig{
|
2022-11-22 07:09:14 -08:00
|
|
|
Port: 80,
|
|
|
|
RefreshInterval: model.Duration(60 * time.Second),
|
|
|
|
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
2021-10-22 01:06:44 -07:00
|
|
|
}
|
2016-10-19 02:20:00 -07:00
|
|
|
|
2020-08-20 05:48:26 -07:00
|
|
|
func init() {
|
2021-04-28 02:29:12 -07:00
|
|
|
discovery.RegisterConfig(&EC2SDConfig{})
|
2020-08-20 05:48:26 -07:00
|
|
|
}
|
|
|
|
|
2021-04-29 01:05:33 -07:00
|
|
|
// EC2Filter is the configuration for filtering EC2 instances.
|
2021-04-28 02:29:12 -07:00
|
|
|
type EC2Filter struct {
|
2018-03-30 23:51:11 -07:00
|
|
|
Name string `yaml:"name"`
|
|
|
|
Values []string `yaml:"values"`
|
|
|
|
}
|
|
|
|
|
2021-04-28 02:29:12 -07:00
|
|
|
// EC2SDConfig is the configuration for EC2 based service discovery.
|
|
|
|
type EC2SDConfig struct {
|
2020-08-20 05:48:26 -07:00
|
|
|
Endpoint string `yaml:"endpoint"`
|
|
|
|
Region string `yaml:"region"`
|
|
|
|
AccessKey string `yaml:"access_key,omitempty"`
|
|
|
|
SecretKey config.Secret `yaml:"secret_key,omitempty"`
|
|
|
|
Profile string `yaml:"profile,omitempty"`
|
|
|
|
RoleARN string `yaml:"role_arn,omitempty"`
|
|
|
|
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"`
|
|
|
|
Port int `yaml:"port"`
|
2021-04-28 02:29:12 -07:00
|
|
|
Filters []*EC2Filter `yaml:"filters"`
|
2022-11-22 07:09:14 -08:00
|
|
|
|
|
|
|
HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`
|
2020-08-20 05:48:26 -07:00
|
|
|
}
|
|
|
|
|
2024-01-23 07:53:55 -08:00
|
|
|
// NewDiscovererMetrics implements discovery.Config.
|
|
|
|
func (*EC2SDConfig) NewDiscovererMetrics(reg prometheus.Registerer, rmi discovery.RefreshMetricsInstantiator) discovery.DiscovererMetrics {
|
|
|
|
return &ec2Metrics{
|
|
|
|
refreshMetrics: rmi,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-28 02:29:12 -07:00
|
|
|
// Name returns the name of the EC2 Config.
|
|
|
|
func (*EC2SDConfig) Name() string { return "ec2" }
|
2020-08-20 05:48:26 -07:00
|
|
|
|
2021-04-28 02:29:12 -07:00
|
|
|
// NewDiscoverer returns a Discoverer for the EC2 Config.
|
|
|
|
func (c *EC2SDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Discoverer, error) {
|
2024-01-23 07:53:55 -08:00
|
|
|
return NewEC2Discovery(c, opts.Logger, opts.Metrics)
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
}
|
|
|
|
|
2021-04-28 02:29:12 -07:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface for the EC2 Config.
|
|
|
|
func (c *EC2SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*c = DefaultEC2SDConfig
|
|
|
|
type plain EC2SDConfig
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
err := unmarshal((*plain)(c))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.Region == "" {
|
|
|
|
sess, err := session.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
metadata := ec2metadata.New(sess)
|
|
|
|
region, err := metadata.Region()
|
|
|
|
if err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("EC2 SD configuration requires a region")
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
}
|
|
|
|
c.Region = region
|
|
|
|
}
|
2018-03-30 23:51:11 -07:00
|
|
|
for _, f := range c.Filters {
|
|
|
|
if len(f.Values) == 0 {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("EC2 SD configuration filter values cannot be empty")
|
2018-03-30 23:51:11 -07:00
|
|
|
}
|
|
|
|
}
|
2023-09-07 23:16:03 -07:00
|
|
|
return c.HTTPClientConfig.Validate()
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 01:05:33 -07:00
|
|
|
// EC2Discovery periodically performs EC2-SD requests. It implements
|
2018-01-08 15:59:18 -08:00
|
|
|
// the Discoverer interface.
|
2021-04-28 02:29:12 -07:00
|
|
|
type EC2Discovery struct {
|
2019-03-25 03:54:22 -07:00
|
|
|
*refresh.Discovery
|
2021-07-23 00:42:03 -07:00
|
|
|
logger log.Logger
|
|
|
|
cfg *EC2SDConfig
|
|
|
|
ec2 *ec2.EC2
|
|
|
|
|
|
|
|
// azToAZID maps this account's availability zones to their underlying AZ
|
|
|
|
// ID, e.g. eu-west-2a -> euw2-az2. Refreshes are performed sequentially, so
|
|
|
|
// no locking is required.
|
|
|
|
azToAZID map[string]string
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
|
|
|
|
2021-04-28 02:29:12 -07:00
|
|
|
// NewEC2Discovery returns a new EC2Discovery which periodically refreshes its targets.
|
2024-01-23 07:53:55 -08:00
|
|
|
func NewEC2Discovery(conf *EC2SDConfig, logger log.Logger, metrics discovery.DiscovererMetrics) (*EC2Discovery, error) {
|
|
|
|
m, ok := metrics.(*ec2Metrics)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid discovery metrics type")
|
|
|
|
}
|
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2021-04-28 02:29:12 -07:00
|
|
|
d := &EC2Discovery{
|
2021-07-23 00:42:03 -07:00
|
|
|
logger: logger,
|
|
|
|
cfg: conf,
|
2021-01-26 03:27:30 -08:00
|
|
|
}
|
|
|
|
d.Discovery = refresh.NewDiscovery(
|
2023-10-23 06:55:36 -07:00
|
|
|
refresh.Options{
|
2024-01-23 07:53:55 -08:00
|
|
|
Logger: logger,
|
|
|
|
Mech: "ec2",
|
|
|
|
Interval: time.Duration(d.cfg.RefreshInterval),
|
|
|
|
RefreshF: d.refresh,
|
|
|
|
MetricsInstantiator: m.refreshMetrics,
|
2023-10-23 06:55:36 -07:00
|
|
|
},
|
2021-01-26 03:27:30 -08:00
|
|
|
)
|
2024-01-23 07:53:55 -08:00
|
|
|
return d, nil
|
2021-01-26 03:27:30 -08:00
|
|
|
}
|
|
|
|
|
2023-04-12 04:05:41 -07:00
|
|
|
func (d *EC2Discovery) ec2Client(context.Context) (*ec2.EC2, error) {
|
2021-01-26 03:27:30 -08:00
|
|
|
if d.ec2 != nil {
|
|
|
|
return d.ec2, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
creds := credentials.NewStaticCredentials(d.cfg.AccessKey, string(d.cfg.SecretKey), "")
|
|
|
|
if d.cfg.AccessKey == "" && d.cfg.SecretKey == "" {
|
|
|
|
creds = nil
|
|
|
|
}
|
2020-12-22 07:47:44 -08:00
|
|
|
|
2022-11-22 07:09:14 -08:00
|
|
|
client, err := config.NewClientFromConfig(d.cfg.HTTPClientConfig, "ec2_sd")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-22 07:47:44 -08:00
|
|
|
sess, err := session.NewSessionWithOptions(session.Options{
|
|
|
|
Config: aws.Config{
|
2021-01-26 03:27:30 -08:00
|
|
|
Endpoint: &d.cfg.Endpoint,
|
|
|
|
Region: &d.cfg.Region,
|
2015-09-21 11:49:19 -07:00
|
|
|
Credentials: creds,
|
2022-11-22 07:09:14 -08:00
|
|
|
HTTPClient: client,
|
2015-09-21 11:49:19 -07:00
|
|
|
},
|
2021-01-26 03:27:30 -08:00
|
|
|
Profile: d.cfg.Profile,
|
2020-12-22 07:47:44 -08:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-06-03 04:47:14 -07:00
|
|
|
return nil, fmt.Errorf("could not create aws session: %w", err)
|
2020-12-22 07:47:44 -08:00
|
|
|
}
|
|
|
|
|
2021-01-26 03:27:30 -08:00
|
|
|
if d.cfg.RoleARN != "" {
|
|
|
|
creds := stscreds.NewCredentials(sess, d.cfg.RoleARN)
|
|
|
|
d.ec2 = ec2.New(sess, &aws.Config{Credentials: creds})
|
2020-12-22 07:47:44 -08:00
|
|
|
} else {
|
2021-01-26 03:27:30 -08:00
|
|
|
d.ec2 = ec2.New(sess)
|
2020-12-22 07:47:44 -08:00
|
|
|
}
|
|
|
|
|
2021-01-26 03:27:30 -08:00
|
|
|
return d.ec2, nil
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
|
|
|
|
2021-08-12 09:38:06 -07:00
|
|
|
func (d *EC2Discovery) refreshAZIDs(ctx context.Context) error {
|
2021-07-23 00:42:03 -07:00
|
|
|
azs, err := d.ec2.DescribeAvailabilityZonesWithContext(ctx, &ec2.DescribeAvailabilityZonesInput{})
|
|
|
|
if err != nil {
|
2021-08-12 09:38:06 -07:00
|
|
|
return err
|
2021-07-23 00:42:03 -07:00
|
|
|
}
|
|
|
|
d.azToAZID = make(map[string]string, len(azs.AvailabilityZones))
|
|
|
|
for _, az := range azs.AvailabilityZones {
|
|
|
|
d.azToAZID[*az.ZoneName] = *az.ZoneId
|
|
|
|
}
|
2021-08-12 09:38:06 -07:00
|
|
|
return nil
|
2021-07-23 00:42:03 -07:00
|
|
|
}
|
|
|
|
|
2021-04-28 02:29:12 -07:00
|
|
|
func (d *EC2Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
|
2021-07-23 00:42:03 -07:00
|
|
|
ec2Client, err := d.ec2Client(ctx)
|
2021-01-26 03:27:30 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tg := &targetgroup.Group{
|
2021-01-26 03:27:30 -08:00
|
|
|
Source: d.cfg.Region,
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
2018-03-30 23:51:11 -07:00
|
|
|
|
|
|
|
var filters []*ec2.Filter
|
2021-01-26 03:27:30 -08:00
|
|
|
for _, f := range d.cfg.Filters {
|
2018-03-30 23:51:11 -07:00
|
|
|
filters = append(filters, &ec2.Filter{
|
|
|
|
Name: aws.String(f.Name),
|
|
|
|
Values: aws.StringSlice(f.Values),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:38:06 -07:00
|
|
|
// Only refresh the AZ ID map if we have never been able to build one.
|
|
|
|
// Prometheus requires a reload if AWS adds a new AZ to the region.
|
|
|
|
if d.azToAZID == nil {
|
|
|
|
if err := d.refreshAZIDs(ctx); err != nil {
|
|
|
|
level.Debug(d.logger).Log(
|
|
|
|
"msg", "Unable to describe availability zones",
|
|
|
|
"err", err)
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 23:51:11 -07:00
|
|
|
|
2021-08-12 09:38:06 -07:00
|
|
|
input := &ec2.DescribeInstancesInput{Filters: filters}
|
2021-01-26 03:27:30 -08:00
|
|
|
if err := ec2Client.DescribeInstancesPagesWithContext(ctx, input, func(p *ec2.DescribeInstancesOutput, lastPage bool) bool {
|
2015-09-21 11:49:19 -07:00
|
|
|
for _, r := range p.Reservations {
|
|
|
|
for _, inst := range r.Instances {
|
|
|
|
if inst.PrivateIpAddress == nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-07-23 00:42:03 -07:00
|
|
|
|
2015-09-21 11:49:19 -07:00
|
|
|
labels := model.LabelSet{
|
|
|
|
ec2LabelInstanceID: model.LabelValue(*inst.InstanceId),
|
2022-09-19 13:14:55 -07:00
|
|
|
ec2LabelRegion: model.LabelValue(d.cfg.Region),
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
2018-10-02 03:48:31 -07:00
|
|
|
|
|
|
|
if r.OwnerId != nil {
|
|
|
|
labels[ec2LabelOwnerID] = model.LabelValue(*r.OwnerId)
|
|
|
|
}
|
|
|
|
|
2015-09-21 11:49:19 -07:00
|
|
|
labels[ec2LabelPrivateIP] = model.LabelValue(*inst.PrivateIpAddress)
|
2018-11-30 03:11:06 -08:00
|
|
|
if inst.PrivateDnsName != nil {
|
|
|
|
labels[ec2LabelPrivateDNS] = model.LabelValue(*inst.PrivateDnsName)
|
|
|
|
}
|
2024-05-13 08:36:19 -07:00
|
|
|
addr := net.JoinHostPort(*inst.PrivateIpAddress, strconv.Itoa(d.cfg.Port))
|
2015-09-21 11:49:19 -07:00
|
|
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
2015-10-20 05:36:53 -07:00
|
|
|
|
2018-11-06 06:39:48 -08:00
|
|
|
if inst.Platform != nil {
|
|
|
|
labels[ec2LabelPlatform] = model.LabelValue(*inst.Platform)
|
|
|
|
}
|
|
|
|
|
2015-10-20 05:36:53 -07:00
|
|
|
if inst.PublicIpAddress != nil {
|
|
|
|
labels[ec2LabelPublicIP] = model.LabelValue(*inst.PublicIpAddress)
|
|
|
|
labels[ec2LabelPublicDNS] = model.LabelValue(*inst.PublicDnsName)
|
|
|
|
}
|
2020-06-11 10:25:58 -07:00
|
|
|
labels[ec2LabelAMI] = model.LabelValue(*inst.ImageId)
|
2015-10-20 05:36:53 -07:00
|
|
|
labels[ec2LabelAZ] = model.LabelValue(*inst.Placement.AvailabilityZone)
|
2021-08-12 09:38:06 -07:00
|
|
|
azID, ok := d.azToAZID[*inst.Placement.AvailabilityZone]
|
|
|
|
if !ok && d.azToAZID != nil {
|
|
|
|
level.Debug(d.logger).Log(
|
|
|
|
"msg", "Availability zone ID not found",
|
|
|
|
"az", *inst.Placement.AvailabilityZone)
|
|
|
|
}
|
2021-07-23 00:42:03 -07:00
|
|
|
labels[ec2LabelAZID] = model.LabelValue(azID)
|
2016-09-22 06:01:23 -07:00
|
|
|
labels[ec2LabelInstanceState] = model.LabelValue(*inst.State.Name)
|
2016-10-21 03:13:47 -07:00
|
|
|
labels[ec2LabelInstanceType] = model.LabelValue(*inst.InstanceType)
|
2015-10-20 05:36:53 -07:00
|
|
|
|
2020-03-03 00:03:16 -08:00
|
|
|
if inst.InstanceLifecycle != nil {
|
|
|
|
labels[ec2LabelInstanceLifecycle] = model.LabelValue(*inst.InstanceLifecycle)
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:41:37 -07:00
|
|
|
if inst.Architecture != nil {
|
|
|
|
labels[ec2LabelArch] = model.LabelValue(*inst.Architecture)
|
|
|
|
}
|
|
|
|
|
2015-10-20 05:36:53 -07:00
|
|
|
if inst.VpcId != nil {
|
|
|
|
labels[ec2LabelVPCID] = model.LabelValue(*inst.VpcId)
|
2018-07-25 00:38:14 -07:00
|
|
|
labels[ec2LabelPrimarySubnetID] = model.LabelValue(*inst.SubnetId)
|
2015-10-20 05:36:53 -07:00
|
|
|
|
2018-07-25 00:36:46 -07:00
|
|
|
var subnets []string
|
2020-11-20 09:13:52 -08:00
|
|
|
var ipv6addrs []string
|
2015-10-20 05:36:53 -07:00
|
|
|
subnetsMap := make(map[string]struct{})
|
|
|
|
for _, eni := range inst.NetworkInterfaces {
|
2018-08-07 00:35:22 -07:00
|
|
|
if eni.SubnetId == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-20 09:13:52 -08:00
|
|
|
// Deduplicate VPC Subnet IDs maintaining the order of the subnets returned by EC2.
|
2018-07-25 00:36:46 -07:00
|
|
|
if _, ok := subnetsMap[*eni.SubnetId]; !ok {
|
|
|
|
subnetsMap[*eni.SubnetId] = struct{}{}
|
|
|
|
subnets = append(subnets, *eni.SubnetId)
|
|
|
|
}
|
2020-11-20 09:13:52 -08:00
|
|
|
|
|
|
|
for _, ipv6addr := range eni.Ipv6Addresses {
|
|
|
|
ipv6addrs = append(ipv6addrs, *ipv6addr.Ipv6Address)
|
|
|
|
}
|
2015-10-20 05:36:53 -07:00
|
|
|
}
|
|
|
|
labels[ec2LabelSubnetID] = model.LabelValue(
|
2020-11-20 09:13:52 -08:00
|
|
|
ec2LabelSeparator +
|
|
|
|
strings.Join(subnets, ec2LabelSeparator) +
|
|
|
|
ec2LabelSeparator)
|
|
|
|
if len(ipv6addrs) > 0 {
|
|
|
|
labels[ec2LabelIPv6Addresses] = model.LabelValue(
|
|
|
|
ec2LabelSeparator +
|
|
|
|
strings.Join(ipv6addrs, ec2LabelSeparator) +
|
|
|
|
ec2LabelSeparator)
|
|
|
|
}
|
2015-10-20 05:36:53 -07:00
|
|
|
}
|
|
|
|
|
2015-09-21 11:49:19 -07:00
|
|
|
for _, t := range inst.Tags {
|
2017-11-03 04:59:04 -07:00
|
|
|
if t == nil || t.Key == nil || t.Value == nil {
|
|
|
|
continue
|
|
|
|
}
|
2015-09-21 11:49:19 -07:00
|
|
|
name := strutil.SanitizeLabelName(*t.Key)
|
|
|
|
labels[ec2LabelTag+model.LabelName(name)] = model.LabelValue(*t.Value)
|
|
|
|
}
|
|
|
|
tg.Targets = append(tg.Targets, labels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
2022-06-03 04:47:14 -07:00
|
|
|
var awsErr awserr.Error
|
|
|
|
if errors.As(err, &awsErr) && (awsErr.Code() == "AuthFailure" || awsErr.Code() == "UnauthorizedOperation") {
|
2021-01-26 03:27:30 -08:00
|
|
|
d.ec2 = nil
|
|
|
|
}
|
2022-06-03 04:47:14 -07:00
|
|
|
return nil, fmt.Errorf("could not describe instances: %w", err)
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
return []*targetgroup.Group{tg}, nil
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|