Skip to content

Commit

Permalink
RED-76192 - Fix log collector previous container fetch logic - unlink…
Browse files Browse the repository at this point in the history
…ing while opened the previous container log file is still inuse. (#234)
  • Loading branch information
alon-zada authored May 30, 2022
1 parent 203225e commit 91cbadc
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions log_collector/log_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,15 +505,14 @@ def collect_logs_from_pod(namespace, pod, logs_dir):
# getting the logs of the containers before the restart can help us with debugging potential bugs
get_logs_before_restart_cmd = "kubectl logs -c {} -n {} {} -p" \
.format(container, namespace, pod)
with open(os.path.join(logs_dir, "{}.log".format(f'{pod}-{container}-instance-before-restart')),
"w+") as file_handle:
err_code, output = run_shell_command(get_logs_before_restart_cmd)
if err_code == 0:
err_code, output = run_shell_command(get_logs_before_restart_cmd)
container_log_before_restart_file = os.path.join(logs_dir,
"{}.log".format(f'{pod}-{container}-instance-before-restart'))
if err_code == 0: # Previous container instance found; did restart.
with open(container_log_before_restart_file, "w+") as file_handle:
file_handle.write(output)
else: # no previous container instance found; did not restart
os.unlink(file_handle.name)

logger.info("Namespace '%s': + %s-%s", namespace, pod, container)
logger.info("Namespace '%s': + %s-%s", namespace, pod, container)


def get_pod_names(namespace, selector=""):
Expand Down

0 comments on commit 91cbadc

Please sign in to comment.