Hi.
I made a yaml file for a resource and launched it without problem:
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: deb
image: google/cloud-sdk:slim
command: ["/bin/bash"]
args: ['-c', 'echo hello']
restartPolicy: Never
backoffLimit: 0
I then tried to do the equivalent with the python API:
kubernetes.config.load_kube_config()
api_instance = kubernetes.client.BatchV1Api()
job_name = 'pi'
command = 'echo hello'
container = kubernetes.client.V1Container(
name='deb',
image='google/cloud-sdk:slim',
command='bin/bash',
args=['-c', command],
)
body = kubernetes.client.V1Job(
api_version='batch/v1',
kind='Job',
metadata=kubernetes.client.V1ObjectMeta(name=job_name),
spec=kubernetes.client.V1JobSpec(
template=kubernetes.client.V1PodTemplateSpec(
spec=kubernetes.client.V1PodSpec(
containers=[container],
restart_policy='Never',
),
),
backoff_limit=0,
),
)
namespace = 'my_example_namespace'
pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. (optional)
try:
api_response = self.api_instance.create_namespaced_job(namespace, body, pretty=pretty)
pprint.pprint(api_response)
except kubernetes.client.rest.ApiException as e:
print("Exception when calling BatchV1Api->create_namespaced_job: %s\n" % e)
Error:
Exception when calling BatchV1Api->create_namespaced_job: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Audit-Id': 'f00b7242-c592-42a2-87b8-c39b761cb85a', 'Content-Type': 'application/json', 'Date': 'Sat, 17 Mar 2018 03:52:39 GMT', 'Content-Length': '684'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Job in version \"v1\" cannot be handled as a Job: v1.Job: Spec: v1.JobSpec: Template: v1.PodTemplateSpec:
Spec: v1.PodSpec: Containers: []v1.Container: v1.Container: Command: []string: ReadArrayCB: expect [ or n, but found: \", parsing 171 ...ommand\": \"... at {\"apiVersion\": \"batch/v1\", \"kind\": \"Job\
", \"metadata\": {\"name\": \"pi\"}, \"spec\": {\"backoffLimit\": 0, \"template\": {\"spec\": {\"containers\": [{\"args\": [\"-c\", \"echo hello\"], \"command\": \"bin/bash\", \"image\": \"google/cloud-sdk:slim\
", \"name\": \"deb\"}], \"restartPolicy\": \"Never\"}}}}","reason":"BadRequest","code":400}
I can't figure out why I'm getting this error, what it means and what the discrepancy is between the python API and the command line API via YAML files.
Thanks so much for your help,
James
You've got the error:
Command: []string: ReadArrayCB: expect [ or n, but found: ",
so it means that a command has to be an array [] not string - as in the yaml file.
BTW, could you format your code ?
Thank you!
Most helpful comment
Thank you!