Skip to content

Commit

Permalink
Add functionaility to update leader label on pods
Browse files Browse the repository at this point in the history
  • Loading branch information
yuha0 committed Jan 5, 2024
1 parent d7f95c7 commit 5b6147a
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cloudflare-ddns/leaderelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def __init__(
except config.config_exception.ConfigException:
self.incluster = False
config.load_kube_config(config_file=kubeconfig)
lock_ns = lock_ns if lock_ns else self._get_namespace()
self.kclient = client.CoreV1Api()
self.ns = self._get_namespace()
lock_ns = lock_ns if lock_ns else self.ns
logging.info("Set election namespace to '%s'", lock_ns)
logging.info("Set election lock to ConfigMap '%s'", lock_name)
self.candidate_id = candidate_id
Expand All @@ -33,8 +35,8 @@ def __init__(
lease_duration,
renew_deadline,
retry_period=5,
onstarted_leading=self._logged_callback(isleader=True, cb=onstart),
onstopped_leading=self._logged_callback(isleader=False, cb=onstop),
onstarted_leading=self._prepare_callback(isleader=True, cb=onstart),
onstopped_leading=self._prepare_callback(isleader=False, cb=onstop),
)
self.election = leaderelection.LeaderElection(election_config)

Expand All @@ -46,9 +48,17 @@ def _get_namespace(self):
namespace = config.list_kube_config_contexts()[1]["context"]["namespace"]
return namespace

def _logged_callback(self, isleader, cb):
def _prepare_callback(self, isleader, cb):
def wrapper():
logging.info("I am %s", "the leader" if isleader else "a follower")
patches = [
{
"op": "replace",
"path": "/metadata/labels/primary",
"value": str(isleader).lower()
}
]
self.kclient.patch_namespaced_pod(self.candidate_id, self.ns, body=patches)
cb()

return wrapper
Expand Down

0 comments on commit 5b6147a

Please sign in to comment.