kiwigrid / k8s-sidecar

This is a docker container intended to run inside a kubernetes cluster to collect config maps with a specified label and store the included files in a local folder.
MIT License
612 stars 183 forks source link

Add exception_handler #333

Closed jkroepke closed 6 months ago

jkroepke commented 8 months ago

From https://stackoverflow.com/questions/6234405/logging-uncaught-exceptions-in-python

Before:

Traceback (most recent call last):
  File "/app/sidecar.py", line 170, in <module>
    main()
    ~~~~^^
  File "/app/sidecar.py", line 81, in main
    _initialize_kubeclient_configuration()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/app/sidecar.py", line 150, in _initialize_kubeclient_configuration
    config.load_incluster_config()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/app/.venv/lib/python3.13/site-packages/kubernetes/config/incluster_config.py", line 121, in load_incluster_config
    try_refresh_token=try_refresh_token).load_and_set(client_configuration)
                                         ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/kubernetes/config/incluster_config.py", line 54, in load_and_set
    self._load_config()
    ~~~~~~~~~~~~~~~~~^^
  File "/app/.venv/lib/python3.13/site-packages/kubernetes/config/incluster_config.py", line 62, in _load_config
    raise ConfigException("Service host/port is not set.")
kubernetes.config.config_exception.ConfigException: Service host/port is not set.

After:

{"time": "2024-03-17T09:43:23.667112+00:00", "exc_info": "Traceback (most recent call last):\n  File \"/app/sidecar.py\", line 170, in <module>\n    main()\n    ~~~~^^\n  File \"/app/sidecar.py\", line 81, in main\n    _initialize_kubeclient_configuration()\n    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^\n  File \"/app/sidecar.py\", line 150, in _initialize_kubeclient_configuration\n    config.load_incluster_config()\n    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^\n  File \"/app/.venv/lib/python3.13/site-packages/kubernetes/config/incluster_config.py\", line 121, in load_incluster_config\n    try_refresh_token=try_refresh_token).load_and_set(client_configuration)\n                                         ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^\n  File \"/app/.venv/lib/python3.13/site-packages/kubernetes/config/incluster_config.py\", line 54, in load_and_set\n    self._load_config()\n    ~~~~~~~~~~~~~~~~~^^\n  File \"/app/.venv/lib/python3.13/site-packages/kubernetes/config/incluster_config.py\", line 62, in _load_config\n    raise ConfigException(\"Service host/port is not set.\")\nkubernetes.config.config_exception.ConfigException: Service host/port is not set.", "taskName": null, "msg": "ConfigException: Service host/port is not set.", "level": "ERROR"}
ChristianGeie commented 6 months ago

@jkroepke so that I understand this correctly: You want to use the sys.excepthook function to set a global exception handler for uncaught exceptions, so this funtion is called via the custom exception hook sys.excepthook = exception_handler whenever an uncaught exception occurs.

jkroepke commented 6 months ago

Yes

ChristianGeie commented 6 months ago

thanks for confirming that. Meantime i extend the tests. pls rebase so i can trigger the workflow again.

jkroepke commented 6 months ago

Done