2020-10-12 10:38:21 +00:00
|
|
|
package dockerswarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-12-24 08:56:10 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
2020-10-12 10:38:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SDConfig represents docker swarm service discovery configuration
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#dockerswarm_sd_config
|
|
|
|
type SDConfig struct {
|
2020-11-23 14:26:52 +00:00
|
|
|
Host string `yaml:"host"`
|
|
|
|
Role string `yaml:"role"`
|
|
|
|
Port int `yaml:"port,omitempty"`
|
|
|
|
Filters []Filter `yaml:"filters,omitempty"`
|
|
|
|
|
2020-12-24 08:56:10 +00:00
|
|
|
ProxyURL proxy.URL `yaml:"proxy_url,omitempty"`
|
2020-11-13 14:17:03 +00:00
|
|
|
TLSConfig *promauth.TLSConfig `yaml:"tls_config,omitempty"`
|
2020-10-12 13:12:36 +00:00
|
|
|
// refresh_interval is obtained from `-promscrape.dockerswarmSDCheckInterval` command-line option
|
2020-11-13 14:17:03 +00:00
|
|
|
BasicAuth *promauth.BasicAuthConfig `yaml:"basic_auth,omitempty"`
|
|
|
|
BearerToken string `yaml:"bearer_token,omitempty"`
|
|
|
|
BearerTokenFile string `yaml:"bearer_token_file,omitempty"`
|
2020-10-12 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-23 14:26:52 +00:00
|
|
|
// Filter is a filter, which can be passed to SDConfig.
|
|
|
|
type Filter struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Values []string `yaml:"values"`
|
|
|
|
}
|
|
|
|
|
2020-10-12 13:12:36 +00:00
|
|
|
// GetLabels returns dockerswarm labels according to sdc.
|
2021-02-26 13:53:42 +00:00
|
|
|
func (sdc *SDConfig) GetLabels(baseDir string) ([]map[string]string, error) {
|
2020-10-12 10:38:21 +00:00
|
|
|
cfg, err := getAPIConfig(sdc, baseDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot get API config: %w", err)
|
|
|
|
}
|
|
|
|
switch sdc.Role {
|
|
|
|
case "tasks":
|
|
|
|
return getTasksLabels(cfg)
|
|
|
|
case "services":
|
|
|
|
return getServicesLabels(cfg)
|
|
|
|
case "nodes":
|
|
|
|
return getNodesLabels(cfg)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unexpected `role`: %q; must be one of `tasks`, `services` or `nodes`; skipping it", sdc.Role)
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 12:13:56 +00:00
|
|
|
|
|
|
|
// MustStop stops further usage for sdc.
|
|
|
|
func (sdc *SDConfig) MustStop() {
|
|
|
|
configMap.Delete(sdc)
|
|
|
|
}
|