|
|
|
|
@ -17,14 +17,17 @@ import ( |
|
|
|
|
"context" |
|
|
|
|
"errors" |
|
|
|
|
"fmt" |
|
|
|
|
"net/http" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/go-kit/kit/log" |
|
|
|
|
"github.com/gophercloud/gophercloud" |
|
|
|
|
"github.com/gophercloud/gophercloud/openstack" |
|
|
|
|
"github.com/mwitkow/go-conntrack" |
|
|
|
|
"github.com/prometheus/client_golang/prometheus" |
|
|
|
|
config_util "github.com/prometheus/common/config" |
|
|
|
|
"github.com/prometheus/common/model" |
|
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
@ -48,18 +51,19 @@ var ( |
|
|
|
|
|
|
|
|
|
// SDConfig is the configuration for OpenStack based service discovery.
|
|
|
|
|
type SDConfig struct { |
|
|
|
|
IdentityEndpoint string `yaml:"identity_endpoint"` |
|
|
|
|
Username string `yaml:"username"` |
|
|
|
|
UserID string `yaml:"userid"` |
|
|
|
|
Password config_util.Secret `yaml:"password"` |
|
|
|
|
ProjectName string `yaml:"project_name"` |
|
|
|
|
ProjectID string `yaml:"project_id"` |
|
|
|
|
DomainName string `yaml:"domain_name"` |
|
|
|
|
DomainID string `yaml:"domain_id"` |
|
|
|
|
Role Role `yaml:"role"` |
|
|
|
|
Region string `yaml:"region"` |
|
|
|
|
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"` |
|
|
|
|
Port int `yaml:"port"` |
|
|
|
|
IdentityEndpoint string `yaml:"identity_endpoint"` |
|
|
|
|
Username string `yaml:"username"` |
|
|
|
|
UserID string `yaml:"userid"` |
|
|
|
|
Password config_util.Secret `yaml:"password"` |
|
|
|
|
ProjectName string `yaml:"project_name"` |
|
|
|
|
ProjectID string `yaml:"project_id"` |
|
|
|
|
DomainName string `yaml:"domain_name"` |
|
|
|
|
DomainID string `yaml:"domain_id"` |
|
|
|
|
Role Role `yaml:"role"` |
|
|
|
|
Region string `yaml:"region"` |
|
|
|
|
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"` |
|
|
|
|
Port int `yaml:"port"` |
|
|
|
|
TLSConfig config_util.TLSConfig `yaml:"tls_config,omitempty"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// OpenStackRole is role of the target in OpenStack.
|
|
|
|
|
@ -138,13 +142,32 @@ func NewDiscovery(conf *SDConfig, l log.Logger) (Discovery, error) { |
|
|
|
|
DomainID: conf.DomainID, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
client, err := openstack.NewClient(conf.IdentityEndpoint) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
tls, err := config_util.NewTLSConfig(&conf.TLSConfig) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} |
|
|
|
|
client.HTTPClient = http.Client{ |
|
|
|
|
Transport: &http.Transport{ |
|
|
|
|
IdleConnTimeout: 5 * time.Duration(conf.RefreshInterval), |
|
|
|
|
TLSClientConfig: tls, |
|
|
|
|
DialContext: conntrack.NewDialContextFunc( |
|
|
|
|
conntrack.DialWithTracing(), |
|
|
|
|
conntrack.DialWithName("openstack_sd"), |
|
|
|
|
), |
|
|
|
|
}, |
|
|
|
|
Timeout: 5 * time.Duration(conf.RefreshInterval), |
|
|
|
|
} |
|
|
|
|
switch conf.Role { |
|
|
|
|
case OpenStackRoleHypervisor: |
|
|
|
|
hypervisor := NewHypervisorDiscovery(&opts, |
|
|
|
|
hypervisor := NewHypervisorDiscovery(client, &opts, |
|
|
|
|
time.Duration(conf.RefreshInterval), conf.Port, conf.Region, l) |
|
|
|
|
return hypervisor, nil |
|
|
|
|
case OpenStackRoleInstance: |
|
|
|
|
instance := NewInstanceDiscovery(&opts, |
|
|
|
|
instance := NewInstanceDiscovery(client, &opts, |
|
|
|
|
time.Duration(conf.RefreshInterval), conf.Port, conf.Region, l) |
|
|
|
|
return instance, nil |
|
|
|
|
default: |
|
|
|
|
|