mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-25 06:17:36 +01:00
3c02937a34
'any' type is supported starting from Go1.18. Let's consistently use it instead of 'interface{}' type across the code base, since `any` is easier to read than 'interface{}'.
105 lines
3.5 KiB
Go
105 lines
3.5 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promutils"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/proxy"
|
|
)
|
|
|
|
// 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://docs.victoriametrics.com/sd_configs/#kubernetes_sd_configs for details")
|
|
|
|
// SDConfig represents kubernetes-based service discovery config.
|
|
//
|
|
// See https://prometheus.io/docs/prometheus/latest/configuration/configuration/#kubernetes_sd_config
|
|
type SDConfig struct {
|
|
APIServer string `yaml:"api_server,omitempty"`
|
|
|
|
// Use role() function for accessing the Role field
|
|
Role string `yaml:"role"`
|
|
// The filepath to kube config.
|
|
// If defined any cluster connection information from HTTPClientConfig is ignored.
|
|
KubeConfigFile string `yaml:"kubeconfig_file,omitempty"`
|
|
|
|
HTTPClientConfig promauth.HTTPClientConfig `yaml:",inline"`
|
|
ProxyURL *proxy.URL `yaml:"proxy_url,omitempty"`
|
|
Namespaces Namespaces `yaml:"namespaces,omitempty"`
|
|
Selectors []Selector `yaml:"selectors,omitempty"`
|
|
AttachMetadata *AttachMetadata `yaml:"attach_metadata,omitempty"`
|
|
|
|
cfg *apiConfig
|
|
startErr error
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// 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"`
|
|
}
|
|
|
|
// Namespaces represents namespaces for SDConfig
|
|
type Namespaces struct {
|
|
OwnNamespace bool `yaml:"own_namespace"`
|
|
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"`
|
|
}
|
|
|
|
// ScrapeWorkConstructorFunc must construct ScrapeWork object for the given metaLabels.
|
|
type ScrapeWorkConstructorFunc func(metaLabels *promutils.Labels) any
|
|
|
|
// GetScrapeWorkObjects returns ScrapeWork objects for the given sdc.
|
|
//
|
|
// This function must be called after MustStart call.
|
|
func (sdc *SDConfig) GetScrapeWorkObjects() ([]any, error) {
|
|
if sdc.cfg == nil {
|
|
return nil, sdc.startErr
|
|
}
|
|
return sdc.cfg.aw.getScrapeWorkObjects(), nil
|
|
}
|
|
|
|
// MustStart initializes sdc before its usage.
|
|
//
|
|
// swcFunc is used for constructing ScrapeWork objects from the given metadata.
|
|
func (sdc *SDConfig) MustStart(baseDir string, swcFunc ScrapeWorkConstructorFunc) {
|
|
cfg, err := newAPIConfig(sdc, baseDir, swcFunc)
|
|
if err != nil {
|
|
sdc.startErr = fmt.Errorf("cannot create API config for kubernetes: %w", err)
|
|
return
|
|
}
|
|
cfg.aw.mustStart()
|
|
sdc.cfg = cfg
|
|
}
|
|
|
|
// MustStop stops further usage for sdc.
|
|
func (sdc *SDConfig) MustStop() {
|
|
if sdc.cfg != nil {
|
|
// sdc.cfg can be nil on MustStart error.
|
|
sdc.cfg.aw.mustStop()
|
|
}
|
|
}
|