diff --git a/lib/promscrape/config.go b/lib/promscrape/config.go index 29e8e2da37..d5af8fef09 100644 --- a/lib/promscrape/config.go +++ b/lib/promscrape/config.go @@ -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 { diff --git a/lib/promscrape/discovery/kubernetes/endpoints.go b/lib/promscrape/discovery/kubernetes/endpoints.go index 43a79daf29..e4a358ff25 100644 --- a/lib/promscrape/discovery/kubernetes/endpoints.go +++ b/lib/promscrape/discovery/kubernetes/endpoints.go @@ -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) diff --git a/lib/promscrape/discovery/kubernetes/ingress.go b/lib/promscrape/discovery/kubernetes/ingress.go index 670b41b57b..696d485376 100644 --- a/lib/promscrape/discovery/kubernetes/ingress.go +++ b/lib/promscrape/discovery/kubernetes/ingress.go @@ -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) diff --git a/lib/promscrape/discovery/kubernetes/kubernetes.go b/lib/promscrape/discovery/kubernetes/kubernetes.go new file mode 100644 index 0000000000..11dbcb1847 --- /dev/null +++ b/lib/promscrape/discovery/kubernetes/kubernetes.go @@ -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) + } +} diff --git a/lib/promscrape/discovery/kubernetes/node.go b/lib/promscrape/discovery/kubernetes/node.go index bb433cfa1b..8d2f43d1ea 100644 --- a/lib/promscrape/discovery/kubernetes/node.go +++ b/lib/promscrape/discovery/kubernetes/node.go @@ -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) diff --git a/lib/promscrape/discovery/kubernetes/pod.go b/lib/promscrape/discovery/kubernetes/pod.go index 8021f61012..2980b747f1 100644 --- a/lib/promscrape/discovery/kubernetes/pod.go +++ b/lib/promscrape/discovery/kubernetes/pod.go @@ -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 diff --git a/lib/promscrape/discovery/kubernetes/service.go b/lib/promscrape/discovery/kubernetes/service.go index 26054135b2..59710ee378 100644 --- a/lib/promscrape/discovery/kubernetes/service.go +++ b/lib/promscrape/discovery/kubernetes/service.go @@ -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