mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/promscrape/discovery/kubernetes: hide role switch logic behind GetLabels function
This commit is contained in:
parent
f9526809e5
commit
1187494c8f
7 changed files with 37 additions and 48 deletions
|
@ -311,46 +311,12 @@ func (sdc *KubernetesSDConfig) appendScrapeWork(dst []ScrapeWork, baseDir string
|
|||
Namespaces: sdc.Namespaces.Names,
|
||||
Selectors: sdc.Selectors,
|
||||
}
|
||||
switch sdc.Role {
|
||||
case "node":
|
||||
targetLabels, err := kubernetes.GetNodesLabels(cfg)
|
||||
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)
|
||||
targetLabels, err := kubernetes.GetLabels(cfg, sdc.Role)
|
||||
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)
|
||||
}
|
||||
|
||||
func appendKubernetesScrapeWork(dst []ScrapeWork, swc *scrapeWorkConfig, targetLabels []map[string]string, role string) []ScrapeWork {
|
||||
|
|
|
@ -5,8 +5,8 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
// GetEndpointsLabels returns labels for k8s endpoints obtained from the given cfg.
|
||||
func GetEndpointsLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||
// getEndpointsLabels returns labels for k8s endpoints obtained from the given cfg.
|
||||
func getEndpointsLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||
data, err := getAPIResponse(cfg, "endpoints", "/api/v1/endpoints")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot obtain endpoints data from API server: %s", err)
|
||||
|
|
|
@ -5,8 +5,8 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
// GetIngressesLabels returns labels for k8s ingresses obtained from the given cfg.
|
||||
func GetIngressesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||
// getIngressesLabels returns labels for k8s ingresses obtained from the given cfg.
|
||||
func getIngressesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||
data, err := getAPIResponse(cfg, "ingress", "/apis/extensions/v1beta1/ingresses")
|
||||
if err != nil {
|
||||
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"
|
||||
)
|
||||
|
||||
// GetNodesLabels returns labels for k8s nodes obtained from the given cfg.
|
||||
func GetNodesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||
// getNodesLabels returns labels for k8s nodes obtained from the given cfg.
|
||||
func getNodesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||
data, err := getAPIResponse(cfg, "node", "/api/v1/nodes")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot obtain nodes data from API server: %s", err)
|
||||
|
|
|
@ -7,8 +7,8 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
// GetPodsLabels returns labels for k8s pods obtained from the given cfg
|
||||
func GetPodsLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||
// getPodsLabels returns labels for k8s pods obtained from the given cfg
|
||||
func getPodsLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||
pods, err := getPods(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -5,8 +5,8 @@ import (
|
|||
"fmt"
|
||||
)
|
||||
|
||||
// GetServicesLabels returns labels for k8s services obtained from the given cfg.
|
||||
func GetServicesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||
// getServicesLabels returns labels for k8s services obtained from the given cfg.
|
||||
func getServicesLabels(cfg *APIConfig) ([]map[string]string, error) {
|
||||
svcs, err := getServices(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in a new issue