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

Add a timeout for Kubernetes API calls #9035

Merged
merged 1 commit into from
Mar 26, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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