Skip to content

Commit

Permalink
Use docker hostname instead of default route to query cadvisor & kube…
Browse files Browse the repository at this point in the history
…let (#2609)

* Use docker hostname instead of default route to query cadvisor & kubelet
* remove is_dockerized condition from DockerUtil get_hostname
  • Loading branch information
hkaj authored and gmmeyer committed Jun 23, 2016
1 parent b5ac195 commit 3c6c143
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
1 change: 1 addition & 0 deletions checks.d/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
RATE: {True: HISTORATE, False: RATE}
}


class Kubernetes(AgentCheck):
""" Collect metrics and events from kubelet """

Expand Down
6 changes: 6 additions & 0 deletions tests/checks/mock/test_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_fail_1_1(self):
}

with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.1.json", string_escape=False))):
with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'):
# Can't use run_check_twice due to specific metrics
self.run_check(config, mocks=mocks, force_reload=True)
self.assertServiceCheck("kubernetes.kubelet.check", status=AgentCheck.CRITICAL, tags=None, count=1)
Expand All @@ -69,6 +70,7 @@ def test_metrics_1_1(self):
}
# parts of the json returned by the kubelet api is escaped, keep it untouched
with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.1.json", string_escape=False))):
with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'):
# Can't use run_check_twice due to specific metrics
self.run_check_twice(config, mocks=mocks, force_reload=True)

Expand Down Expand Up @@ -125,6 +127,7 @@ def test_historate_1_1(self):

# parts of the json returned by the kubelet api is escaped, keep it untouched
with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.1.json", string_escape=False))):
with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'):
# Can't use run_check_twice due to specific metrics
self.run_check_twice(config, mocks=mocks, force_reload=True)

Expand Down Expand Up @@ -163,6 +166,7 @@ def test_fail_1_2(self):
}

with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.2.json", string_escape=False))):
with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'):
# Can't use run_check_twice due to specific metrics
self.run_check(config, mocks=mocks, force_reload=True)
self.assertServiceCheck("kubernetes.kubelet.check", status=AgentCheck.CRITICAL)
Expand All @@ -183,6 +187,7 @@ def test_metrics_1_2(self):
}
# parts of the json returned by the kubelet api is escaped, keep it untouched
with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.2.json", string_escape=False))):
with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'):
# Can't use run_check_twice due to specific metrics
self.run_check_twice(config, mocks=mocks, force_reload=True)

Expand Down Expand Up @@ -235,6 +240,7 @@ def test_historate_1_2(self):

# parts of the json returned by the kubelet api is escaped, keep it untouched
with mock.patch('utils.kubeutil.KubeUtil.retrieve_pods_list', side_effect=lambda: json.loads(Fixtures.read_file("pods_list_1.2.json", string_escape=False))):
with mock.patch('utils.dockerutil.DockerUtil.get_hostname', side_effect=lambda: 'foo'):
# Can't use run_check_twice due to specific metrics
self.run_check_twice(config, mocks=mocks, force_reload=True)

Expand Down
10 changes: 4 additions & 6 deletions utils/dockerutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,10 @@ def get_events(self):

def get_hostname(self):
"""Return the `Name` param from `docker info` to use as the hostname"""
if self.is_dockerized():
try:
return self.client.info().get("Name")
except Exception:
log.critical("Unable to find docker host hostname")

try:
return self.client.info().get("Name")
except Exception:
log.critical("Unable to find docker host hostname")
return None

@property
Expand Down
21 changes: 4 additions & 17 deletions utils/kubeutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
from collections import defaultdict
import logging
import os
import socket
import struct
from urlparse import urljoin

# project
from util import check_yaml
from utils.checkfiles import get_conf_path
from utils.http import retrieve_json
from utils.singleton import Singleton
from utils.dockerutil import DockerUtil

log = logging.getLogger('collector')

Expand All @@ -39,6 +38,7 @@ class KubeUtil():
NAMESPACE_LABEL = "io.kubernetes.pod.namespace"

def __init__(self):
self.docker_util = DockerUtil()
try:
config_file_path = get_conf_path(KUBERNETES_CHECK_NAME)
check_config = check_yaml(config_file_path)
Expand All @@ -53,7 +53,7 @@ def __init__(self):
instance = {}

self.method = instance.get('method', KubeUtil.DEFAULT_METHOD)
self.host = instance.get("host") or self._get_default_router()
self.host = instance.get("host") or self.docker_util.get_hostname()

self.cadvisor_port = instance.get('port', KubeUtil.DEFAULT_CADVISOR_PORT)
self.kubelet_port = instance.get('kubelet_port', KubeUtil.DEFAULT_KUBELET_PORT)
Expand Down Expand Up @@ -85,7 +85,7 @@ def extract_kube_labels(self, pods_list, excluded_keys=None):
if name and labels and namespace:
key = "%s/%s" % (namespace, name)

for k,v in labels.iteritems():
for k, v in labels.iteritems():
if k in excluded_keys:
continue

Expand All @@ -95,16 +95,3 @@ def extract_kube_labels(self, pods_list, excluded_keys=None):

def retrieve_pods_list(self):
return retrieve_json(self.pods_list_url)

@classmethod
def _get_default_router(cls):
try:
with open('/proc/net/route') as f:
for line in f.readlines():
fields = line.strip().split()
if fields[1] == '00000000':
return socket.inet_ntoa(struct.pack('<L', int(fields[2], 16)))
except IOError, e:
log.error('Unable to open /proc/net/route: %s', e)

return None

0 comments on commit 3c6c143

Please sign in to comment.