2020-10-12 12:38:21 +02:00
|
|
|
package dockerswarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-12-24 09:56:10 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
2020-10-12 12:38:21 +02: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 15:26:52 +01:00
|
|
|
Host string `yaml:"host"`
|
|
|
|
Role string `yaml:"role"`
|
|
|
|
Port int `yaml:"port,omitempty"`
|
|
|
|
Filters []Filter `yaml:"filters,omitempty"`
|
|
|
|
|
2020-12-24 09:56:10 +01:00
|
|
|
ProxyURL proxy.URL `yaml:"proxy_url,omitempty"`
|
2020-11-13 15:17:03 +01:00
|
|
|
TLSConfig *promauth.TLSConfig `yaml:"tls_config,omitempty"`
|
2020-10-12 15:12:36 +02:00
|
|
|
// refresh_interval is obtained from `-promscrape.dockerswarmSDCheckInterval` command-line option
|
2020-11-13 15:17:03 +01:00
|
|
|
BasicAuth *promauth.BasicAuthConfig `yaml:"basic_auth,omitempty"`
|
|
|
|
BearerToken string `yaml:"bearer_token,omitempty"`
|
|
|
|
BearerTokenFile string `yaml:"bearer_token_file,omitempty"`
|
2020-10-12 12:38:21 +02:00
|
|
|
}
|
|
|
|
|
2020-11-23 15:26:52 +01: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 15:12:36 +02:00
|
|
|
// GetLabels returns dockerswarm labels according to sdc.
|
2020-10-12 12:38:21 +02:00
|
|
|
func GetLabels(sdc *SDConfig, baseDir string) ([]map[string]string, error) {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|