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. "+
|
2022-08-14 22:40:20 +00:00
|
|
|
"See https://docs.victoriametrics.com/sd_configs.html#kubernetes_sd_configs for details")
|
2021-06-25 09:10:20 +00:00
|
|
|
|
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-08-29 09:34:55 +00:00
|
|
|
APIServer string `yaml:"api_server,omitempty"`
|
|
|
|
|
|
|
|
// Use role() function for accessing the Role field
|
2022-06-01 18:44:45 +00:00
|
|
|
Role string `yaml:"role"`
|
2022-06-06 11:24:52 +00:00
|
|
|
// The filepath to kube config.
|
|
|
|
// If defined any cluster connection information from HTTPClientConfig is ignored.
|
2022-10-28 18:33:07 +00:00
|
|
|
KubeConfigFile string `yaml:"kubeconfig_file,omitempty"`
|
2021-08-29 09:34:55 +00:00
|
|
|
|
2021-04-02 18:17:43 +00:00
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
2021-10-26 18:21:08 +00:00
|
|
|
ProxyURL *proxy.URL `yaml:"proxy_url,omitempty"`
|
2021-04-02 18:17:43 +00:00
|
|
|
Namespaces Namespaces `yaml:"namespaces,omitempty"`
|
|
|
|
Selectors []Selector `yaml:"selectors,omitempty"`
|
2022-04-22 16:39:34 +00:00
|
|
|
AttachMetadata AttachMetadata `yaml:"attach_metadata,omitempty"`
|
2021-04-05 19:02:09 +00:00
|
|
|
|
|
|
|
cfg *apiConfig
|
|
|
|
startErr error
|
2020-04-23 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 09:34:55 +00:00
|
|
|
func (sdc *SDConfig) role() string {
|
|
|
|
if sdc.Role == "endpointslices" {
|
|
|
|
// The endpointslices role isn't supported by Prometheus, but it is used by VictoriaMetrics operator.
|
|
|
|
// Support it for backwards compatibility.
|
|
|
|
return "endpointslice"
|
|
|
|
}
|
|
|
|
return sdc.Role
|
|
|
|
}
|
|
|
|
|
2022-04-22 16:39:34 +00:00
|
|
|
// AttachMetadata represents `attach_metadata` option at `kuberentes_sd_config`.
|
|
|
|
//
|
|
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config
|
|
|
|
type AttachMetadata struct {
|
|
|
|
Node bool `yaml:"node"`
|
|
|
|
}
|
|
|
|
|
2020-04-23 08:34:04 +00:00
|
|
|
// Namespaces represents namespaces for SDConfig
|
|
|
|
type Namespaces struct {
|
2022-01-13 20:44:14 +00:00
|
|
|
OwnNamespace bool `yaml:"own_namespace"`
|
|
|
|
Names []string `yaml:"names"`
|
2020-04-23 08:34:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
//
|
2022-01-13 20:44:14 +00:00
|
|
|
// swcFunc is used for constructing ScrapeWork objects from the given metadata.
|
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
|
|
|
}
|
|
|
|
}
|