2020-04-13 18:02:27 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-05-04 13:21:24 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutils"
|
2020-04-13 18:02:27 +00:00
|
|
|
)
|
|
|
|
|
2020-04-23 08:34:04 +00:00
|
|
|
// apiConfig contains config for API server
|
|
|
|
type apiConfig struct {
|
2020-05-04 17:48:02 +00:00
|
|
|
client *discoveryutils.Client
|
2020-05-04 12:53:50 +00:00
|
|
|
namespaces []string
|
|
|
|
selectors []Selector
|
2020-04-23 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:21:24 +00:00
|
|
|
var configMap = discoveryutils.NewConfigMap()
|
2020-05-04 12:53:50 +00:00
|
|
|
|
2020-05-04 13:21:24 +00:00
|
|
|
func getAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) {
|
|
|
|
v, err := configMap.Get(sdc, func() (interface{}, error) { return newAPIConfig(sdc, baseDir) })
|
2020-04-13 18:02:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-04 13:21:24 +00:00
|
|
|
return v.(*apiConfig), nil
|
2020-05-04 12:53:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newAPIConfig(sdc *SDConfig, baseDir string) (*apiConfig, error) {
|
|
|
|
ac, err := promauth.NewConfig(baseDir, sdc.BasicAuth, sdc.BearerToken, sdc.BearerTokenFile, 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 12:53:50 +00:00
|
|
|
}
|
2020-05-04 17:48:02 +00:00
|
|
|
apiServer := sdc.APIServer
|
2020-04-13 18:02:27 +00: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
|
2020-04-23 08:34:04 +00:00
|
|
|
host := os.Getenv("KUBERNETES_SERVICE_HOST")
|
|
|
|
port := os.Getenv("KUBERNETES_SERVICE_PORT")
|
2020-04-13 18:02:27 +00:00
|
|
|
if len(host) == 0 {
|
2020-04-14 11:19:15 +00:00
|
|
|
return nil, fmt.Errorf("cannot find KUBERNETES_SERVICE_HOST env var; it must be defined when running in k8s; " +
|
2020-04-13 18:02:27 +00:00
|
|
|
"probably, `kubernetes_sd_config->api_server` is missing in Prometheus configs?")
|
|
|
|
}
|
|
|
|
if len(port) == 0 {
|
2020-04-14 11:19:15 +00:00
|
|
|
return nil, fmt.Errorf("cannot find KUBERNETES_SERVICE_PORT env var; it must be defined when running in k8s; "+
|
2020-04-13 18:02:27 +00:00
|
|
|
"KUBERNETES_SERVICE_HOST=%q", host)
|
|
|
|
}
|
|
|
|
apiServer = "https://" + net.JoinHostPort(host, port)
|
|
|
|
tlsConfig := promauth.TLSConfig{
|
|
|
|
CAFile: "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt",
|
|
|
|
}
|
2020-05-12 14:20:55 +00:00
|
|
|
acNew, err := promauth.NewConfig(".", nil, "", "/var/run/secrets/kubernetes.io/serviceaccount/token", &tlsConfig)
|
2020-04-13 18:02:27 +00:00
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return nil, fmt.Errorf("cannot initialize service account auth: %w; probably, `kubernetes_sd_config->api_server` is missing in Prometheus configs?", err)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
|
|
|
ac = acNew
|
|
|
|
}
|
2020-12-24 08:52:37 +00:00
|
|
|
client, err := discoveryutils.NewClient(apiServer, ac, sdc.ProxyURL.URL())
|
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-04-13 18:02:27 +00:00
|
|
|
}
|
2020-05-04 17:48:02 +00:00
|
|
|
cfg := &apiConfig{
|
|
|
|
client: client,
|
|
|
|
namespaces: sdc.Namespaces.Names,
|
|
|
|
selectors: sdc.Selectors,
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
2020-05-04 17:48:02 +00:00
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAPIResponse(cfg *apiConfig, role, path string) ([]byte, error) {
|
|
|
|
query := joinSelectors(role, cfg.namespaces, cfg.selectors)
|
|
|
|
if len(query) > 0 {
|
|
|
|
path += "?" + query
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|
2020-05-04 17:48:02 +00:00
|
|
|
return cfg.client.GetAPIResponse(path)
|
2020-04-13 18:02:27 +00:00
|
|
|
}
|