2020-04-22 19:16:01 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2021-06-25 09:10:20 +00:00
|
|
|
"flag"
|
2020-04-22 19:16:01 +00:00
|
|
|
"fmt"
|
2021-06-25 09:10:20 +00:00
|
|
|
"time"
|
2020-04-23 08:34:04 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2020-12-24 08:56:10 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
2020-04-22 19:16:01 +00:00
|
|
|
)
|
|
|
|
|
2021-06-25 09:10:20 +00:00
|
|
|
// SDCheckInterval defines interval for targets refresh.
|
|
|
|
var SDCheckInterval = flag.Duration("promscrape.kubernetesSDCheckInterval", 30*time.Second, "Interval for checking for changes in Kubernetes API server. "+
|
|
|
|
"This works only if kubernetes_sd_configs is configured in '-promscrape.config' file. "+
|
|
|
|
"See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config for details")
|
|
|
|
|
2020-04-23 08:34:04 +00:00
|
|
|
// SDConfig represents kubernetes-based service discovery config.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config
|
|
|
|
type SDConfig struct {
|
2021-04-02 18:17:43 +00:00
|
|
|
APIServer string `yaml:"api_server,omitempty"`
|
|
|
|
Role string `yaml:"role"`
|
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
|
|
|
ProxyURL proxy.URL `yaml:"proxy_url,omitempty"`
|
|
|
|
Namespaces Namespaces `yaml:"namespaces,omitempty"`
|
|
|
|
Selectors []Selector `yaml:"selectors,omitempty"`
|
2021-04-05 19:02:09 +00:00
|
|
|
|
|
|
|
cfg *apiConfig
|
|
|
|
startErr error
|
2020-04-23 08:34:04 +00: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 14:50:21 +00:00
|
|
|
|
2021-03-02 14:42:48 +00:00
|
|
|
// ScrapeWorkConstructorFunc must construct ScrapeWork object for the given metaLabels.
|
|
|
|
type ScrapeWorkConstructorFunc func(metaLabels map[string]string) interface{}
|
|
|
|
|
2021-04-05 19:02:09 +00:00
|
|
|
// GetScrapeWorkObjects returns ScrapeWork objects for the given sdc.
|
|
|
|
//
|
|
|
|
// This function must be called after MustStart call.
|
|
|
|
func (sdc *SDConfig) GetScrapeWorkObjects() ([]interface{}, error) {
|
|
|
|
if sdc.cfg == nil {
|
|
|
|
return nil, sdc.startErr
|
|
|
|
}
|
|
|
|
return sdc.cfg.aw.getScrapeWorkObjects(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustStart initializes sdc before its usage.
|
2021-03-02 14:42:48 +00:00
|
|
|
//
|
|
|
|
// swcFunc is used for constructing such objects.
|
2021-04-05 19:02:09 +00:00
|
|
|
func (sdc *SDConfig) MustStart(baseDir string, swcFunc ScrapeWorkConstructorFunc) {
|
|
|
|
cfg, err := newAPIConfig(sdc, baseDir, swcFunc)
|
2020-05-04 12:53:50 +00:00
|
|
|
if err != nil {
|
2021-04-05 19:02:09 +00:00
|
|
|
sdc.startErr = fmt.Errorf("cannot create API config for kubernetes: %w", err)
|
|
|
|
return
|
2020-04-24 14:50:21 +00:00
|
|
|
}
|
2021-04-05 19:02:09 +00:00
|
|
|
cfg.aw.mustStart()
|
|
|
|
sdc.cfg = cfg
|
2020-04-24 14:50:21 +00:00
|
|
|
}
|
2021-03-01 12:13:56 +00:00
|
|
|
|
|
|
|
// MustStop stops further usage for sdc.
|
|
|
|
func (sdc *SDConfig) MustStop() {
|
2021-04-05 19:02:09 +00:00
|
|
|
if sdc.cfg != nil {
|
|
|
|
// sdc.cfg can be nil on MustStart error.
|
2021-04-05 19:41:48 +00:00
|
|
|
sdc.cfg.aw.mustStop()
|
2021-03-01 12:13:56 +00:00
|
|
|
}
|
|
|
|
}
|