Get OpenStack variables from env as fallback (#3293)

This change enables the OpenStack service discovery to read the
authentication parameters from the OS_* environment variables when the
identity endpoint URL is not defined in the Prometheus configuration
file.
This commit is contained in:
pasquier-s 2017-10-16 19:01:50 +02:00 committed by Brian Brazil
parent e948721a0b
commit 88e4815bb7

View file

@ -19,6 +19,7 @@ import (
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -52,15 +53,24 @@ type Discovery interface {
// NewDiscovery returns a new OpenStackDiscovery which periodically refreshes its targets. // NewDiscovery returns a new OpenStackDiscovery which periodically refreshes its targets.
func NewDiscovery(conf *config.OpenstackSDConfig, l log.Logger) (Discovery, error) { func NewDiscovery(conf *config.OpenstackSDConfig, l log.Logger) (Discovery, error) {
opts := gophercloud.AuthOptions{ var opts gophercloud.AuthOptions
IdentityEndpoint: conf.IdentityEndpoint, if conf.IdentityEndpoint == "" {
Username: conf.Username, var err error
UserID: conf.UserID, opts, err = openstack.AuthOptionsFromEnv()
Password: string(conf.Password), if err != nil {
TenantName: conf.ProjectName, return nil, err
TenantID: conf.ProjectID, }
DomainName: conf.DomainName, } else {
DomainID: conf.DomainID, opts = gophercloud.AuthOptions{
IdentityEndpoint: conf.IdentityEndpoint,
Username: conf.Username,
UserID: conf.UserID,
Password: string(conf.Password),
TenantName: conf.ProjectName,
TenantID: conf.ProjectID,
DomainName: conf.DomainName,
DomainID: conf.DomainID,
}
} }
switch conf.Role { switch conf.Role {
case config.OpenStackRoleHypervisor: case config.OpenStackRoleHypervisor: