Skip to content

Commit

Permalink
salt: Add logs to custom drain module
Browse files Browse the repository at this point in the history
Mostly debug information, one more visible log about Eviction creation
retried in case we receive a 429 from k-a.

See: #2530
  • Loading branch information
gdemonet committed May 7, 2020
1 parent 561b134 commit 5074002
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions salt/_modules/metalk8s_drain.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
module when called by salt by virtue of its `__virtualname__` attribute.
'''

import json
import logging
import operator
import time
Expand Down Expand Up @@ -345,6 +346,7 @@ def run_drain(self, dry_run=False):
Returns: string message
Raises: CommandExecutionError in case of timeout or eviction failure
'''
log.debug("Beginning drain of Node {}".format(self.node_name))
try:
pods = self.get_pods_for_eviction()
except DrainException as exc:
Expand All @@ -358,15 +360,20 @@ def run_drain(self, dry_run=False):
)
)

if pods:
pods_to_evict = ", ".join([
pod['metadata']['name'] for pod in pods
])
else
pods_to_evict = "no pods to evict."

if dry_run:
# Would be nice to create the Eviction in dry-run mode and see if
# we hit some 429 Too Many Requests (because a disruption budget
# would prevent the eviction)
return "Prepared for eviction of pods: {0}".format(
", ".join([pod['metadata']['name'] for pod in pods])
if pods else "no pods to evict."
)
return "Prepared for eviction of pods: {}".format(pods_to_evict)

log.debug("Starting eviction of pods: {}".format(pods_to_evict))
try:
self.evict_pods(pods)
except DrainTimeoutException as exc:
Expand Down Expand Up @@ -434,6 +441,11 @@ def wait_for_eviction(self, pods):
response['metadata']['uid'] != pod['metadata']['uid']:
log.info("%s evicted", pod['metadata']['name'])
else:
log.debug(
"Waiting for eviction of Pod %s (current status: %s)",
pod['metadata']['name'],
pod.get('status', {}).get('phase'),
)
pending.append(pod)

if not pending:
Expand Down Expand Up @@ -507,10 +519,22 @@ def evict_pod(name, namespace='default', grace_period=1,
if isinstance(exc, ApiException):
if exc.status == 404:
# Seems to be ignored in kubectl, let's do the same
log.debug(
"Received '404 Not Found' when creating Eviction for %s, "
"ignoring",
name,
)
return True
if exc.status == 429:
# Too Many Requests: the eviction is rejected, but indicates
# we should retry later (probably due to a disruption budget)
status = json.loads(exc.body, encoding="utf-8")
log.info(
"Cannot evict %s at the moment: %s",
name,
status['message'],
)
# When implemented by Kubernetes, read the `Retry-After` advice
return False

raise CommandExecutionError(
Expand Down

0 comments on commit 5074002

Please sign in to comment.