mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-05 22:32:20 +01:00
lib/promscrape/discovery/kubernetes: hide role switch logic behind GetLabels function
This commit is contained in:
parent
f9526809e5
commit
1187494c8f
@ -311,46 +311,12 @@ func (sdc *KubernetesSDConfig) appendScrapeWork(dst []ScrapeWork, baseDir string
|
|||||||
Namespaces: sdc.Namespaces.Names,
|
Namespaces: sdc.Namespaces.Names,
|
||||||
Selectors: sdc.Selectors,
|
Selectors: sdc.Selectors,
|
||||||
}
|
}
|
||||||
switch sdc.Role {
|
targetLabels, err := kubernetes.GetLabels(cfg, sdc.Role)
|
||||||
case "node":
|
if err != nil {
|
||||||
targetLabels, err := kubernetes.GetNodesLabels(cfg)
|
logger.Errorf("error when discovering kubernetes nodes for `job_name` %q: %s; skipping it", swc.jobName, err)
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("error when discovering kubernetes nodes for `job_name` %q: %s; skipping it", swc.jobName, err)
|
|
||||||
return dst
|
|
||||||
}
|
|
||||||
return appendKubernetesScrapeWork(dst, swc, targetLabels, sdc.Role)
|
|
||||||
case "service":
|
|
||||||
targetLabels, err := kubernetes.GetServicesLabels(cfg)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("error when discovering kubernetes services for `job_name` %q: %s; skipping it", swc.jobName, err)
|
|
||||||
return dst
|
|
||||||
}
|
|
||||||
return appendKubernetesScrapeWork(dst, swc, targetLabels, sdc.Role)
|
|
||||||
case "pod":
|
|
||||||
targetLabels, err := kubernetes.GetPodsLabels(cfg)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("error when discovering kubernetes pods for `job_name` %q: %s; skipping it", swc.jobName, err)
|
|
||||||
return dst
|
|
||||||
}
|
|
||||||
return appendKubernetesScrapeWork(dst, swc, targetLabels, sdc.Role)
|
|
||||||
case "endpoints":
|
|
||||||
targetLabels, err := kubernetes.GetEndpointsLabels(cfg)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("error when discovering kubernetes endpoints for `job_name` %q: %s; skipping it", swc.jobName, err)
|
|
||||||
return dst
|
|
||||||
}
|
|
||||||
return appendKubernetesScrapeWork(dst, swc, targetLabels, sdc.Role)
|
|
||||||
case "ingress":
|
|
||||||
targetLabels, err := kubernetes.GetIngressesLabels(cfg)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf("error when discovering kubernetes ingresses for `job_name` %q: %s; skipping it", swc.jobName, err)
|
|
||||||
return dst
|
|
||||||
}
|
|
||||||
return appendKubernetesScrapeWork(dst, swc, targetLabels, sdc.Role)
|
|
||||||
default:
|
|
||||||
logger.Errorf("unexpected `role`: %q; must be one of `node`, `service`, `pod`, `endpoints` or `ingress`; skipping it", sdc.Role)
|
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
return appendKubernetesScrapeWork(dst, swc, targetLabels, sdc.Role)
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendKubernetesScrapeWork(dst []ScrapeWork, swc *scrapeWorkConfig, targetLabels []map[string]string, role string) []ScrapeWork {
|
func appendKubernetesScrapeWork(dst []ScrapeWork, swc *scrapeWorkConfig, targetLabels []map[string]string, role string) []ScrapeWork {
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetEndpointsLabels returns labels for k8s endpoints obtained from the given cfg.
|
// getEndpointsLabels returns labels for k8s endpoints obtained from the given cfg.
|
||||||
func GetEndpointsLabels(cfg *APIConfig) ([]map[string]string, error) {
|
func getEndpointsLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||||
data, err := getAPIResponse(cfg, "endpoints", "/api/v1/endpoints")
|
data, err := getAPIResponse(cfg, "endpoints", "/api/v1/endpoints")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot obtain endpoints data from API server: %s", err)
|
return nil, fmt.Errorf("cannot obtain endpoints data from API server: %s", err)
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetIngressesLabels returns labels for k8s ingresses obtained from the given cfg.
|
// getIngressesLabels returns labels for k8s ingresses obtained from the given cfg.
|
||||||
func GetIngressesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
func getIngressesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||||
data, err := getAPIResponse(cfg, "ingress", "/apis/extensions/v1beta1/ingresses")
|
data, err := getAPIResponse(cfg, "ingress", "/apis/extensions/v1beta1/ingresses")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot obtain ingresses data from API server: %s", err)
|
return nil, fmt.Errorf("cannot obtain ingresses data from API server: %s", err)
|
||||||
|
23
lib/promscrape/discovery/kubernetes/kubernetes.go
Normal file
23
lib/promscrape/discovery/kubernetes/kubernetes.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package kubernetes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetLabels returns labels for the given k8s role and the given cfg.
|
||||||
|
func GetLabels(cfg *APIConfig, role string) ([]map[string]string, error) {
|
||||||
|
switch 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", role)
|
||||||
|
}
|
||||||
|
}
|
@ -5,8 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetNodesLabels returns labels for k8s nodes obtained from the given cfg.
|
// getNodesLabels returns labels for k8s nodes obtained from the given cfg.
|
||||||
func GetNodesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
func getNodesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||||
data, err := getAPIResponse(cfg, "node", "/api/v1/nodes")
|
data, err := getAPIResponse(cfg, "node", "/api/v1/nodes")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot obtain nodes data from API server: %s", err)
|
return nil, fmt.Errorf("cannot obtain nodes data from API server: %s", err)
|
||||||
|
@ -7,8 +7,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetPodsLabels returns labels for k8s pods obtained from the given cfg
|
// getPodsLabels returns labels for k8s pods obtained from the given cfg
|
||||||
func GetPodsLabels(cfg *APIConfig) ([]map[string]string, error) {
|
func getPodsLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||||
pods, err := getPods(cfg)
|
pods, err := getPods(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetServicesLabels returns labels for k8s services obtained from the given cfg.
|
// getServicesLabels returns labels for k8s services obtained from the given cfg.
|
||||||
func GetServicesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
func getServicesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||||
svcs, err := getServices(cfg)
|
svcs, err := getServices(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user