2

I am trying to run jobs that call python apps that take dictionaries as parameters.

For example, running it locally might look like this:

 python3 examples/test.py '{"school":"EFG", "standard": "2", "name": "abc", "city": "miami"}'

So, in my jobspec I am trying to do this:

command: ["/bin/bash","-c"]
args: ["python3 examples/test.py '{"school":"EFG", "standard": "2", "name": "abc", "city": "miami"}'"]

However, I get a yaml error:

error: error parsing job.yaml.tmpl: error converting YAML to JSON: yaml: line 18: could not find expected ':'

I see in yaml lint that it is not valid. The only way I have managed to pass yaml validation is to escape every single quote, which is not scalable for hundreds of jobs that users will be running

args: 
  - "python3 examples/test.py '{\"school\":\"hello\", \"standard\": \"5\"}'"

Why is this not valid yaml and how can I do this without needing to escape every single quote in a given dictionary a user passes in?

Howard_Roark
  • 4,088
  • 1
  • 14
  • 24

1 Answers1

0

It's hard to replicate your setup without your code, however below examples should work for you. In Kubernetes Docs - Run a command in a shell you can find information, that all commands should be wrapped.

In some cases, you need your command to run in a shell. For example, your command might consist of several commands piped together, or it might be a shell script. To run your command in a shell, wrap it.

Below you have default syntax

command: ["/bin/sh","-c"]
args: ["command one; command two && command three"]

I've changed a bit your command and verified on yamllint, all passed.

Examples with only /bin/bash in command:

command: 
  - /bin/bash
args: 
  - "-c"
  - "python3 examples/test.py '{\"school\":\"EFG\", \"standard\": \"2\", \"name\": \"abc\", \"city\": \"miami\"}'\"]"

or

command: ["/bin/bash"]
args: ["-c", "python3 examples/test.py", '{"school":"EFG", "standard": "2", "name": "abc", "city": "miami"}']

Examples with /bin/bash and -c in command:

command: 
  - /bin/bash
  - "-c"
args: 
  - "python3 examples/test.py"
  - "{\"school\":\"EFG\", \"standard\": \"2\", \"name\": \"abc\", \"city\": \"miami\"}"

or

command: ["/bin/bash", "-c"]
args: ["python3 examples/test.py", '{"school":"EFG", "standard": "2", "name": "abc", "city": "miami"}']

In addition, there was a question about running multiple kubernetes commands in spec. You can find it here.

Let me know if this worked for you.

PjoterS
  • 12,841
  • 1
  • 22
  • 54
  • No I don't think so. The goal is to not escape any quotes. Using the examples of separating the json into a separate argument seems to cause the python program to be run without any arguments. ie- it seems like `python3 examples/test.py` is getting called on its own without the json getting passed in to it – Howard_Roark Jan 20 '21 at 16:37
  • Regarding the above comment, I tested these two ideas: `args: ["-c", "python3 examples/test.py", '{"school":"EFG", "standard": "2", "name": "abc", "city": "miami"}']` `args: ["python3 examples/test.py", '{"school":"EFG", "standard": "2", "name": "abc", "city": "miami"}']` – Howard_Roark Jan 20 '21 at 16:41
  • Maybe different approach like using `ConfigMap` with JSON embedded inside literal block scalar? Something like that was already mention in [this thread](https://stackoverflow.com/a/61654051/10347794). [Python docs](https://www.w3schools.com/python/python_json.asp) – PjoterS Feb 01 '21 at 11:49