2021-06-25 10:20:18 +00:00
|
|
|
package dockerswarm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var configMap = discoveryutils.NewConfigMap()
|
|
|
|
|
|
|
|
type apiConfig struct {
|
|
|
|
client *discoveryutils.Client
|
|
|
|
port int
|
|
|
|
|
2023-01-12 06:50:34 +00:00
|
|
|
// role is the type of objects to discover.
|
2023-01-12 06:54:16 +00:00
|
|
|
//
|
|
|
|
// filtersQueryArg is applied only to the given role - the rest of objects are queried without filters.
|
2023-01-12 06:50:34 +00:00
|
|
|
role string
|
|
|
|
|
2021-06-25 10:20:18 +00:00
|
|
|
// filtersQueryArg contains escaped `filters` query arg to add to each request to Docker Swarm API.
|
|
|
|
filtersQueryArg string
|
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
|
|
|
port: sdc.Port,
|
|
|
|
filtersQueryArg: getFiltersQueryArg(sdc.Filters),
|
|
|
|
}
|
|
|
|
if cfg.port == 0 {
|
|
|
|
cfg.port = 80
|
|
|
|
}
|
|
|
|
ac, err := sdc.HTTPClientConfig.NewConfig(baseDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse auth config: %w", err)
|
|
|
|
}
|
|
|
|
proxyAC, err := sdc.ProxyClientConfig.NewConfig(baseDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse proxy auth config: %w", err)
|
|
|
|
}
|
2023-05-26 07:39:45 +00:00
|
|
|
client, err := discoveryutils.NewClient(sdc.Host, ac, sdc.ProxyURL, proxyAC, sdc.HTTPClientConfig.FollowRedirects)
|
2021-06-25 10:20:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create HTTP client for %q: %w", sdc.Host, err)
|
|
|
|
}
|
|
|
|
cfg.client = client
|
2023-01-12 06:50:34 +00:00
|
|
|
cfg.role = sdc.Role
|
2021-06-25 10:20:18 +00:00
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2023-01-12 06:50:34 +00:00
|
|
|
func (cfg *apiConfig) getAPIResponse(path, filtersQueryArg string) ([]byte, error) {
|
|
|
|
if len(filtersQueryArg) > 0 {
|
2021-06-25 10:20:18 +00:00
|
|
|
separator := "?"
|
|
|
|
if strings.Contains(path, "?") {
|
|
|
|
separator = "&"
|
|
|
|
}
|
|
|
|
path += separator + "filters=" + cfg.filtersQueryArg
|
|
|
|
}
|
|
|
|
return cfg.client.GetAPIResponse(path)
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:34:25 +00:00
|
|
|
// Encodes filters as `map[string][]string` and then marshals it to JSON.
|
|
|
|
// Reference: https://docs.docker.com/engine/api/v1.41/#tag/Task
|
2021-06-25 10:20:18 +00:00
|
|
|
func getFiltersQueryArg(filters []Filter) string {
|
|
|
|
if len(filters) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
2023-01-05 11:34:25 +00:00
|
|
|
|
|
|
|
m := make(map[string][]string)
|
2021-06-25 10:20:18 +00:00
|
|
|
for _, f := range filters {
|
2023-01-05 11:34:25 +00:00
|
|
|
m[f.Name] = f.Values
|
2021-06-25 10:20:18 +00:00
|
|
|
}
|
|
|
|
buf, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
logger.Panicf("BUG: unexpected error in json.Marshal: %s", err)
|
|
|
|
}
|
|
|
|
return url.QueryEscape(string(buf))
|
|
|
|
}
|