Skip to content

Commit

Permalink
Revise log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobHenner committed Jan 8, 2025
1 parent 1ffa611 commit 7865347
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
3 changes: 3 additions & 0 deletions examples/leaderelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import asyncio
import logging
import os
import uuid

Expand All @@ -21,6 +22,8 @@
from kubernetes_asyncio.leaderelection import electionconfig, leaderelection
from kubernetes_asyncio.leaderelection.resourcelock.leaselock import LeaseLock

logging.basicConfig(level=logging.INFO)


async def main():

Expand Down
28 changes: 14 additions & 14 deletions kubernetes_asyncio/leaderelection/leaderelection.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

from .leaderelectionrecord import LeaderElectionRecord


"""
This package implements leader election using an annotation in a Kubernetes
object. The onstarted_leading coroutine is run as a task, which is cancelled if
Expand Down Expand Up @@ -73,7 +72,7 @@ async def run(self):

async def acquire(self):
# Follower
logging.info("%s is a follower", self.election_config.lock.identity)
logging.debug("%s is a follower", self.election_config.lock.identity)
retry_period = self.election_config.retry_period

while True:
Expand All @@ -86,7 +85,7 @@ async def acquire(self):

async def renew_loop(self):
# Leader
logging.info(
logging.debug(
"Leader has entered renew loop and will try to update lease continuously"
)

Expand Down Expand Up @@ -131,13 +130,14 @@ async def try_acquire_or_renew(self):
# A lock is not created with that name, try to create one
if not lock_status:
if json.loads(old_election_record.body)["code"] != HTTPStatus.NOT_FOUND:
logging.info(
logging.error(

Check warning on line 133 in kubernetes_asyncio/leaderelection/leaderelection.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/leaderelection.py#L133

Added line #L133 was not covered by tests
"Error retrieving resource lock %s as %s",
self.election_config.lock.name, old_election_record.reason,
self.election_config.lock.name,
old_election_record.reason,
)
return False

Check warning on line 138 in kubernetes_asyncio/leaderelection/leaderelection.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/leaderelection.py#L138

Added line #L138 was not covered by tests

logging.info(
logging.debug(
"%s is trying to create a lock",
leader_election_record.holder_identity,
)
Expand All @@ -148,8 +148,8 @@ async def try_acquire_or_renew(self):
)

if not create_status:
logging.info(
"%s Failed to create lock", leader_election_record.holder_identity
logging.error(

Check warning on line 151 in kubernetes_asyncio/leaderelection/leaderelection.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/leaderelection.py#L151

Added line #L151 was not covered by tests
"%s failed to create lock", leader_election_record.holder_identity
)
return False

Check warning on line 154 in kubernetes_asyncio/leaderelection/leaderelection.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/leaderelection.py#L154

Added line #L154 was not covered by tests

Expand Down Expand Up @@ -178,7 +178,7 @@ async def try_acquire_or_renew(self):
and self.observed_record.holder_identity
!= old_election_record.holder_identity
):
logging.info(
logging.debug(

Check warning on line 181 in kubernetes_asyncio/leaderelection/leaderelection.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/leaderelection.py#L181

Added line #L181 was not covered by tests
"Leader has switched to %s", old_election_record.holder_identity
)

Expand All @@ -196,8 +196,8 @@ async def try_acquire_or_renew(self):
+ self.election_config.lease_duration * 1000
> int(now_timestamp * 1000)
):
logging.info(
"yet to finish lease_duration, lease held by %s and has not expired",
logging.debug(
"Yet to finish lease_duration, lease held by %s and has not expired",
old_election_record.holder_identity,
)
return False
Expand All @@ -218,15 +218,15 @@ async def update_lock(self, leader_election_record):
)

if not update_status:
logging.info(
logging.warning(
"%s failed to acquire lease", leader_election_record.holder_identity
)
return False

self.observed_record = leader_election_record
self.observed_time_milliseconds = int(time.time() * 1000)
logging.info(
"leader %s has successfully acquired lease",
logging.debug(
"Leader %s has successfully updated lease",
leader_election_record.holder_identity,
)
return True
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ async def create(self, name, namespace, election_record):
namespace, body, pretty=True
)
return True
except ApiException as e:
logging.info("Failed to create lock as %s", e)
except ApiException:
logging.exception("Failed to create lock")
return False

Check warning on line 110 in kubernetes_asyncio/leaderelection/resourcelock/configmaplock.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/resourcelock/configmaplock.py#L107-L110

Added lines #L107 - L110 were not covered by tests

async def update(self, name, namespace, updated_record):

Check warning on line 112 in kubernetes_asyncio/leaderelection/resourcelock/configmaplock.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/resourcelock/configmaplock.py#L112

Added line #L112 was not covered by tests
Expand All @@ -125,8 +125,8 @@ async def update(self, name, namespace, updated_record):
name=name, namespace=namespace, body=self.configmap_reference
)
return True
except ApiException as e:
logging.info("Failed to update lock as %s", e)
except ApiException:
logging.exception("Failed to update lock")
return False

Check warning on line 130 in kubernetes_asyncio/leaderelection/resourcelock/configmaplock.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/resourcelock/configmaplock.py#L127-L130

Added lines #L127 - L130 were not covered by tests

def get_lock_object(self, lock_record):
Expand Down
8 changes: 4 additions & 4 deletions kubernetes_asyncio/leaderelection/resourcelock/leaselock.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ async def create(self, name, namespace, election_record):
namespace, body, pretty=True
)
return True
except ApiException as e:
logging.info("Failed to create lock as %s", e)
except ApiException:
logging.exception("Failed to create lock")
return False

Check warning on line 74 in kubernetes_asyncio/leaderelection/resourcelock/leaselock.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/resourcelock/leaselock.py#L71-L74

Added lines #L71 - L74 were not covered by tests

async def update(self, name, namespace, updated_record):

Check warning on line 76 in kubernetes_asyncio/leaderelection/resourcelock/leaselock.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/resourcelock/leaselock.py#L76

Added line #L76 was not covered by tests
Expand All @@ -90,8 +90,8 @@ async def update(self, name, namespace, updated_record):
name=name, namespace=namespace, body=self.lease_reference
)
return True
except ApiException as e:
logging.info("Failed to update lock as %s", e)
except ApiException:
logging.exception("Failed to update lock")
return False

Check warning on line 95 in kubernetes_asyncio/leaderelection/resourcelock/leaselock.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/resourcelock/leaselock.py#L92-L95

Added lines #L92 - L95 were not covered by tests

def update_lease(self, leader_election_record, current_spec=None):

Check warning on line 97 in kubernetes_asyncio/leaderelection/resourcelock/leaselock.py

View check run for this annotation

Codecov / codecov/patch

kubernetes_asyncio/leaderelection/resourcelock/leaselock.py#L97

Added line #L97 was not covered by tests
Expand Down

0 comments on commit 7865347

Please sign in to comment.