2020-04-22 21:16:01 +02:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2021-06-25 11:10:20 +02:00
|
|
|
"flag"
|
2020-04-22 21:16:01 +02:00
|
|
|
"fmt"
|
2021-06-25 11:10:20 +02:00
|
|
|
"time"
|
2020-04-23 10:34:04 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
2022-11-30 06:22:12 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
2020-12-24 09:56:10 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
2020-04-22 21:16:01 +02:00
|
|
|
)
|
|
|
|
|
2021-06-25 11:10:20 +02: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-15 00:40:20 +02:00
|
|
|
"See https://docs.victoriametrics.com/sd_configs.html#kubernetes_sd_configs for details")
|
2021-06-25 11:10:20 +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 {
|
2021-08-29 11:34:55 +02:00
|
|
|
APIServer string `yaml:"api_server,omitempty"`
|
|
|
|
|
|
|
|
// Use role() function for accessing the Role field
|
2022-06-01 20:44:45 +02:00
|
|
|
Role string `yaml:"role"`
|
2022-06-06 13:24:52 +02:00
|
|
|
// The filepath to kube config.
|
|
|
|
// If defined any cluster connection information from HTTPClientConfig is ignored.
|
2022-10-28 20:33:07 +02:00
|
|
|
KubeConfigFile string `yaml:"kubeconfig_file,omitempty"`
|
2021-08-29 11:34:55 +02:00
|
|
|
|
2021-04-02 20:17:43 +02:00
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
2021-10-26 20:21:08 +02:00
|
|
|
ProxyURL *proxy.URL `yaml:"proxy_url,omitempty"`
|
2021-04-02 20:17:43 +02:00
|
|
|
Namespaces Namespaces `yaml:"namespaces,omitempty"`
|
|
|
|
Selectors []Selector `yaml:"selectors,omitempty"`
|
2022-04-22 18:39:34 +02:00
|
|
|
AttachMetadata AttachMetadata `yaml:"attach_metadata,omitempty"`
|
2021-04-05 21:02:09 +02:00
|
|
|
|
|
|
|
cfg *apiConfig
|
|
|
|
startErr error
|
2020-04-23 10:34:04 +02:00
|
|
|
}
|
|
|
|
|
2021-08-29 11:34:55 +02: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 18:39:34 +02: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 10:34:04 +02:00
|
|
|
// Namespaces represents namespaces for SDConfig
|
|
|
|
type Namespaces struct {
|
2022-01-13 21:44:14 +01:00
|
|
|
OwnNamespace bool `yaml:"own_namespace"`
|
|
|
|
Names []string `yaml:"names"`
|
2020-04-23 10:34:04 +02: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 16:50:21 +02:00
|
|
|
|
2021-03-02 15:42:48 +01:00
|
|
|
// ScrapeWorkConstructorFunc must construct ScrapeWork object for the given metaLabels.
|
2022-11-30 06:22:12 +01:00
|
|
|
type ScrapeWorkConstructorFunc func(metaLabels *promutils.Labels) interface{}
|
2021-03-02 15:42:48 +01:00
|
|
|
|
2021-04-05 21:02:09 +02: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 15:42:48 +01:00
|
|
|
//
|
2022-01-13 21:44:14 +01:00
|
|
|
// swcFunc is used for constructing ScrapeWork objects from the given metadata.
|
2021-04-05 21:02:09 +02:00
|
|
|
func (sdc *SDConfig) MustStart(baseDir string, swcFunc ScrapeWorkConstructorFunc) {
|
|
|
|
cfg, err := newAPIConfig(sdc, baseDir, swcFunc)
|
2020-05-04 14:53:50 +02:00
|
|
|
if err != nil {
|
2021-04-05 21:02:09 +02:00
|
|
|
sdc.startErr = fmt.Errorf("cannot create API config for kubernetes: %w", err)
|
|
|
|
return
|
2020-04-24 16:50:21 +02:00
|
|
|
}
|
2021-04-05 21:02:09 +02:00
|
|
|
cfg.aw.mustStart()
|
|
|
|
sdc.cfg = cfg
|
2020-04-24 16:50:21 +02:00
|
|
|
}
|
2021-03-01 13:13:56 +01:00
|
|
|
|
|
|
|
// MustStop stops further usage for sdc.
|
|
|
|
func (sdc *SDConfig) MustStop() {
|
2021-04-05 21:02:09 +02:00
|
|
|
if sdc.cfg != nil {
|
|
|
|
// sdc.cfg can be nil on MustStart error.
|
2021-04-05 21:41:48 +02:00
|
|
|
sdc.cfg.aw.mustStop()
|
2021-03-01 13:13:56 +01:00
|
|
|
}
|
|
|
|
}
|