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 1eb2867
Showing 1 changed file with 5 additions and 3 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

0 comments on commit 1eb2867

Please sign in to comment.