2017-06-01 21:49:02 +00:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
|
|
|
package openstack
|
|
|
|
|
|
|
|
import (
|
2017-10-25 04:21:42 +00:00
|
|
|
"context"
|
2022-06-03 11:47:14 +00:00
|
|
|
"errors"
|
2020-07-02 14:17:56 +00:00
|
|
|
"fmt"
|
2024-09-10 01:41:53 +00:00
|
|
|
"log/slog"
|
2018-09-24 14:08:42 +00:00
|
|
|
"net/http"
|
2017-06-01 21:49:02 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gophercloud/gophercloud"
|
2017-10-16 17:01:50 +00:00
|
|
|
"github.com/gophercloud/gophercloud/openstack"
|
2022-08-31 13:50:38 +00:00
|
|
|
"github.com/mwitkow/go-conntrack"
|
2023-10-23 13:55:36 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-08-20 12:48:26 +00:00
|
|
|
"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 20:01:34 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2019-03-12 12:53:03 +00:00
|
|
|
|
2020-08-20 12:48:26 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2019-03-25 10:54:22 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/refresh"
|
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 20:01:34 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2017-06-01 21:49:02 +00:00
|
|
|
)
|
|
|
|
|
2019-03-25 10:54:22 +00:00
|
|
|
// DefaultSDConfig is the default OpenStack SD configuration.
|
|
|
|
var DefaultSDConfig = SDConfig{
|
|
|
|
Port: 80,
|
|
|
|
RefreshInterval: model.Duration(60 * time.Second),
|
2020-07-02 14:17:56 +00:00
|
|
|
Availability: "public",
|
2019-03-25 10:54:22 +00:00
|
|
|
}
|
2017-06-01 21:49:02 +00:00
|
|
|
|
2020-08-20 12:48:26 +00:00
|
|
|
func init() {
|
|
|
|
discovery.RegisterConfig(&SDConfig{})
|
|
|
|
}
|
|
|
|
|
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 20:01:34 +00:00
|
|
|
// SDConfig is the configuration for OpenStack based service discovery.
|
|
|
|
type SDConfig struct {
|
2020-08-20 12:48:26 +00:00
|
|
|
IdentityEndpoint string `yaml:"identity_endpoint"`
|
|
|
|
Username string `yaml:"username"`
|
|
|
|
UserID string `yaml:"userid"`
|
|
|
|
Password config.Secret `yaml:"password"`
|
|
|
|
ProjectName string `yaml:"project_name"`
|
|
|
|
ProjectID string `yaml:"project_id"`
|
|
|
|
DomainName string `yaml:"domain_name"`
|
|
|
|
DomainID string `yaml:"domain_id"`
|
|
|
|
ApplicationCredentialName string `yaml:"application_credential_name"`
|
|
|
|
ApplicationCredentialID string `yaml:"application_credential_id"`
|
|
|
|
ApplicationCredentialSecret config.Secret `yaml:"application_credential_secret"`
|
|
|
|
Role Role `yaml:"role"`
|
|
|
|
Region string `yaml:"region"`
|
|
|
|
RefreshInterval model.Duration `yaml:"refresh_interval"`
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
AllTenants bool `yaml:"all_tenants,omitempty"`
|
|
|
|
TLSConfig config.TLSConfig `yaml:"tls_config,omitempty"`
|
|
|
|
Availability string `yaml:"availability,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-01-23 15:53:55 +00:00
|
|
|
// NewDiscovererMetrics implements discovery.Config.
|
|
|
|
func (*SDConfig) NewDiscovererMetrics(reg prometheus.Registerer, rmi discovery.RefreshMetricsInstantiator) discovery.DiscovererMetrics {
|
|
|
|
return &openstackMetrics{
|
|
|
|
refreshMetrics: rmi,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-20 12:48:26 +00:00
|
|
|
// Name returns the name of the Config.
|
|
|
|
func (*SDConfig) Name() string { return "openstack" }
|
|
|
|
|
|
|
|
// NewDiscoverer returns a Discoverer for the Config.
|
|
|
|
func (c *SDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Discoverer, error) {
|
2024-01-23 15:53:55 +00:00
|
|
|
return NewDiscovery(c, opts.Logger, opts.Metrics)
|
2020-08-20 12:48:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDirectory joins any relative file paths with dir.
|
|
|
|
func (c *SDConfig) SetDirectory(dir string) {
|
|
|
|
c.TLSConfig.SetDirectory(dir)
|
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 20:01:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 10:54:22 +00:00
|
|
|
// Role is the role of the target in OpenStack.
|
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 20:01:34 +00:00
|
|
|
type Role string
|
|
|
|
|
|
|
|
// The valid options for OpenStackRole.
|
|
|
|
const (
|
|
|
|
// OpenStack document reference
|
|
|
|
// https://docs.openstack.org/nova/pike/admin/arch.html#hypervisors
|
|
|
|
OpenStackRoleHypervisor Role = "hypervisor"
|
|
|
|
// OpenStack document reference
|
|
|
|
// https://docs.openstack.org/horizon/pike/user/launch-instances.html
|
|
|
|
OpenStackRoleInstance Role = "instance"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *Role) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
if err := unmarshal((*string)(c)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch *c {
|
|
|
|
case OpenStackRoleHypervisor, OpenStackRoleInstance:
|
|
|
|
return nil
|
|
|
|
default:
|
2022-06-03 11:47:14 +00:00
|
|
|
return fmt.Errorf("unknown OpenStack SD role %q", *c)
|
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 20:01:34 +00: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
|
|
|
|
}
|
2020-07-02 14:17:56 +00:00
|
|
|
|
|
|
|
switch c.Availability {
|
|
|
|
case "public", "internal", "admin":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown availability %s, must be one of admin, internal or public", c.Availability)
|
|
|
|
}
|
|
|
|
|
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 20:01:34 +00:00
|
|
|
if c.Role == "" {
|
2019-03-25 23:01:12 +00:00
|
|
|
return errors.New("role missing (one of: instance, hypervisor)")
|
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 20:01:34 +00:00
|
|
|
}
|
2018-03-05 19:27:54 +00:00
|
|
|
if c.Region == "" {
|
2019-03-25 23:01:12 +00:00
|
|
|
return errors.New("openstack SD configuration requires a region")
|
2018-03-05 19:27:54 +00:00
|
|
|
}
|
2020-07-02 14:17:56 +00:00
|
|
|
|
2018-04-04 08:07:39 +00:00
|
|
|
return nil
|
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 20:01:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 12:53:03 +00:00
|
|
|
type refresher interface {
|
2019-03-25 10:54:22 +00:00
|
|
|
refresh(context.Context) ([]*targetgroup.Group, error)
|
2019-03-12 12:53:03 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 10:54:22 +00:00
|
|
|
// NewDiscovery returns a new OpenStack Discoverer which periodically refreshes its targets.
|
2024-09-10 01:41:53 +00:00
|
|
|
func NewDiscovery(conf *SDConfig, l *slog.Logger, metrics discovery.DiscovererMetrics) (*refresh.Discovery, error) {
|
2024-01-23 15:53:55 +00:00
|
|
|
m, ok := metrics.(*openstackMetrics)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("invalid discovery metrics type")
|
|
|
|
}
|
|
|
|
|
2019-03-25 10:54:22 +00:00
|
|
|
r, err := newRefresher(conf, l)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return refresh.NewDiscovery(
|
2023-10-23 13:55:36 +00:00
|
|
|
refresh.Options{
|
2024-01-23 15:53:55 +00:00
|
|
|
Logger: l,
|
|
|
|
Mech: "openstack",
|
|
|
|
Interval: time.Duration(conf.RefreshInterval),
|
|
|
|
RefreshF: r.refresh,
|
|
|
|
MetricsInstantiator: m.refreshMetrics,
|
2023-10-23 13:55:36 +00:00
|
|
|
},
|
2019-03-25 10:54:22 +00:00
|
|
|
), nil
|
2017-06-01 21:49:02 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 01:41:53 +00:00
|
|
|
func newRefresher(conf *SDConfig, l *slog.Logger) (refresher, error) {
|
2017-10-16 17:01:50 +00:00
|
|
|
var opts gophercloud.AuthOptions
|
|
|
|
if conf.IdentityEndpoint == "" {
|
|
|
|
var err error
|
|
|
|
opts, err = openstack.AuthOptionsFromEnv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
opts = gophercloud.AuthOptions{
|
2019-01-09 15:18:58 +00:00
|
|
|
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,
|
|
|
|
ApplicationCredentialID: conf.ApplicationCredentialID,
|
|
|
|
ApplicationCredentialName: conf.ApplicationCredentialName,
|
|
|
|
ApplicationCredentialSecret: string(conf.ApplicationCredentialSecret),
|
2017-10-16 17:01:50 +00:00
|
|
|
}
|
2017-06-01 21:49:02 +00:00
|
|
|
}
|
2018-10-17 14:04:07 +00:00
|
|
|
client, err := openstack.NewClient(opts.IdentityEndpoint)
|
2018-09-24 14:08:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-20 12:48:26 +00:00
|
|
|
tls, err := config.NewTLSConfig(&conf.TLSConfig)
|
2018-09-24 14:08:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client.HTTPClient = http.Client{
|
|
|
|
Transport: &http.Transport{
|
2020-07-02 20:50:32 +00:00
|
|
|
IdleConnTimeout: 2 * time.Duration(conf.RefreshInterval),
|
2018-09-24 14:08:42 +00:00
|
|
|
TLSClientConfig: tls,
|
|
|
|
DialContext: conntrack.NewDialContextFunc(
|
|
|
|
conntrack.DialWithTracing(),
|
|
|
|
conntrack.DialWithName("openstack_sd"),
|
|
|
|
),
|
|
|
|
},
|
2020-07-02 20:50:32 +00:00
|
|
|
Timeout: time.Duration(conf.RefreshInterval),
|
2018-09-24 14:08:42 +00:00
|
|
|
}
|
2020-07-02 14:17:56 +00:00
|
|
|
availability := gophercloud.Availability(conf.Availability)
|
2017-08-22 08:16:01 +00:00
|
|
|
switch conf.Role {
|
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 20:01:34 +00:00
|
|
|
case OpenStackRoleHypervisor:
|
2020-07-02 14:17:56 +00:00
|
|
|
return newHypervisorDiscovery(client, &opts, conf.Port, conf.Region, availability, l), nil
|
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 20:01:34 +00:00
|
|
|
case OpenStackRoleInstance:
|
2020-07-02 14:17:56 +00:00
|
|
|
return newInstanceDiscovery(client, &opts, conf.Port, conf.Region, conf.AllTenants, availability, l), nil
|
2019-03-12 12:53:03 +00:00
|
|
|
}
|
2019-03-25 10:54:22 +00:00
|
|
|
return nil, errors.New("unknown OpenStack discovery role")
|
2017-06-01 21:49:02 +00:00
|
|
|
}
|