Skip to content

Commit

Permalink
[container_log] Add pods support
Browse files Browse the repository at this point in the history
In Kubernetes/OpenShift rotated logs have no symbolic link in
/var/log/containers and they cannot be retrieved using `oc logs` either,
so there is no way to get these rotated logs in a SOS report.

This patch proposes extending the `container_log` to make it also
capable of retrieving logs from `var/log/pods` using a plugin boolean
option called `rotated`.

When retrieving the pods directory, all logs will be retrieved from it
regardless of size, as it is implicit that the intent is to get all
rotated logs.

Closes #3677
Signed-off-by: Gorka Eguileor <[email protected]>
  • Loading branch information
Akrog committed Jun 20, 2024
1 parent e4671de commit 06881d1
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions sos/report/plugins/container_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,45 @@
# See the LICENSE file in the source distribution for further information.

import os
from sos.report.plugins import Plugin, IndependentPlugin
from sos.report.plugins import Plugin, IndependentPlugin, PluginOpt


class ContainerLog(Plugin, IndependentPlugin):

short_desc = 'All logs under /var/log/containers'
plugin_name = 'container_log'
logdir = '/var/log/containers/'
poddir = '/var/log/pods/'
files = (logdir, )

option_list = [
PluginOpt('rotated', default=False, val_type=bool,
desc='get all logs from /var/log/pods regardless of size, '
'which will include all rotated logs'),
]

def setup(self):
sizelimit = None
# Remove size limit from containers logs when getting rotated, since
# they are just symlinks to the pods logs
if self.get_option('rotated'):
# This should not happen, but better safe than sorry
if not os.path.exists(self.poddir):
self._log_warn(f"could not find {self.poddir}, skipping it")
else:
sizelimit = 0
self.add_copy_spec(self.poddir, sizelimit=sizelimit)

if self.get_option('all_logs'):
self.add_copy_spec(self.logdir)
else:
self.collect_subdirs()
self.collect_subdirs(sizelimit=sizelimit)

def collect_subdirs(self, root=logdir):
def collect_subdirs(self, root=logdir, sizelimit=None):
"""Collect *.log files from subdirs of passed root path
"""
for dir_name, _, _ in os.walk(root):
self.add_copy_spec(self.path_join(dir_name, '*.log'))
self.add_copy_spec(self.path_join(dir_name, '*.log'),
sizelimit=sizelimit)

# vim: set et ts=4 sw=4 :

0 comments on commit 06881d1

Please sign in to comment.