diff --git a/utilities/k8s-coredump/k8s-coredump/k8s_coredump/__main__.py b/utilities/k8s-coredump/k8s-coredump/k8s_coredump/__main__.py index cc506957..788043fc 100644 --- a/utilities/k8s-coredump/k8s-coredump/k8s_coredump/__main__.py +++ b/utilities/k8s-coredump/k8s-coredump/k8s_coredump/__main__.py @@ -1,5 +1,5 @@ ################################################################################ -# Copyright (c) 2022 Wind River Systems, Inc. +# Copyright (c) 2022,2025 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -12,7 +12,7 @@ from . import coredump def main(): # https://man7.org/linux/man-pages/man5/core.5.html kwargs = { - 'pid': sys.argv[1], # %P + 'host_pid': sys.argv[1], # %P 'uid': sys.argv[2], # %u 'gid': sys.argv[3], # %g 'signal': sys.argv[4], # %s @@ -20,6 +20,7 @@ def main(): 'comm': sys.argv[6], # %e 'hostname': sys.argv[7], # %h 'comm2': sys.argv[8], # %h + 'container_pid': sys.argv[9], # %p } coredump.CoreDumpHandler(**kwargs) diff --git a/utilities/k8s-coredump/k8s-coredump/k8s_coredump/config_functions.py b/utilities/k8s-coredump/k8s-coredump/k8s_coredump/config_functions.py index 05b2b249..4732fb12 100644 --- a/utilities/k8s-coredump/k8s-coredump/k8s_coredump/config_functions.py +++ b/utilities/k8s-coredump/k8s-coredump/k8s_coredump/config_functions.py @@ -1,5 +1,5 @@ ################################################################################ -# Copyright (c) 2022 Wind River Systems, Inc. +# Copyright (c) 2022,2025 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -71,14 +71,20 @@ def parse_core_pattern(string_core_pattern, **kwargs): string_core_pattern = splitted_path[-1] LOG.info(f'Parsing core pattern: {string_core_pattern}') - processed_string = string_core_pattern.lower() - processed_string = processed_string.replace('%p', kwargs['pid']) - processed_string = processed_string.replace('%u', kwargs['uid']) - processed_string = processed_string.replace('%g', kwargs['gid']) - processed_string = processed_string.replace('%s', kwargs['signal']) - processed_string = processed_string.replace('%t', kwargs['timestamp']) - processed_string = processed_string.replace('%h', kwargs['hostname']) - processed_string = processed_string.replace('%e', kwargs['comm2']) + processed_string = string_core_pattern + for pattern, value in [ + ('%p', kwargs.get('container_pid')), + ('%P', kwargs.get('host_pid')), + ('%u', kwargs.get('uid')), + ('%g', kwargs.get('gid')), + ('%s', kwargs.get('signal')), + ('%t', kwargs.get('timestamp')), + ('%h', kwargs.get('hostname')), + ('%e', kwargs.get('comm2')) + ]: + if pattern in processed_string: + processed_string = processed_string.replace(pattern, value) + LOG.info(f'Core pattern parsed to {processed_string}') splitted_path[-1] = processed_string diff --git a/utilities/k8s-coredump/k8s-coredump/k8s_coredump/coredump.py b/utilities/k8s-coredump/k8s-coredump/k8s_coredump/coredump.py index 308451d3..3c1b47d0 100644 --- a/utilities/k8s-coredump/k8s-coredump/k8s_coredump/coredump.py +++ b/utilities/k8s-coredump/k8s-coredump/k8s_coredump/coredump.py @@ -1,5 +1,5 @@ ################################################################################ -# Copyright (c) 2022 Wind River Systems, Inc. +# Copyright (c) 2022,2025 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -98,11 +98,14 @@ def _podCoreFile(pid, corefile, annotations_config): def CoreDumpHandler(**kwargs): - pid = kwargs['pid'] + pid = kwargs['host_pid'] uid = kwargs['uid'] exe = kwargs['comm'] - - LOG.critical("Process %s (%s) of user %s dumped core." % (pid, exe, uid)) + container_pid = kwargs['container_pid'] + LOG.critical( + "Process of External PID %s / Internal PID %s (command:%s) of user %s dumped core" % + (pid, container_pid, exe, uid) + ) pod = _lookupPod(pid) if pod: diff --git a/utilities/k8s-coredump/k8s-coredump/k8s_coredump/tests/base.py b/utilities/k8s-coredump/k8s-coredump/k8s_coredump/tests/base.py index c7f4c9b1..2e104ee1 100644 --- a/utilities/k8s-coredump/k8s-coredump/k8s_coredump/tests/base.py +++ b/utilities/k8s-coredump/k8s-coredump/k8s_coredump/tests/base.py @@ -1,5 +1,5 @@ ################################################################################ -# Copyright (c) 2023 Wind River Systems, Inc. +# Copyright (c) 2023,2025 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -125,7 +125,7 @@ class BaseTestCase(TestCase): # Values that would come from the invokation of k8s-coredump-handler self.input_kwargs = { - 'pid': "999999", # %P + 'host_pid': "999999", # %P 'uid': "8", # %u 'gid': "7", # %g 'signal': "6", # %s @@ -133,4 +133,5 @@ class BaseTestCase(TestCase): 'comm': "process_name_for_systemd_handler", # %e 'hostname': "test_host", # %h 'comm2': "process_name_for_k8s_handler", # %e + 'container_pid': "123456", # %p } diff --git a/utilities/k8s-coredump/k8s-coredump/k8s_coredump/tests/test_data.py b/utilities/k8s-coredump/k8s-coredump/k8s_coredump/tests/test_data.py index bb13b8a9..5ffc15ea 100644 --- a/utilities/k8s-coredump/k8s-coredump/k8s_coredump/tests/test_data.py +++ b/utilities/k8s-coredump/k8s-coredump/k8s_coredump/tests/test_data.py @@ -1,5 +1,5 @@ ################################################################################ -# Copyright (c) 2023 Wind River Systems, Inc. +# Copyright (c) 2023,2025 Wind River Systems, Inc. # # SPDX-License-Identifier: Apache-2.0 # @@ -17,12 +17,12 @@ DISK_USAGE = {'total_space': 536870912000, 'used_space': 268435456000, 'free_spa # Dictionary with test input values and expected values for individual test cases. ANNOTATIONS_EXAMPLES = [ { - "starlingx.io/core_pattern": "test.core.%P.%U.%G.%S.%T.%E.%H", # All Upper Case + "starlingx.io/core_pattern": "test.core.%P.%p.%u.%g.%s.%t.%e.%h", "starlingx.io/core_max_size": "200K", # Test Kilobytes and Upper case "starlingx.io/core_compression": "lz4", # Test compression. "starlingx.io/core_max_used": "20%", # Test maximum used space "starlingx.io/core_min_free": "20%", - "expected_core_pattern": "test.core.999999.8.7.6.1671181200.process_name_for_k8s_handler.test_host", + "expected_core_pattern": "test.core.999999.123456.8.7.6.1671181200.process_name_for_k8s_handler.test_host", "expected_core_max_size": (200.0, config_functions.file_size_properties['k']), "expected_truncate_value": 0, # The value here is 0 because the core_max_used is 20% and the test @@ -33,14 +33,14 @@ ANNOTATIONS_EXAMPLES = [ { "starlingx.io/core_pattern": "test.core.%p.%u.%g.%s.%t.%e.%h", # All Lower Case "starlingx.io/core_max_size": "20m", # Test Megabytes and Lower case - "expected_core_pattern": "test.core.999999.8.7.6.1671181200.process_name_for_k8s_handler.test_host", + "expected_core_pattern": "test.core.123456.8.7.6.1671181200.process_name_for_k8s_handler.test_host", "expected_core_max_size": (20.0, config_functions.file_size_properties['m']), "expected_truncate_value": 20971520, # 20mb in Bytes "coredump_file_content": "0123456789012345678901234567890123456789", "expected_write_content": "0123456789012345678901234567890123456789", }, { - "starlingx.io/core_pattern": "test.core.%P.%u.%G.%s.%t.%E.%h", # Mixed Case + "starlingx.io/core_pattern": "test.core.%P.%u.%g.%s.%t.%e.%h", # Mixed Case "starlingx.io/core_max_size": "2G", # Test Gigabytes "starlingx.io/core_min_free": "249G", # The test is setup to have 250gb free space, configuring 249gb as @@ -64,14 +64,14 @@ ANNOTATIONS_EXAMPLES = [ { "starlingx.io/core_pattern": "test.core.%p.%u.%g.%s.%t.%e.%h", # All Lower Case "starlingx.io/core_max_size": "10b", # Test bytes and Lower case - "expected_core_pattern": "test.core.999999.8.7.6.1671181200.process_name_for_k8s_handler.test_host", + "expected_core_pattern": "test.core.123456.8.7.6.1671181200.process_name_for_k8s_handler.test_host", "expected_core_max_size": (10.0, config_functions.file_size_properties['b']), "expected_truncate_value": 10, # 10 Bytes "coredump_file_content": "012345678901234567890123456789", "expected_write_content": "0123456789", }, { - "starlingx.io/core_pattern": "/var/log/coredump/test.core.%P.%u.%G.%s.%t.%E.%h", # With path + "starlingx.io/core_pattern": "/var/log/coredump/test.core.%P.%u.%g.%s.%t.%e.%h", # With path "expected_core_pattern": "/var/log/coredump/test.core.999999.8.7.6.1671181200.process_name_for_k8s_handler.test_host", "expected_truncate_value": 0, # No size limit @@ -112,7 +112,7 @@ MOCKED_POD_INFO = f""" "uid": "{MOCKED_UID}", "annotations": {{ - "starlingx.io/core_pattern": "test.core.%P.%U.%G.%S.%T.%E.%H", + "starlingx.io/core_pattern": "test.core.%P.%u.%g.%s.%t.%e.%h.%p", "starlingx.io/core_max_size": "200K", "starlingx.io/core_compression": "lz4", "starlingx.io/core_max_used": "20%", diff --git a/utilities/stx-extensions/files/coredump-sysctl-debian.conf b/utilities/stx-extensions/files/coredump-sysctl-debian.conf index 90d6262e..e4a3fafd 100644 --- a/utilities/stx-extensions/files/coredump-sysctl-debian.conf +++ b/utilities/stx-extensions/files/coredump-sysctl-debian.conf @@ -13,7 +13,7 @@ # the core dump. # # See systemd-coredump(8) and core(5). -kernel.core_pattern=|/usr/bin/k8s-coredump %P %u %g %s %t 9223372036854775808 %h %e +kernel.core_pattern=|/usr/bin/k8s-coredump %P %u %g %s %t 9223372036854775808 %h %e %p # Allow that 16 coredumps are dispatched in parallel by the kernel. We want to # be able to collect process metadata from /proc/%P/ while processing