Running python script from kubernetes with input args

8/23/2020

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.

-- JaVaPG
bash
docker
kubernetes
kubernetes-helm
python-2.7

1 Answer

8/23/2020

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
-- Victor Wong
Source: StackOverflow