Kubernetes Jobspec Passing Json Args: Why Is This Invalid Yaml And How To Fix?

Viewed 190

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?

1 Answers

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.

Related