1

With Kubernetes Container running a Python script:

import time
while True:
    try:
        for i in range(10):
            if i==0:
                raise Exception('Exception occurred!')
    except:
        pass
    time.sleep(1)

I would like to pass the Exception's message 'Exception occurred!' down to the Container so this error message could be seen with:

kubectl describe pod pod_id

Would it be possible?

1
  • 5
    What you have right now will be visible in kubectl logs ... if you want it in the output of kubectl describe pod..., you'll need to write it to create it as a kubernetes event. Commented Jul 25, 2019 at 0:41

1 Answer 1

5

Anything you print() will be visible in kubectl logs. (You may need to set an environment variable PYTHONUNBUFFERED=1 in your pod spec.)

Your code as you've written it will never print anything. The construct

try:
  ...
except:
  pass

silently ignores any and all exceptions out of the try block. The bare except: even captures some system-level exceptions like SystemExit or KeyboardInterrupt; this is almost always wrong. Often you want your except blocks to be as tightly scoped as you can, and the Python tutorial on user-defined exceptions is a helpful pattern.

(The exception to this, particularly in a Kubernetes context, is that you will often want a very broad exception handler to do something like return an HTTP 500 error to a network request, rather than crashing the application.)

A better example might look like:

import time

class OneException(Exception):
  pass

def iteration():
  for i in range(10):
    try:
      if i == 1:
        raise OneException("it is one")
      print(i, math.sqrt(i), math.sqrt(-i))
      # will work when i==0 but fail when i==2
    except OneException as e:
      print(i, repr(e))
      # and proceed to the next iteration

if __name__ == '__main__':
  while True:
    # The top-level loop.  We want a very broad catch here.
    try:
      iteration()
    except Exception as e:
      print('iteration failed', repr(e))
    time.sleep(1)
Sign up to request clarification or add additional context in comments.

2 Comments

PYTHONUNBUFFERED=1 did the trick for me, thank you!
If i want to use logging module not print(). Will that work with PYTHONUNBUFFERED= 1?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.