-
Notifications
You must be signed in to change notification settings - Fork 2
/
nodes_job.py
67 lines (57 loc) · 2.84 KB
/
nodes_job.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import (Optional)
from .cluster import *
from .prom_dsl import *
class NodesJob(GeneratorJob):
def __init__(self,
scrape_interval: Optional[str] = None,
additional_relabel_configs: Optional[List[Any]] = None,
additional_metric_relabel_configs: Optional[List[Any]] = None):
self.type = 'nodes'
self.scrape_interval = scrape_interval
self.additional_relabel_configs = additional_relabel_configs or []
self.additional_metric_relabel_configs = additional_metric_relabel_configs or []
# Scrape config for nodes (kubelet).
#
# Rather than connecting directly to the node, the scrape is proxied though the
# Kubernetes apiserver. This means it will work if Prometheus is running out of
# cluster, or can't connect to nodes for some other reason (e.g. because of
# firewalling).
def generate(self, prom_conf: Dict[str, Any], cluster: Cluster) -> None:
prom_conf['scrape_configs'].append({
'job_name': f'{cluster.name}-kubernetes-nodes',
'scheme': 'https',
'kubernetes_sd_configs': [
cluster.get_kubernetes_sd_config('node')
],
# This TLS & bearer token file config is used to connect to the actual scrape
# endpoints for cluster components. This is separate to discovery auth
# configuration because discovery & scraping are two separate concerns in
# Prometheus. The discovery auth config is automatic if Prometheus runs inside
# the cluster. Otherwise, more config options have to be provided within the
# <kubernetes_sd_config>.
'tls_config': {
'ca_file': cluster.ca_file
},
'bearer_token_file': cluster.bearer_token_file,
# Keep all node labels without their prefix, proxy against apiserver with
# HTTPS by default
'relabel_configs': [
remove_label('__meta_kubernetes_node_label_node_role_kubernetes_io_node'),
remove_label('__meta_kubernetes_node_label_node_role_kubernetes_io_master'),
labelmap(regex='__meta_kubernetes_node_label_(.+)'),
copy_value('__address__', 'instance'),
set_value('__address__', f'{cluster.api_server}:443'),
replace(source_labels=['__meta_kubernetes_node_name'],
regex='(.+)', replacement='/api/v1/nodes/${1}:10255/proxy/metrics',
target_label='__metrics_path__')
],
'metric_relabel_configs': [
drop(source_labels=['__name__'], regex='go_.*')
]
}) # yapf: disable
# set job's scrape_interval if defined
if not self.scrape_interval is None:
prom_conf['scrape_configs'][-1]['scrape_interval'] = self.scrape_interval
# add additional configs
prom_conf['scrape_configs'][-1]['relabel_configs'].extend(self.additional_relabel_configs)
prom_conf['scrape_configs'][-1]['metric_relabel_configs'].extend(self.additional_metric_relabel_configs)