2020-04-13 20:02:27 +02:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getAPIResponse(cfg *APIConfig, role, path string) ([]byte, error) {
|
|
|
|
hcv, err := getHostClient(cfg.Server, cfg.AuthConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
query := joinSelectors(role, cfg.Namespaces, cfg.Selectors)
|
|
|
|
if len(query) > 0 {
|
|
|
|
path += "?" + query
|
|
|
|
}
|
|
|
|
requestURL := hcv.apiServer + path
|
|
|
|
var u fasthttp.URI
|
|
|
|
u.Update(requestURL)
|
|
|
|
var req fasthttp.Request
|
|
|
|
req.SetRequestURIBytes(u.RequestURI())
|
|
|
|
req.SetHost(hcv.hostPort)
|
|
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
|
|
|
if hcv.ac != nil && hcv.ac.Authorization != "" {
|
|
|
|
req.Header.Set("Authorization", hcv.ac.Authorization)
|
|
|
|
}
|
|
|
|
var resp fasthttp.Response
|
|
|
|
// There is no need in calling DoTimeout, since the timeout is already set in hc.ReadTimeout above.
|
|
|
|
if err := hcv.hc.Do(&req, &resp); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot fetch %q: %s", requestURL, err)
|
|
|
|
}
|
|
|
|
var data []byte
|
|
|
|
if ce := resp.Header.Peek("Content-Encoding"); string(ce) == "gzip" {
|
|
|
|
dst, err := fasthttp.AppendGunzipBytes(nil, resp.Body())
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot ungzip response from %q: %s", requestURL, err)
|
|
|
|
}
|
|
|
|
data = dst
|
|
|
|
} else {
|
|
|
|
data = append(data[:0], resp.Body()...)
|
|
|
|
}
|
|
|
|
statusCode := resp.StatusCode()
|
|
|
|
if statusCode != fasthttp.StatusOK {
|
|
|
|
return nil, fmt.Errorf("unexpected status code returned from %q: %d; expecting %d; response body: %q",
|
|
|
|
requestURL, statusCode, fasthttp.StatusOK, data)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2020-04-14 13:19:15 +02:00
|
|
|
func getHostClient(apiServer string, ac *promauth.Config) (*hcValue, error) {
|
2020-04-20 16:07:02 +02:00
|
|
|
if len(apiServer) == 0 {
|
|
|
|
// ac is ignored when apiServer should be auto-discovered when running inside k8s pod.
|
|
|
|
ac = nil
|
|
|
|
}
|
2020-04-13 20:02:27 +02:00
|
|
|
k := hcKey{
|
|
|
|
apiServer: apiServer,
|
|
|
|
ac: ac,
|
|
|
|
}
|
|
|
|
hcMapLock.Lock()
|
|
|
|
defer hcMapLock.Unlock()
|
|
|
|
|
|
|
|
if !hasHCMapCleaner {
|
|
|
|
go hcMapCleaner()
|
|
|
|
hasHCMapCleaner = true
|
|
|
|
}
|
2020-04-14 13:19:15 +02:00
|
|
|
hcv := hcMap[k]
|
|
|
|
if hcv == nil {
|
2020-04-13 20:02:27 +02:00
|
|
|
hcvNew, err := newHostClient(apiServer, ac)
|
|
|
|
if err != nil {
|
|
|
|
return hcv, fmt.Errorf("cannot create new HTTP client for %q: %s", apiServer, err)
|
|
|
|
}
|
|
|
|
hcMap[k] = hcvNew
|
|
|
|
hcv = hcvNew
|
|
|
|
}
|
2020-04-14 13:19:15 +02:00
|
|
|
hcv.lastAccessTime = time.Now()
|
2020-04-13 20:02:27 +02:00
|
|
|
return hcv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func hcMapCleaner() {
|
|
|
|
tc := time.NewTicker(15 * time.Minute)
|
2020-04-14 13:19:15 +02:00
|
|
|
for currentTime := range tc.C {
|
2020-04-13 20:02:27 +02:00
|
|
|
hcMapLock.Lock()
|
2020-04-14 13:19:15 +02:00
|
|
|
for k, v := range hcMap {
|
|
|
|
if currentTime.Sub(v.lastAccessTime) > 10*time.Minute {
|
|
|
|
delete(hcMap, k)
|
|
|
|
}
|
|
|
|
}
|
2020-04-13 20:02:27 +02:00
|
|
|
hcMapLock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type hcKey struct {
|
|
|
|
apiServer string
|
|
|
|
ac *promauth.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
type hcValue struct {
|
2020-04-14 13:19:15 +02:00
|
|
|
hc *fasthttp.HostClient
|
|
|
|
ac *promauth.Config
|
|
|
|
apiServer string
|
|
|
|
hostPort string
|
|
|
|
lastAccessTime time.Time
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
hasHCMapCleaner bool
|
2020-04-14 13:19:15 +02:00
|
|
|
hcMap = make(map[hcKey]*hcValue)
|
2020-04-13 20:02:27 +02:00
|
|
|
hcMapLock sync.Mutex
|
|
|
|
)
|
|
|
|
|
2020-04-14 13:19:15 +02:00
|
|
|
func newHostClient(apiServer string, ac *promauth.Config) (*hcValue, error) {
|
2020-04-13 20:02:27 +02:00
|
|
|
if len(apiServer) == 0 {
|
|
|
|
// Assume we run at k8s pod.
|
|
|
|
// Discover apiServer and auth config according to k8s docs.
|
|
|
|
// See https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/#service-account-admission-controller
|
|
|
|
host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
|
|
|
|
if len(host) == 0 {
|
2020-04-14 13:19:15 +02:00
|
|
|
return nil, fmt.Errorf("cannot find KUBERNETES_SERVICE_HOST env var; it must be defined when running in k8s; " +
|
2020-04-13 20:02:27 +02:00
|
|
|
"probably, `kubernetes_sd_config->api_server` is missing in Prometheus configs?")
|
|
|
|
}
|
|
|
|
if len(port) == 0 {
|
2020-04-14 13:19:15 +02:00
|
|
|
return nil, fmt.Errorf("cannot find KUBERNETES_SERVICE_PORT env var; it must be defined when running in k8s; "+
|
2020-04-13 20:02:27 +02:00
|
|
|
"KUBERNETES_SERVICE_HOST=%q", host)
|
|
|
|
}
|
|
|
|
apiServer = "https://" + net.JoinHostPort(host, port)
|
|
|
|
tlsConfig := promauth.TLSConfig{
|
|
|
|
CAFile: "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt",
|
|
|
|
}
|
|
|
|
acNew, err := promauth.NewConfig("/", nil, "", "/var/run/secrets/kubernetes.io/serviceaccount/token", &tlsConfig)
|
|
|
|
if err != nil {
|
2020-04-14 13:19:15 +02:00
|
|
|
return nil, fmt.Errorf("cannot initialize service account auth: %s; probably, `kubernetes_sd_config->api_server` is missing in Prometheus configs?", err)
|
2020-04-13 20:02:27 +02:00
|
|
|
}
|
|
|
|
ac = acNew
|
|
|
|
}
|
|
|
|
|
|
|
|
var u fasthttp.URI
|
|
|
|
u.Update(apiServer)
|
|
|
|
hostPort := string(u.Host())
|
|
|
|
isTLS := string(u.Scheme()) == "https"
|
|
|
|
var tlsCfg *tls.Config
|
|
|
|
if isTLS && ac != nil {
|
|
|
|
tlsCfg = ac.NewTLSConfig()
|
|
|
|
}
|
|
|
|
if !strings.Contains(hostPort, ":") {
|
|
|
|
port := "80"
|
|
|
|
if isTLS {
|
|
|
|
port = "443"
|
|
|
|
}
|
|
|
|
hostPort = net.JoinHostPort(hostPort, port)
|
|
|
|
}
|
|
|
|
hc := &fasthttp.HostClient{
|
|
|
|
Addr: hostPort,
|
|
|
|
Name: "vm_promscrape/discovery",
|
|
|
|
DialDualStack: netutil.TCP6Enabled(),
|
|
|
|
IsTLS: isTLS,
|
|
|
|
TLSConfig: tlsCfg,
|
|
|
|
ReadTimeout: time.Minute,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
MaxResponseBodySize: 300 * 1024 * 1024,
|
|
|
|
}
|
2020-04-14 13:19:15 +02:00
|
|
|
return &hcValue{
|
2020-04-13 20:02:27 +02:00
|
|
|
hc: hc,
|
|
|
|
ac: ac,
|
|
|
|
apiServer: apiServer,
|
|
|
|
hostPort: hostPort,
|
|
|
|
}, nil
|
|
|
|
}
|