通过Kubernetes Python SDK创建Cron作业时遇到问题

时间:2019-06-27 09:19:41

标签: python kubernetes

我编写了一个Python函数,用于通过kubernetes SDK创建cron作业。但出现以下错误:

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "Job.batch \"ewantest3\" is invalid: [spec.template.spec.containers: Required value, spec.template.spec.restartPolicy: Unsupported value: \"Always\": supported values: \"OnFailure\", \"Never\"]",
  "reason": "Invalid",
  "details": {
    "name": "ewantest3",
    "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
}

代码:

def kube_create_job_object(name, cron, container_image, namespace="default", container_name="jobcontainer", env_vars={}):

    # Passing Arguments in Env:
    env_list = []
    for env_name, env_value in env_vars.items():
        env_list.append(client.V1EnvVar(name=env_name, value=env_value))

    container = client.V1Container(name=container_name, image=container_image, env=env_list, image_pull_policy='Always')
    job_template = client.V1beta1CronJob()
    job_template_spec = client.V1PodSpec(containers=[container], restart_policy='OnFailure')
    job_template_spec.completions = 10
    job_template_spec.concurrency_policy = 'Allow'
    job_template_spec.auto_selector = True
    job_template_spec.parallelism = 10

    job_spec_template = client.V1PodTemplateSpec(spec=job_template_spec)
    job_template.spec = client.V1JobSpec(template=job_spec_template)

    job_spec = client.V1beta1CronJobSpec(schedule=cron, job_template=job_template)

    body = client.V1beta1CronJob(spec=job_spec)
    body.metadata = client.V1ObjectMeta(namespace=namespace, name=name)
    body.status = client.V1beta1CronJobStatus()

    print(body)

    return body

我确定这与实例化模板块的顺序有关,但是我一直在努力找出正确的顺序。据我所知,我正在定义正确的重新启动策略以及容器等。

0 个答案:

没有答案