2015-09-21 11:49:19 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// 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.
|
|
|
|
|
2016-11-21 06:51:36 -08:00
|
|
|
package ec2
|
2015-09-21 11:49:19 -07:00
|
|
|
|
|
|
|
import (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2015-09-21 11:49:19 -07:00
|
|
|
"fmt"
|
2016-09-05 05:40:28 -07:00
|
|
|
"net"
|
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"
|
|
|
|
"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"
|
2017-08-11 11:45:52 -07:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2016-10-19 02:20:00 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-09-21 11:49:19 -07:00
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2018-01-11 07:10:25 -08:00
|
|
|
config_util "github.com/prometheus/common/config"
|
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 (
|
2016-09-22 06:01:23 -07:00
|
|
|
ec2Label = model.MetaLabelPrefix + "ec2_"
|
|
|
|
ec2LabelAZ = ec2Label + "availability_zone"
|
|
|
|
ec2LabelInstanceID = ec2Label + "instance_id"
|
|
|
|
ec2LabelInstanceState = ec2Label + "instance_state"
|
2016-10-21 03:13:47 -07:00
|
|
|
ec2LabelInstanceType = ec2Label + "instance_type"
|
2016-09-22 06:01:23 -07:00
|
|
|
ec2LabelPublicDNS = ec2Label + "public_dns_name"
|
|
|
|
ec2LabelPublicIP = ec2Label + "public_ip"
|
|
|
|
ec2LabelPrivateIP = ec2Label + "private_ip"
|
|
|
|
ec2LabelSubnetID = ec2Label + "subnet_id"
|
|
|
|
ec2LabelTag = ec2Label + "tag_"
|
|
|
|
ec2LabelVPCID = ec2Label + "vpc_id"
|
|
|
|
subnetSeparator = ","
|
2015-09-21 11:49:19 -07:00
|
|
|
)
|
|
|
|
|
2016-10-19 02:20:00 -07:00
|
|
|
var (
|
2016-10-26 02:03:35 -07:00
|
|
|
ec2SDRefreshFailuresCount = prometheus.NewCounter(
|
2016-10-19 02:20:00 -07:00
|
|
|
prometheus.CounterOpts{
|
2016-11-21 06:51:36 -08:00
|
|
|
Name: "prometheus_sd_ec2_refresh_failures_total",
|
|
|
|
Help: "The number of EC2-SD scrape failures.",
|
2016-10-19 02:20:00 -07:00
|
|
|
})
|
2016-10-26 02:03:35 -07:00
|
|
|
ec2SDRefreshDuration = prometheus.NewSummary(
|
2016-10-19 02:20:00 -07:00
|
|
|
prometheus.SummaryOpts{
|
2016-11-21 06:51:36 -08:00
|
|
|
Name: "prometheus_sd_ec2_refresh_duration_seconds",
|
|
|
|
Help: "The duration of a EC2-SD refresh in seconds.",
|
2016-10-19 02:20:00 -07:00
|
|
|
})
|
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
|
|
|
// DefaultSDConfig is the default EC2 SD configuration.
|
|
|
|
DefaultSDConfig = SDConfig{
|
|
|
|
Port: 80,
|
|
|
|
RefreshInterval: model.Duration(60 * time.Second),
|
|
|
|
}
|
2016-10-19 02:20:00 -07:00
|
|
|
)
|
|
|
|
|
2018-03-30 23:51:11 -07:00
|
|
|
// Filter is the configuration for filtering EC2 instances.
|
|
|
|
type Filter struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Values []string `yaml:"values"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// SDConfig is the configuration for EC2 based service discovery.
|
|
|
|
type SDConfig struct {
|
2018-07-18 02:48:14 -07:00
|
|
|
Endpoint string `yaml:"endpoint"`
|
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
|
|
|
Region string `yaml:"region"`
|
|
|
|
AccessKey string `yaml:"access_key,omitempty"`
|
|
|
|
SecretKey config_util.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"`
|
2018-03-30 23:51:11 -07:00
|
|
|
Filters []*Filter `yaml:"filters"`
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*c = DefaultSDConfig
|
|
|
|
type plain SDConfig
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("EC2 SD configuration requires a region")
|
|
|
|
}
|
|
|
|
c.Region = region
|
|
|
|
}
|
2018-03-30 23:51:11 -07:00
|
|
|
for _, f := range c.Filters {
|
|
|
|
if len(f.Values) == 0 {
|
|
|
|
return fmt.Errorf("EC2 SD configuration filter values cannot be empty")
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-19 02:20:00 -07:00
|
|
|
func init() {
|
2016-10-26 02:03:35 -07:00
|
|
|
prometheus.MustRegister(ec2SDRefreshFailuresCount)
|
|
|
|
prometheus.MustRegister(ec2SDRefreshDuration)
|
2016-10-19 02:20:00 -07:00
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
// Discovery periodically performs EC2-SD requests. It implements
|
2018-01-08 15:59:18 -08:00
|
|
|
// the Discoverer interface.
|
2017-03-16 16:29:47 -07:00
|
|
|
type Discovery struct {
|
2015-09-21 11:49:19 -07:00
|
|
|
aws *aws.Config
|
|
|
|
interval time.Duration
|
2016-11-03 07:54:27 -07:00
|
|
|
profile string
|
2017-07-03 23:40:55 -07:00
|
|
|
roleARN string
|
2015-09-21 11:49:19 -07:00
|
|
|
port int
|
2018-03-30 23:51:11 -07:00
|
|
|
filters []*Filter
|
2017-04-28 08:12:38 -07:00
|
|
|
logger log.Logger
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
|
|
|
|
2016-11-21 06:51:36 -08:00
|
|
|
// NewDiscovery returns a new EC2Discovery which periodically refreshes its targets.
|
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
|
|
|
func NewDiscovery(conf *SDConfig, logger log.Logger) *Discovery {
|
2017-05-29 04:46:23 -07:00
|
|
|
creds := credentials.NewStaticCredentials(conf.AccessKey, string(conf.SecretKey), "")
|
2015-09-21 11:49:19 -07:00
|
|
|
if conf.AccessKey == "" && conf.SecretKey == "" {
|
2016-11-03 07:54:27 -07:00
|
|
|
creds = nil
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
2017-08-11 11:45:52 -07:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.NewNopLogger()
|
|
|
|
}
|
2017-03-16 16:29:47 -07:00
|
|
|
return &Discovery{
|
2015-09-21 11:49:19 -07:00
|
|
|
aws: &aws.Config{
|
2018-07-18 02:48:14 -07:00
|
|
|
Endpoint: &conf.Endpoint,
|
2015-09-21 11:49:19 -07:00
|
|
|
Region: &conf.Region,
|
|
|
|
Credentials: creds,
|
|
|
|
},
|
2016-11-03 07:54:27 -07:00
|
|
|
profile: conf.Profile,
|
2017-07-03 23:40:55 -07:00
|
|
|
roleARN: conf.RoleARN,
|
2018-03-30 23:51:11 -07:00
|
|
|
filters: conf.Filters,
|
2015-09-21 11:49:19 -07:00
|
|
|
interval: time.Duration(conf.RefreshInterval),
|
|
|
|
port: conf.Port,
|
2017-04-28 08:12:38 -07:00
|
|
|
logger: logger,
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 15:59:18 -08:00
|
|
|
// Run implements the Discoverer interface.
|
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
|
|
|
func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
2017-03-16 16:29:47 -07:00
|
|
|
ticker := time.NewTicker(d.interval)
|
2015-09-21 11:49:19 -07:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
// Get an initial set right away.
|
2017-03-16 16:29:47 -07:00
|
|
|
tg, err := d.refresh()
|
2015-09-21 11:49:19 -07:00
|
|
|
if err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Error(d.logger).Log("msg", "Refresh failed", "err", err)
|
2015-09-21 11:49:19 -07:00
|
|
|
} else {
|
2016-11-18 01:55:29 -08:00
|
|
|
select {
|
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
|
|
|
case ch <- []*targetgroup.Group{tg}:
|
2016-11-18 01:55:29 -08:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2017-03-16 16:29:47 -07:00
|
|
|
tg, err := d.refresh()
|
2015-09-21 11:49:19 -07:00
|
|
|
if err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Error(d.logger).Log("msg", "Refresh failed", "err", err)
|
2016-11-18 01:55:29 -08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
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
|
|
|
case ch <- []*targetgroup.Group{tg}:
|
2016-11-18 01:55:29 -08:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
2016-02-18 08:26:27 -08:00
|
|
|
case <-ctx.Done():
|
2015-09-21 11:49:19 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
func (d *Discovery) refresh() (tg *targetgroup.Group, err error) {
|
2016-10-19 02:20:00 -07:00
|
|
|
t0 := time.Now()
|
|
|
|
defer func() {
|
2016-10-26 02:03:35 -07:00
|
|
|
ec2SDRefreshDuration.Observe(time.Since(t0).Seconds())
|
2016-10-19 02:20:00 -07:00
|
|
|
if err != nil {
|
2016-10-26 02:03:35 -07:00
|
|
|
ec2SDRefreshFailuresCount.Inc()
|
2016-10-19 02:20:00 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-11-03 07:54:27 -07:00
|
|
|
sess, err := session.NewSessionWithOptions(session.Options{
|
2017-03-16 16:29:47 -07:00
|
|
|
Config: *d.aws,
|
|
|
|
Profile: d.profile,
|
2016-11-03 07:54:27 -07:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not create aws session: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:40:55 -07:00
|
|
|
var ec2s *ec2.EC2
|
|
|
|
if d.roleARN != "" {
|
|
|
|
creds := stscreds.NewCredentials(sess, d.roleARN)
|
|
|
|
ec2s = ec2.New(sess, &aws.Config{Credentials: creds})
|
|
|
|
} else {
|
2018-03-20 05:34:54 -07:00
|
|
|
ec2s = ec2.New(sess)
|
2017-07-03 23:40:55 -07:00
|
|
|
}
|
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
|
|
|
tg = &targetgroup.Group{
|
2017-03-16 16:29:47 -07:00
|
|
|
Source: *d.aws.Region,
|
2015-09-21 11:49:19 -07:00
|
|
|
}
|
2018-03-30 23:51:11 -07:00
|
|
|
|
|
|
|
var filters []*ec2.Filter
|
|
|
|
for _, f := range d.filters {
|
|
|
|
filters = append(filters, &ec2.Filter{
|
|
|
|
Name: aws.String(f.Name),
|
|
|
|
Values: aws.StringSlice(f.Values),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
input := &ec2.DescribeInstancesInput{Filters: filters}
|
|
|
|
|
|
|
|
if err = ec2s.DescribeInstancesPages(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
|
|
|
|
}
|
|
|
|
labels := model.LabelSet{
|
|
|
|
ec2LabelInstanceID: model.LabelValue(*inst.InstanceId),
|
|
|
|
}
|
|
|
|
labels[ec2LabelPrivateIP] = model.LabelValue(*inst.PrivateIpAddress)
|
2017-03-16 16:29:47 -07:00
|
|
|
addr := net.JoinHostPort(*inst.PrivateIpAddress, fmt.Sprintf("%d", d.port))
|
2015-09-21 11:49:19 -07:00
|
|
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
2015-10-20 05:36:53 -07:00
|
|
|
|
|
|
|
if inst.PublicIpAddress != nil {
|
|
|
|
labels[ec2LabelPublicIP] = model.LabelValue(*inst.PublicIpAddress)
|
|
|
|
labels[ec2LabelPublicDNS] = model.LabelValue(*inst.PublicDnsName)
|
|
|
|
}
|
|
|
|
|
|
|
|
labels[ec2LabelAZ] = model.LabelValue(*inst.Placement.AvailabilityZone)
|
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
|
|
|
|
|
|
|
if inst.VpcId != nil {
|
|
|
|
labels[ec2LabelVPCID] = model.LabelValue(*inst.VpcId)
|
|
|
|
|
2018-07-25 00:36:46 -07:00
|
|
|
// Deduplicate VPC Subnet IDs maintaining the order of the network interfaces returned by EC2.
|
|
|
|
var subnets []string
|
2015-10-20 05:36:53 -07:00
|
|
|
subnetsMap := make(map[string]struct{})
|
|
|
|
for _, eni := range inst.NetworkInterfaces {
|
2018-07-25 00:36:46 -07:00
|
|
|
if _, ok := subnetsMap[*eni.SubnetId]; !ok {
|
|
|
|
subnetsMap[*eni.SubnetId] = struct{}{}
|
|
|
|
subnets = append(subnets, *eni.SubnetId)
|
|
|
|
}
|
2015-10-20 05:36:53 -07:00
|
|
|
}
|
|
|
|
labels[ec2LabelSubnetID] = model.LabelValue(
|
|
|
|
subnetSeparator +
|
|
|
|
strings.Join(subnets, subnetSeparator) +
|
|
|
|
subnetSeparator)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return nil, fmt.Errorf("could not describe instances: %s", err)
|
|
|
|
}
|
|
|
|
return tg, nil
|
|
|
|
}
|