2020-04-22 21:16:01 +02:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-23 10:34:04 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-12-24 09:56:10 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
2020-04-22 21:16:01 +02:00
|
|
|
)
|
|
|
|
|
2020-04-23 10:34:04 +02:00
|
|
|
// SDConfig represents kubernetes-based service discovery config.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config
|
|
|
|
type SDConfig struct {
|
2020-11-13 15:17:03 +01:00
|
|
|
APIServer string `yaml:"api_server,omitempty"`
|
2020-04-23 10:34:04 +02:00
|
|
|
Role string `yaml:"role"`
|
2020-11-13 15:17:03 +01:00
|
|
|
BasicAuth *promauth.BasicAuthConfig `yaml:"basic_auth,omitempty"`
|
|
|
|
BearerToken string `yaml:"bearer_token,omitempty"`
|
|
|
|
BearerTokenFile string `yaml:"bearer_token_file,omitempty"`
|
2020-12-24 09:56:10 +01:00
|
|
|
ProxyURL proxy.URL `yaml:"proxy_url,omitempty"`
|
2020-11-13 15:17:03 +01:00
|
|
|
TLSConfig *promauth.TLSConfig `yaml:"tls_config,omitempty"`
|
|
|
|
Namespaces Namespaces `yaml:"namespaces,omitempty"`
|
|
|
|
Selectors []Selector `yaml:"selectors,omitempty"`
|
2020-04-23 10:34:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Namespaces represents namespaces for SDConfig
|
|
|
|
type Namespaces struct {
|
|
|
|
Names []string `yaml:"names"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Selector represents kubernetes selector.
|
|
|
|
//
|
|
|
|
// See https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/
|
|
|
|
// and https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
|
|
|
|
type Selector struct {
|
|
|
|
Role string `yaml:"role"`
|
|
|
|
Label string `yaml:"label"`
|
|
|
|
Field string `yaml:"field"`
|
|
|
|
}
|
2020-04-24 16:50:21 +02:00
|
|
|
|
2021-02-26 15:46:13 +01:00
|
|
|
// StartWatchOnce returns init labels for the given sdc and baseDir.
|
|
|
|
// and starts watching for changes.
|
|
|
|
func StartWatchOnce(watchCfg *WatchConfig, setName string, sdc *SDConfig, baseDir string) ([]map[string]string, error) {
|
|
|
|
cfg, err := getAPIConfig(watchCfg, setName, sdc, baseDir)
|
2020-05-04 14:53:50 +02:00
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("cannot create API config: %w", err)
|
2020-04-24 16:50:21 +02:00
|
|
|
}
|
2021-02-26 15:46:13 +01:00
|
|
|
var ms []map[string]string
|
|
|
|
cfg.watchOnce.Do(func() {
|
|
|
|
ms = startWatcherByRole(watchCfg.Ctx, sdc.Role, cfg, watchCfg.SC)
|
|
|
|
})
|
|
|
|
return ms, nil
|
2020-04-24 16:50:21 +02:00
|
|
|
}
|