Support discover instances from all projects (#4682)

By default, OpenStack SD only queries for instances
from specified project. To discover instances from other
projects, users have to add more openstack_sd_configs for
each project.

This patch adds `all_tenants` <bool> options to
openstack_sd_configs. For example:

- job_name: 'openstack_all_instances'
  openstack_sd_configs:
    - role: instance
      region: RegionOne
      identity_endpoint: http://<identity_server>/identity/v3
      username: <username>
      password: <super_secret_password>
      domain_name: Default
      all_tenants: true

Co-authored-by: Kien Nguyen <kiennt2609@gmail.com>
Signed-off-by: dmatosl <danielmatos.lima@gmail.com>
This commit is contained in:
Kien Nguyen-Tuan 2018-10-17 19:01:33 +07:00 committed by Brian Brazil
parent a0ba9b7f3e
commit 9c5370fdfe
4 changed files with 19 additions and 12 deletions

View File

@ -27,7 +27,6 @@ import (
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/pagination"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery/targetgroup"
"github.com/prometheus/prometheus/util/strutil"
)
@ -46,22 +45,23 @@ const (
// InstanceDiscovery discovers OpenStack instances.
type InstanceDiscovery struct {
provider *gophercloud.ProviderClient
authOpts *gophercloud.AuthOptions
region string
interval time.Duration
logger log.Logger
port int
provider *gophercloud.ProviderClient
authOpts *gophercloud.AuthOptions
region string
interval time.Duration
logger log.Logger
port int
allTenants bool
}
// NewInstanceDiscovery returns a new instance discovery.
func NewInstanceDiscovery(provider *gophercloud.ProviderClient, opts *gophercloud.AuthOptions,
interval time.Duration, port int, region string, l log.Logger) *InstanceDiscovery {
interval time.Duration, port int, region string, allTenants bool, l log.Logger) *InstanceDiscovery {
if l == nil {
l = log.NewNopLogger()
}
return &InstanceDiscovery{provider: provider, authOpts: opts,
region: region, interval: interval, port: port, logger: l}
region: region, interval: interval, port: port, allTenants: allTenants, logger: l}
}
// Run implements the Discoverer interface.
@ -153,7 +153,9 @@ func (i *InstanceDiscovery) refresh() (*targetgroup.Group, error) {
// OpenStack API reference
// https://developer.openstack.org/api-ref/compute/#list-servers
opts := servers.ListOpts{}
opts := servers.ListOpts{
AllTenants: i.allTenants,
}
pager := servers.List(client, opts)
tg := &targetgroup.Group{
Source: fmt.Sprintf("OS_" + i.region),

View File

@ -48,6 +48,7 @@ func (s *OpenstackSDInstanceTestSuite) openstackAuthSuccess() (Discovery, error)
DomainName: "12345",
Region: "RegionOne",
Role: "instance",
AllTenants: true,
}
return NewDiscovery(&conf, nil)
}

View File

@ -27,7 +27,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
config_util "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/discovery/targetgroup"
)
@ -63,6 +62,7 @@ type SDConfig struct {
Region string `yaml:"region"`
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"`
Port int `yaml:"port"`
AllTenants bool `yaml:"all_tenants,omitempty"`
TLSConfig config_util.TLSConfig `yaml:"tls_config,omitempty"`
}
@ -168,7 +168,7 @@ func NewDiscovery(conf *SDConfig, l log.Logger) (Discovery, error) {
return hypervisor, nil
case OpenStackRoleInstance:
instance := NewInstanceDiscovery(client, &opts,
time.Duration(conf.RefreshInterval), conf.Port, conf.Region, l)
time.Duration(conf.RefreshInterval), conf.Port, conf.Region, conf.AllTenants, l)
return instance, nil
default:
return nil, errors.New("unknown OpenStack discovery role")

View File

@ -531,6 +531,10 @@ region: <string>
[ project_name: <string> ]
[ project_id: <string> ]
# Whether the service discovery should list all instances for all projects.
# It is only relevant for the 'instance' role and usually requires admin permissions.
[ all_tenants: <boolean> | default: false ]
# Refresh interval to re-read the instance list.
[ refresh_interval: <duration> | default = 60s ]