Skip to content

Commit

Permalink
Add a timeout for Kubernetes API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
L3n41c committed Mar 25, 2021
1 parent 3b2def1 commit fb2437c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# will be tried in the order of the list
ELECTION_ANNOTATION_NAMES = ["control-plane.alpha.kubernetes.io/leader"]

K8S_REQUEST_TIMEOUT = 30


class KubeLeaderElectionMixin(object):
"""
Expand Down Expand Up @@ -80,7 +82,7 @@ def _get_record(kind, name, namespace):
@staticmethod
def _get_record_from_lease(client, name, namespace):
coordination_v1 = client.CoordinationV1Api()
obj = coordination_v1.read_namespaced_lease(name, namespace)
obj = coordination_v1.read_namespaced_lease(name, namespace, _request_timeout=K8S_REQUEST_TIMEOUT)

return ElectionRecordLease(obj)

Expand All @@ -89,9 +91,9 @@ def _get_record_from_annotation(client, kind, name, namespace):
v1 = client.CoreV1Api()

if kind.lower() in ["endpoints", "endpoint", "ep"]:
obj = v1.read_namespaced_endpoints(name, namespace)
obj = v1.read_namespaced_endpoints(name, namespace, _request_timeout=K8S_REQUEST_TIMEOUT)
elif kind.lower() in ["configmap", "cm"]:
obj = v1.read_namespaced_config_map(name, namespace)
obj = v1.read_namespaced_config_map(name, namespace, _request_timeout=K8S_REQUEST_TIMEOUT)
else:
raise ValueError("Unknown kind {}".format(kind))

Expand Down
4 changes: 2 additions & 2 deletions datadog_checks_base/tests/test_kube_leader.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def test_valid_endpoints(self, aggregator, mock_read_endpoints, mock_incluster):
c.check(EP_INSTANCE)

assert c.get_warnings() == []
mock_read_endpoints.assert_called_once_with("thisrecord", "myns")
mock_read_endpoints.assert_called_once_with("thisrecord", "myns", _request_timeout=30)
aggregator.assert_metric("base.leader_election.transitions", value=7, tags=EP_TAGS)
aggregator.assert_metric("base.leader_election.lease_duration", value=60, tags=EP_TAGS)
aggregator.assert_service_check("base.leader_election.status", status=AgentCheck.CRITICAL, tags=EP_TAGS)
Expand All @@ -231,7 +231,7 @@ def test_valid_configmap(self, aggregator, mock_read_configmap, mock_incluster):
c.check(CM_INSTANCE)

assert c.get_warnings() == []
mock_read_configmap.assert_called_once_with("thisrecord", "myns")
mock_read_configmap.assert_called_once_with("thisrecord", "myns", _request_timeout=30)
aggregator.assert_metric("base.leader_election.transitions", value=7, tags=CM_TAGS)
aggregator.assert_metric("base.leader_election.lease_duration", value=60, tags=CM_TAGS)
aggregator.assert_service_check("base.leader_election.status", status=AgentCheck.CRITICAL, tags=CM_TAGS)
Expand Down

0 comments on commit fb2437c

Please sign in to comment.