Python: Job with restart_policy = Never fails

Created on 23 Mar 2017  路  4Comments  路  Source: kubernetes-client/python

When using the create_namespaced_job method, the response is being returned is stating an Unsupported value when 'Never' is used.

Response:
kubernetes.client.rest.ApiException: (422) Reason: Unprocessable Entity HTTP response headers: HTTPHeaderDict({'Content-Length': '621', 'Date': 'Thu, 23 Mar 2017 13:19:54 GMT', 'Content-Type': 'application/json'}) HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Job.batch \"test-job\" is invalid: [spec.template.spec.containers: Required value, spec.template.spec.restartPolicy: Unsupported value: \"Always\": supported values: OnFailure, Never]","reason":"Invalid","details":{"name":"test-job","group":"batch","kind":"Job","causes":[{"reason":"FieldValueRequired","message":"Required value","field":"spec.template.spec.containers"},{"reason":"FieldValueNotSupported","message":"Unsupported value: \"Always\": supported values: OnFailure, Never","field":"spec.template.spec.restartPolicy"}]},"code":422}

Example code that creates the error
```
name = 'test-job'
labels = {'job': 'test'}
container_name = 'alpine'
container_image = 'alpine'
container_command = ['echo', 'Hello World']
job_restart_policy = 'Never'
namespace = 'default'

meta = kubernetes.client.V1ObjectMeta()
meta.name = name
meta.labels = labels

spec_template_meta = kubernetes.client.V1ObjectMeta()
spec_template_meta.name = name

container_manifest = kubernetes.client.V1Container()
container_manifest.name = container_name
container_manifest.image = container_image
container_manifest.command = container_command

spec_template_spec = kubernetes.client.V1PodSpec()
spec_template_spec.containers = [container_manifest]
spec_template_spec.restart_policy = job_restart_policy

spec_template = kubernetes.client.V1PodTemplateSpec()
spec_template.metadata = spec_template_meta
spec_template.spec = spec_template_spec

data = kubernetes.client.V1Job()
data.api_version = 'batch/v1'
data.kind = 'Job'
data.metadata = meta
data.spec = spec_template

k8s_client = kubernetes.client.BatchV1Api()
k8s_client.create_namespaced_job(body=data, namespace=namespace)```

All 4 comments

This is expected. Jobs only support OnFailure or Never. If you want a container to always restart, you want to use a ReplicaSet

More context in this doc:

https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/

Yes, I know that a Job should only work with OnFailure and Never. If you looks at the response by the API it is saying that I am sending Always and the code example shows that I am using job_restart_policy = 'Never'

the code above is wrong

        meta = kubernetes.client.V1ObjectMeta()
        meta.name = name
        meta.labels = {}

        spec = kubernetes.client.V1JobSpec()

        spec.template = kubernetes.client.V1PodTemplateSpec()
        spec.template.metadata = kubernetes.client.V1ObjectMeta(name=name)
        spec.template.spec = kubernetes.client.V1PodSpec(containers=[
            kubernetes.client.V1Container(name=name, image=image, command=['echo', 'Hello World'])
        ], restart_policy="OnFailure")

        data = kubernetes.client.V1Job()
        data.api_version = 'batch/v1'
        data.kind = 'Job'
        data.metadata = meta
        data.spec = spec

Yep, I see where i went wrong @justinwp. closing issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

doremi666 picture doremi666  路  4Comments

karmab picture karmab  路  5Comments

bjaworski3 picture bjaworski3  路  5Comments

pzoxiuv picture pzoxiuv  路  5Comments

tdigangi picture tdigangi  路  4Comments