2020-10-12 10:38:21 +00:00
|
|
|
package dockerswarm
|
|
|
|
|
|
|
|
import (
|
2020-11-23 14:26:52 +00:00
|
|
|
"encoding/json"
|
2020-10-12 10:38:21 +00:00
|
|
|
"fmt"
|
2020-11-23 14:26:52 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2020-10-12 10:38:21 +00:00
|
|
|
|
2020-11-23 14:26:52 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2020-10-12 10:38:21 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var configMap = discoveryutils.NewConfigMap()
|
|
|
|
|
|
|
|
type apiConfig struct {
|
|
|
|
client *discoveryutils.Client
|
|
|
|
port int
|
2020-11-23 14:26:52 +00:00
|
|
|
|
|
|
|
// filtersQueryArg contains escaped `filters` query arg to add to each request to Docker Swarm API.
|
|
|
|
filtersQueryArg string
|
2020-10-12 10:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) {
|
|
|
|
v, err := configMap.Get(sdc, func() (interface{}, error) { return newAPIConfig(sdc, baseDir) })
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return v.(*apiConfig), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) {
|
|
|
|
cfg := &apiConfig{
|
2020-11-23 14:26:52 +00:00
|
|
|
port: sdc.Port,
|
|
|
|
filtersQueryArg: getFiltersQueryArg(sdc.Filters),
|
2020-10-12 10:38:21 +00:00
|
|
|
}
|
2020-12-24 08:52:37 +00:00
|
|
|
|
2020-10-12 13:12:36 +00:00
|
|
|
ac, err := promauth.NewConfig(baseDir, sdc.BasicAuth, sdc.BearerToken, sdc.BearerTokenFile, sdc.TLSConfig)
|
2020-10-12 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-24 08:56:10 +00:00
|
|
|
client, err := discoveryutils.NewClient(sdc.Host, ac, sdc.ProxyURL)
|
2020-10-12 10:38:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create HTTP client for %q: %w", sdc.Host, err)
|
|
|
|
}
|
|
|
|
cfg.client = client
|
|
|
|
return cfg, nil
|
|
|
|
}
|
2020-11-23 14:26:52 +00:00
|
|
|
|
|
|
|
func (cfg *apiConfig) getAPIResponse(path string) ([]byte, error) {
|
|
|
|
if len(cfg.filtersQueryArg) > 0 {
|
|
|
|
separator := "?"
|
|
|
|
if strings.Contains(path, "?") {
|
|
|
|
separator = "&"
|
|
|
|
}
|
|
|
|
path += separator + "filters=" + cfg.filtersQueryArg
|
|
|
|
}
|
|
|
|
return cfg.client.GetAPIResponse(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFiltersQueryArg(filters []Filter) string {
|
|
|
|
if len(filters) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
m := make(map[string]map[string]bool)
|
|
|
|
for _, f := range filters {
|
|
|
|
x := m[f.Name]
|
|
|
|
if x == nil {
|
|
|
|
x = make(map[string]bool)
|
|
|
|
m[f.Name] = x
|
|
|
|
}
|
|
|
|
for _, value := range f.Values {
|
|
|
|
x[value] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buf, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("BUG: unexpected error in json.Marshal: %s", err)
|
|
|
|
}
|
|
|
|
return url.QueryEscape(string(buf))
|
|
|
|
}
|