From 88e4815bb7f5118cb52f86bd9e5fdd680b2088da Mon Sep 17 00:00:00 2001 From: pasquier-s Date: Mon, 16 Oct 2017 19:01:50 +0200 Subject: [PATCH] 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. --- discovery/openstack/openstack.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/discovery/openstack/openstack.go b/discovery/openstack/openstack.go index 9b5f90519..171ead8b2 100644 --- a/discovery/openstack/openstack.go +++ b/discovery/openstack/openstack.go @@ -19,6 +19,7 @@ import ( "github.com/go-kit/kit/log" "github.com/gophercloud/gophercloud" + "github.com/gophercloud/gophercloud/openstack" "github.com/prometheus/client_golang/prometheus" "golang.org/x/net/context" @@ -52,15 +53,24 @@ type Discovery interface { // NewDiscovery returns a new OpenStackDiscovery which periodically refreshes its targets. func NewDiscovery(conf *config.OpenstackSDConfig, l log.Logger) (Discovery, error) { - 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, + var opts gophercloud.AuthOptions + if conf.IdentityEndpoint == "" { + var err error + opts, err = openstack.AuthOptionsFromEnv() + if err != nil { + return nil, err + } + } else { + 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 { case config.OpenStackRoleHypervisor: