Skip to content

Commit

Permalink
Fix tests, rework config interface
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaj committed Aug 22, 2017
1 parent 1e458be commit 08bc76e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
13 changes: 3 additions & 10 deletions tests/core/test_leader_elector.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@
import mock

# project
from utils.kubernetes import LeaderElector
from tests.core.test_kubeutil import KubeTestCase


HEALTH_ENDPOINT = '/healthz'
DEFAULT_NAMESPACE = 'default' # TODO: use agent's own ns
CM_ENDPOINT = '/namespaces/{namespace}/configmaps'
CM_NAME = 'datadog-leader-elector'
CREATOR_ANNOTATION = 'creator'
ACQUIRE_TIME_ANNOTATION = 'acquired_time'
from utils.kubernetes.leader_elector import LeaderElector, \
ACQUIRE_TIME_ANNOTATION, CREATOR_ANNOTATION, CM_NAME


class TestLeaderElector(KubeTestCase):
Expand Down Expand Up @@ -74,7 +67,7 @@ def test_build_create_cm_payload(self):
'data': {},
'metadata': {
'name': CM_NAME,
'namespace': DEFAULT_NAMESPACE,
'namespace': 'default',
'annotations': {
CREATOR_ANNOTATION: 'foo',
ACQUIRE_TIME_ANNOTATION: datetime.datetime.strftime(now, "%Y-%m-%dT%H:%M:%S.%f")
Expand Down
1 change: 1 addition & 0 deletions utils/kubernetes/kubeutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __init__(self, instance=None):
# leader status triggers event collection
self.is_leader = False
self.leader_elector = None
self.leader_lease_duration = instance.get('lease_duration')

# kubelet
try:
Expand Down
9 changes: 4 additions & 5 deletions utils/kubernetes/leader_elector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
CM_NAME = 'datadog-leader-elector'
CREATOR_ANNOTATION = 'creator'
ACQUIRE_TIME_ANNOTATION = 'acquired_time'
# TODO: make lease duration configurable
DEFAULT_LEASE_DURATION = 5 * 60 # seconds

class LeaderElector:
Expand Down Expand Up @@ -41,6 +40,7 @@ def __init__(self, kubeutil):
self.apiserver_url = kubeutil.kubernetes_api_url
self.self_namespace = kubeutil.self_namespace
self.last_acquire_time = None
self.lease_duration = kubeutil.leader_lease_duration or DEFAULT_LEASE_DURATION
if not self._is_reachable():
return

Expand All @@ -59,7 +59,7 @@ def try_acquire_or_refresh(self):
"""
expiry_time = None
if self.last_acquire_time:
expiry_time = self.last_acquire_time + datetime.timedelta(seconds=DEFAULT_LEASE_DURATION)
expiry_time = self.last_acquire_time + datetime.timedelta(seconds=self.lease_duration)

if self.kubeutil.is_leader:
if expiry_time and expiry_time - datetime.timedelta(seconds=30) <= datetime.datetime.utcnow():
Expand All @@ -78,7 +78,6 @@ def _try_acquire(self):
note: if the CM already exists, is fresh, and the creator is the local node,
this agent is elected leader. It means agents were re-deployed quickly
and the expiry time is not up yet.
TODO: should we _try_refresh if _is_cm_mine(cm) ?
"""
try:
cm = self._get_cm()
Expand Down Expand Up @@ -139,7 +138,7 @@ def _is_lock_expired(self, cm):

acquired_time = datetime.datetime.strptime(acquired_time, "%Y-%m-%dT%H:%M:%S.%f")

if acquired_time + datetime.timedelta(seconds=DEFAULT_LEASE_DURATION) <= datetime.datetime.utcnow():
if acquired_time + datetime.timedelta(seconds=self.lease_duration) <= datetime.datetime.utcnow():
return True
return False

Expand Down Expand Up @@ -203,7 +202,7 @@ def _build_create_cm_payload(self):
ACQUIRE_TIME_ANNOTATION: datetime.datetime.strftime(now, "%Y-%m-%dT%H:%M:%S.%f")
},
'name': CM_NAME,
'namespace': self.self_namespace # TODO: use agent namespace
'namespace': self.self_namespace
}
}
return pl
Expand Down

0 comments on commit 08bc76e

Please sign in to comment.