Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kubernetes] add namespace tag to kubernetes.pods.running #2931

Merged
merged 2 commits into from Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions checks.d/kubernetes.py
Expand Up @@ -368,21 +368,24 @@ def _update_pods_metrics(self, instance, pods):
"ReplicaSet",
]

# (create-by, namespace): count
controllers_map = defaultdict(int)
for pod in pods['items']:
try:
created_by = json.loads(pod['metadata']['annotations']['kubernetes.io/created-by'])
kind = created_by['reference']['kind']
if kind in supported_kinds:
controllers_map[created_by['reference']['name']] += 1
namespace = created_by['reference']['namespace']
controllers_map[(created_by['reference']['name'], namespace)] += 1
except (KeyError, ValueError) as e:
self.log.debug("Unable to retrieve pod kind for pod %s: %s", pod, e)
continue

tags = instance.get('tags', [])
for ctrl, pod_count in controllers_map.iteritems():
for (ctrl, namespace), pod_count in controllers_map.iteritems():
_tags = tags[:] # copy base tags
_tags.append('kube_replication_controller:{0}'.format(ctrl))
_tags.append('kube_namespace:{0}'.format(namespace))
self.publish_gauge(self, NAMESPACE + '.pods.running', pod_count, _tags)

def _process_events(self, instance, pods_list):
Expand Down
20 changes: 10 additions & 10 deletions tests/checks/mock/test_kubernetes.py
Expand Up @@ -113,10 +113,10 @@ def test_metrics_1_1(self, *args):
(['kube_replication_controller:kube-dns-v8','kube_namespace:kube-system', 'container_name:k8s_healthz.4469a25d_kube-dns-v8-smhcb_kube-system_b80ffab3-3619-11e5-84ce-42010af01c62_241c34d1', 'pod_name:kube-system/kube-dns-v8-smhcb'], [MEM, CPU, FS, NET, NET_ERRORS, DISK]),
(['kube_replication_controller:fluentd-cloud-logging-kubernetes-minion','kube_namespace:kube-system', 'container_name:k8s_fluentd-cloud-logging.7721935b_fluentd-cloud-logging-kubernetes-minion-mu4w_kube-system_d0feac1ad02da9e97c4bf67970ece7a1_2c3c0879', 'pod_name:kube-system/fluentd-cloud-logging-kubernetes-minion-mu4w'], [MEM, CPU, FS, NET, NET_ERRORS, DISK]),
(['container_name:dd-agent', 'pod_name:no_pod'], [MEM, CPU, FS, NET, NET_ERRORS, DISK]),
(['kube_replication_controller:l7-lb-controller'], [PODS]),
(['kube_replication_controller:redis-slave'], [PODS]),
(['kube_replication_controller:frontend'], [PODS]),
(['kube_replication_controller:heapster-v11'], [PODS]),
(['kube_replication_controller:l7-lb-controller', 'kube_namespace:kube-system'], [PODS]),
(['kube_replication_controller:redis-slave', 'kube_namespace:default'], [PODS]),
(['kube_replication_controller:frontend', 'kube_namespace:default'], [PODS]),
(['kube_replication_controller:heapster-v11', 'kube_namespace:kube-system'], [PODS]),
([], [LIM, REQ, CAP]) # container from kubernetes api doesn't have a corresponding entry in Cadvisor
]
for m, _type in METRICS:
Expand Down Expand Up @@ -161,10 +161,10 @@ def test_historate_1_1(self, *args):
(['kube_replication_controller:kube-ui-v1','kube_namespace:kube-system', 'pod_name:kube-system/kube-ui-v1-sv2sq'], [MEM, CPU, FS, NET, NET_ERRORS]),
(['kube_replication_controller:propjoe', 'kube_namespace:default', 'pod_name:default/propjoe-lkc3l'], [MEM, CPU, FS, NET, NET_ERRORS]),
(['kube_replication_controller:haproxy-6db79c7bbcac01601ac35bcdb18868b3', 'kube_namespace:default', 'pod_name:default/haproxy-6db79c7bbcac01601ac35bcdb18868b3-rr7la'], [MEM, CPU, FS, NET, NET_ERRORS]),
(['kube_replication_controller:l7-lb-controller'], [PODS]),
(['kube_replication_controller:redis-slave'], [PODS]),
(['kube_replication_controller:frontend'], [PODS]),
(['kube_replication_controller:heapster-v11'], [PODS]),
(['kube_replication_controller:l7-lb-controller', 'kube_namespace:kube-system'], [PODS]),
(['kube_replication_controller:redis-slave', 'kube_namespace:default'], [PODS]),
(['kube_replication_controller:frontend', 'kube_namespace:default'], [PODS]),
(['kube_replication_controller:heapster-v11', 'kube_namespace:kube-system'], [PODS]),
([], [LIM, REQ, CAP]) # container from kubernetes api doesn't have a corresponding entry in Cadvisor
]

Expand Down Expand Up @@ -225,7 +225,7 @@ def test_metrics_1_2(self, *args):
(['container_name:k8s_dd-agent.7b520f3f_dd-agent-1rxlh_default_12c7be82-33ca-11e6-ac8f-42010af00003_321fecb4',
'pod_name:default/dd-agent-1rxlh', 'kube_namespace:default', 'kube_app:dd-agent', 'kube_foo:bar',
'kube_bar:baz', 'kube_replication_controller:dd-agent'], [LIM, REQ, MEM, CPU, NET, DISK, DISK_USAGE]),
(['kube_replication_controller:dd-agent'], [PODS]),
(['kube_replication_controller:dd-agent', 'kube_namespace:default'], [PODS]),
([], [LIM, REQ, CAP]) # container from kubernetes api doesn't have a corresponding entry in Cadvisor
]

Expand Down Expand Up @@ -272,7 +272,7 @@ def test_historate_1_2(self, *args):
'kube_bar:baz',
'kube_replication_controller:dd-agent'], [MEM, CPU, NET, DISK, NET_ERRORS, DISK_USAGE, LIM, REQ]),
(['pod_name:no_pod'], [MEM, CPU, FS, NET, NET_ERRORS, DISK]),
(['kube_replication_controller:dd-agent'], [PODS]),
(['kube_replication_controller:dd-agent', 'kube_namespace:default'], [PODS]),
([], [LIM, REQ, CAP]) # container from kubernetes api doesn't have a corresponding entry in Cadvisor
]

Expand Down