2020-05-04 17:48:02 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-12-03 17:47:40 +00:00
|
|
|
"strconv"
|
2020-05-04 17:48:02 +00:00
|
|
|
"strings"
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// apiConfig contains config for API server
|
2020-12-03 17:47:40 +00:00
|
|
|
// with consulWatch service.
|
2020-05-04 17:48:02 +00:00
|
|
|
type apiConfig struct {
|
2020-12-03 17:47:40 +00:00
|
|
|
tagSeparator string
|
|
|
|
consulWatcher *consulWatch
|
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) {
|
|
|
|
token, err := getToken(sdc.Token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var ba *promauth.BasicAuthConfig
|
|
|
|
if len(sdc.Username) > 0 {
|
|
|
|
ba = &promauth.BasicAuthConfig{
|
|
|
|
Username: sdc.Username,
|
|
|
|
Password: sdc.Password,
|
|
|
|
}
|
|
|
|
token = ""
|
|
|
|
}
|
|
|
|
ac, err := promauth.NewConfig(baseDir, ba, token, "", sdc.TLSConfig)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
client, err := discoveryutils.NewClient(apiServer, ac)
|
|
|
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-03 17:47:40 +00:00
|
|
|
cw, err := newConsulWatch(client, sdc, dc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot start consul watcher: %w", err)
|
|
|
|
}
|
|
|
|
cfg := &apiConfig{
|
|
|
|
tagSeparator: tagSeparator,
|
|
|
|
consulWatcher: cw,
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2020-05-05 04:48:08 +00:00
|
|
|
func getToken(token *string) (string, error) {
|
|
|
|
if token != nil {
|
|
|
|
return *token, nil
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|
|
|
|
if tokenFile := os.Getenv("CONSUL_HTTP_TOKEN_FILE"); tokenFile != "" {
|
|
|
|
data, err := ioutil.ReadFile(tokenFile)
|
|
|
|
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-03 17:47:40 +00:00
|
|
|
// returns ServiceNodesState and version index.
|
|
|
|
func getServiceState(client *discoveryutils.Client, svc, baseArgs string, index uint64) ([]ServiceNode, uint64, error) {
|
|
|
|
path := fmt.Sprintf("/v1/health/service/%s%s", svc, baseArgs)
|
|
|
|
|
|
|
|
data, newIndex, err := getBlockingAPIResponse(client, path, index)
|
|
|
|
if err != nil {
|
|
|
|
return nil, index, err
|
|
|
|
}
|
|
|
|
sns, err := parseServiceNodes(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, index, err
|
|
|
|
}
|
|
|
|
return sns, newIndex, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns consul api response with new index version of object.
|
|
|
|
// https://www.consul.io/api-docs/features/blocking
|
|
|
|
func getBlockingAPIResponse(client *discoveryutils.Client, path string, index uint64) ([]byte, uint64, error) {
|
|
|
|
path += "&index=" + strconv.FormatUint(index, 10)
|
|
|
|
path = path + fmt.Sprintf("&wait=%s", watchTime)
|
|
|
|
getMeta := func(resp *fasthttp.Response) {
|
|
|
|
if ind := resp.Header.Peek("X-Consul-Index"); len(ind) > 0 {
|
|
|
|
newIndex, err := strconv.ParseUint(string(ind), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("failed to parse consul index: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// reset index
|
|
|
|
// 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 {
|
|
|
|
return nil, index, fmt.Errorf("failed query consul api path=%q, err=%w", path, err)
|
|
|
|
}
|
|
|
|
return data, index, nil
|
2020-05-04 17:48:02 +00:00
|
|
|
}
|