2020-05-04 17:48:02 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2021-03-23 17:33:11 +00:00
|
|
|
"flag"
|
2020-05-04 17:48:02 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-12-03 17:47:40 +00:00
|
|
|
"strconv"
|
2020-05-04 17:48:02 +00:00
|
|
|
"strings"
|
2020-12-03 17:50:50 +00:00
|
|
|
"time"
|
2020-05-04 17:48:02 +00:00
|
|
|
|
2020-12-03 17:47:40 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2020-05-04 17:48:02 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2020-12-03 17:47:40 +00:00
|
|
|
"github.com/VictoriaMetrics/fasthttp"
|
2020-05-04 17:48:02 +00:00
|
|
|
)
|
|
|
|
|
2021-03-23 17:33:11 +00:00
|
|
|
var waitTime = flag.Duration("promscrape.consul.waitTime", 0, "Wait time used by Consul service discovery. Default value is used if not set")
|
|
|
|
|
2020-12-03 17:50:50 +00:00
|
|
|
// apiConfig contains config for API server.
|
2020-05-04 17:48:02 +00:00
|
|
|
type apiConfig struct {
|
2020-12-03 17:47:40 +00:00
|
|
|
tagSeparator string
|
2020-12-03 17:50:50 +00:00
|
|
|
consulWatcher *consulWatcher
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 12:13:56 +00:00
|
|
|
func (ac *apiConfig) mustStop() {
|
|
|
|
ac.consulWatcher.mustStop()
|
|
|
|
}
|
|
|
|
|
2020-05-04 17:48:02 +00:00
|
|
|
var configMap = discoveryutils.NewConfigMap()
|
|
|
|
|
|
|
|
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) {
|
2021-06-22 10:17:42 +00:00
|
|
|
hcc := sdc.HTTPClientConfig
|
2020-05-04 17:48:02 +00:00
|
|
|
token, err := getToken(sdc.Token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-22 10:17:42 +00:00
|
|
|
if token != "" {
|
2021-11-05 12:41:14 +00:00
|
|
|
if hcc.BearerToken != nil {
|
2021-06-22 10:17:42 +00:00
|
|
|
return nil, fmt.Errorf("cannot set both token and bearer_token configs")
|
|
|
|
}
|
2021-11-05 12:41:14 +00:00
|
|
|
hcc.BearerToken = promauth.NewSecret(token)
|
2021-06-22 10:17:42 +00:00
|
|
|
}
|
2020-05-04 17:48:02 +00:00
|
|
|
if len(sdc.Username) > 0 {
|
2021-06-22 10:17:42 +00:00
|
|
|
if hcc.BasicAuth != nil {
|
|
|
|
return nil, fmt.Errorf("cannot set both username and basic_auth configs")
|
|
|
|
}
|
|
|
|
hcc.BasicAuth = &promauth.BasicAuthConfig{
|
2020-05-04 17:48:02 +00:00
|
|
|
Username: sdc.Username,
|
|
|
|
Password: sdc.Password,
|
|
|
|
}
|
|
|
|
}
|
2021-06-22 10:17:42 +00:00
|
|
|
ac, err := hcc.NewConfig(baseDir)
|
2020-05-04 17:48:02 +00:00
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot parse auth config: %w", err)
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
apiServer := sdc.Server
|
|
|
|
if apiServer == "" {
|
|
|
|
apiServer = "localhost:8500"
|
|
|
|
}
|
|
|
|
if !strings.Contains(apiServer, "://") {
|
|
|
|
scheme := sdc.Scheme
|
|
|
|
if scheme == "" {
|
|
|
|
scheme = "http"
|
|
|
|
}
|
|
|
|
apiServer = scheme + "://" + apiServer
|
|
|
|
}
|
2021-04-03 21:40:08 +00:00
|
|
|
proxyAC, err := sdc.ProxyClientConfig.NewConfig(baseDir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot parse proxy auth config: %w", err)
|
|
|
|
}
|
|
|
|
client, err := discoveryutils.NewClient(apiServer, ac, sdc.ProxyURL, proxyAC)
|
2020-05-04 17:48:02 +00:00
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot create HTTP client for %q: %w", apiServer, err)
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
tagSeparator := ","
|
|
|
|
if sdc.TagSeparator != nil {
|
|
|
|
tagSeparator = *sdc.TagSeparator
|
|
|
|
}
|
|
|
|
dc, err := getDatacenter(client, sdc.Datacenter)
|
|
|
|
if err != nil {
|
2023-01-06 02:01:11 +00:00
|
|
|
return nil, fmt.Errorf("cannot obtain consul datacenter: %w", err)
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 09:49:44 +00:00
|
|
|
namespace := sdc.Namespace
|
|
|
|
// default namespace can be detected from env var.
|
|
|
|
if namespace == "" {
|
|
|
|
namespace = os.Getenv("CONSUL_NAMESPACE")
|
|
|
|
}
|
|
|
|
|
2021-06-22 14:42:29 +00:00
|
|
|
cw := newConsulWatcher(client, sdc, dc, namespace)
|
2020-12-03 17:47:40 +00:00
|
|
|
cfg := &apiConfig{
|
|
|
|
tagSeparator: tagSeparator,
|
|
|
|
consulWatcher: cw,
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2021-11-05 12:41:14 +00:00
|
|
|
func getToken(token *promauth.Secret) (string, error) {
|
2020-05-05 04:48:08 +00:00
|
|
|
if token != nil {
|
2021-11-05 12:41:14 +00:00
|
|
|
return token.String(), nil
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
if tokenFile := os.Getenv("CONSUL_HTTP_TOKEN_FILE"); tokenFile != "" {
|
2022-08-21 20:51:13 +00:00
|
|
|
data, err := os.ReadFile(tokenFile)
|
2020-05-04 17:48:02 +00:00
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return "", fmt.Errorf("cannot read consul token file %q; probably, `token` arg is missing in `consul_sd_config`? error: %w", tokenFile, err)
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
return string(data), nil
|
|
|
|
}
|
2020-05-05 04:48:08 +00:00
|
|
|
t := os.Getenv("CONSUL_HTTP_TOKEN")
|
2020-05-04 17:48:02 +00:00
|
|
|
// Allow empty token - it shouls work if authorization is disabled in Consul
|
2020-05-05 04:48:08 +00:00
|
|
|
return t, nil
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getDatacenter(client *discoveryutils.Client, dc string) (string, error) {
|
|
|
|
if dc != "" {
|
|
|
|
return dc, nil
|
|
|
|
}
|
|
|
|
// See https://www.consul.io/api/agent.html#read-configuration
|
|
|
|
data, err := client.GetAPIResponse("/v1/agent/self")
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return "", fmt.Errorf("cannot query consul agent info: %w", err)
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
a, err := parseAgent(data)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return a.Config.Datacenter, nil
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:22:37 +00:00
|
|
|
// maxWaitTime is duration for consul blocking request.
|
2021-03-23 17:33:11 +00:00
|
|
|
func maxWaitTime() time.Duration {
|
2020-12-11 15:22:37 +00:00
|
|
|
d := discoveryutils.BlockingClientReadTimeout
|
|
|
|
// Consul adds random delay up to wait/16, so reduce the timeout in order to keep it below BlockingClientReadTimeout.
|
|
|
|
// See https://www.consul.io/api-docs/features/blocking
|
|
|
|
d -= d / 8
|
|
|
|
// The timeout cannot exceed 10 minuntes. See https://www.consul.io/api-docs/features/blocking
|
|
|
|
if d > 10*time.Minute {
|
|
|
|
d = 10 * time.Minute
|
|
|
|
}
|
2021-03-23 17:33:11 +00:00
|
|
|
if *waitTime > time.Second && *waitTime < d {
|
|
|
|
d = *waitTime
|
|
|
|
}
|
2020-12-11 15:22:37 +00:00
|
|
|
return d
|
2021-03-23 17:33:11 +00:00
|
|
|
}
|
2020-12-03 17:47:40 +00:00
|
|
|
|
2020-12-03 17:50:50 +00:00
|
|
|
// getBlockingAPIResponse perfoms blocking request to Consul via client and returns response.
|
|
|
|
//
|
|
|
|
// See https://www.consul.io/api-docs/features/blocking .
|
|
|
|
func getBlockingAPIResponse(client *discoveryutils.Client, path string, index int64) ([]byte, int64, error) {
|
|
|
|
path += "&index=" + strconv.FormatInt(index, 10)
|
2021-03-23 17:33:11 +00:00
|
|
|
path += "&wait=" + fmt.Sprintf("%ds", int(maxWaitTime().Seconds()))
|
2020-12-03 17:47:40 +00:00
|
|
|
getMeta := func(resp *fasthttp.Response) {
|
2020-12-03 18:05:23 +00:00
|
|
|
ind := resp.Header.Peek("X-Consul-Index")
|
|
|
|
if len(ind) == 0 {
|
|
|
|
logger.Errorf("cannot find X-Consul-Index header in response from %q", path)
|
|
|
|
return
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
2020-12-03 18:05:23 +00:00
|
|
|
newIndex, err := strconv.ParseInt(string(ind), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot parse X-Consul-Index header value in response from %q: %s", path, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Properly handle the returned newIndex according to https://www.consul.io/api-docs/features/blocking#implementation-details
|
|
|
|
if newIndex < 1 {
|
|
|
|
index = 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if index > newIndex {
|
|
|
|
index = 0
|
|
|
|
return
|
|
|
|
}
|
|
|
|
index = newIndex
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
2020-12-03 17:47:40 +00:00
|
|
|
data, err := client.GetBlockingAPIResponse(path, getMeta)
|
|
|
|
if err != nil {
|
2020-12-03 17:50:50 +00:00
|
|
|
return nil, index, fmt.Errorf("cannot perform blocking Consul API request at %q: %w", path, err)
|
2020-12-03 17:47:40 +00:00
|
|
|
}
|
|
|
|
return data, index, nil
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|