2

I'm trying to run a python script from kubernetes with input args (using import argsparse on python), the python script running fine without any input args on kubernetes, also the following command python python_script.py -e $(date -d '2020/08/23') -d 7 -m runs with no issues on the docker image.

I'm trying to build my values.yaml file so kubernetes could run it as well, with no success until now.

My values.yaml file (only relevant part to the script):

- name: python-script
  command: ["python"]
  args:
    - "python_script.py"
    - "-e $(date -d '2020/08/23')"
    - "-d 7"
    - "-m"
  resources:
    limits:
      cpu: 50m
      memory: 512Mi
    requests:
      cpu: 50m
      memory: 512Mi
  failedJobsHistoryLimit: 1
  successfulJobsHistoryLimit: 3
  concurrencyPolicy: Forbid
  restartPolicy: Never

Output when running on kubernetes : python_script.py: error argument -e/--end_date: Not a valid date: ' $(date -d "2020/08/23")'.

It's like the kubernetes doesn't even parse the bash command $(date -d "2020/08/23") into a valid date for some reason, while the docker image there's no issues with it.

I tried to add another command of ["bash"] after the python file and add the args there but still getting error.

Any ideas? Any help will be really appreciated!

Thanks.

1 Answer 1

10

When you run with command python, then shell is not invoked.

So try:

- name: python-script
  command: ["/bin/sh"]
  args:
    - -c
    - >-
        python python_script.py
        -e $(date -d '2020/08/23')
        -d 7
        -m
  resources:
    limits:
      cpu: 50m
      memory: 512Mi
    requests:
      cpu: 50m
      memory: 512Mi
  failedJobsHistoryLimit: 1
  successfulJobsHistoryLimit: 3
  concurrencyPolicy: Forbid
  restartPolicy: Never
Sign up to request clarification or add additional context in comments.

5 Comments

The entire command python python_script.py ... needs to be inside a single args: line, but probably separate from the -c.
Worked! with @DavidMaze fix Victor if you can please edit the answer for future people coming accord this thanks to you both! :)
@VictorWong what does '->' under '-c' in args do?
@2bon2b you can read more about that in yaml-multiline.info
two years later and you still nailed it

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.