mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-28 18:05:24 +01:00
61 lines
1.9 KiB
Go
61 lines
1.9 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
|
|
)
|
|
|
|
// GetLabels returns labels for the given k8s role and the given cfg.
|
|
func GetLabels(ac *promauth.Config, sdc *SDConfig) ([]map[string]string, error) {
|
|
cfg := &apiConfig{
|
|
Server: sdc.APIServer,
|
|
AuthConfig: ac,
|
|
Namespaces: sdc.Namespaces.Names,
|
|
Selectors: sdc.Selectors,
|
|
}
|
|
switch sdc.Role {
|
|
case "node":
|
|
return getNodesLabels(cfg)
|
|
case "service":
|
|
return getServicesLabels(cfg)
|
|
case "pod":
|
|
return getPodsLabels(cfg)
|
|
case "endpoints":
|
|
return getEndpointsLabels(cfg)
|
|
case "ingress":
|
|
return getIngressesLabels(cfg)
|
|
default:
|
|
return nil, fmt.Errorf("unexpected `role`: %q; must be one of `node`, `service`, `pod`, `endpoints` or `ingress`; skipping it", sdc.Role)
|
|
}
|
|
}
|
|
|
|
// 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"`
|
|
Role string `yaml:"role"`
|
|
BasicAuth *promauth.BasicAuthConfig `yaml:"basic_auth"`
|
|
BearerToken string `yaml:"bearer_token"`
|
|
BearerTokenFile string `yaml:"bearer_token_file"`
|
|
TLSConfig *promauth.TLSConfig `yaml:"tls_config"`
|
|
Namespaces Namespaces `yaml:"namespaces"`
|
|
Selectors []Selector `yaml:"selectors"`
|
|
}
|
|
|
|
// 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"`
|
|
}
|