gpt4 book ai didi

python - 尝试创建 Kubernetes 作业时出错

转载 作者:太空宇宙 更新时间:2023-11-03 11:15:16 27 4
gpt4 key购买 nike

我已经定义了这个方法,它应该为我创建一个 Kubernetes 作业。

def make_job():
job = client.V1Job()
job.metadata = client.V1ObjectMeta()
job.metadata.name = "process"
job.spec = client.V1JobSpec()
job.spec.template = client.V1PodTemplate()
job.spec.template.spec = client.V1PodTemplateSpec()
job.spec.template.spec.restart_policy = "Never"
job.spec.template.spec.containers = [
make_container()
]
return job

但是,它会在此行返回错误。

job.spec = client.V1JobSpec()

ValueError: Invalid value for `template`, must not be `None`

我想知道我是否在这里做错了什么,如果是,我在这里做错了什么?

编辑:

我已经通过此更改解决了错误

job.spec = client.V1JobSpec(template=client.V1PodTemplate)

最佳答案

正如您已经知道的那样,如果不注入(inject)其模板,就不可能构建一个Job.Spec,这是在 Job documentation 中固定不变的东西。

The .spec.template is the only required field of the .spec.

The .spec.template is a pod template. It has exactly the same schema as a pod, except it is nested and does not have an apiVersion or kind.

查看 V1JobSpec 的 Python Kubernetes 客户端实现,可以验证 spec 属性是否标记为 non-optional,与其他属性相反。

因此,通过预先构造 template 并在构造 JobSpec 时注入(inject)它确实解决了这个问题:

job.spec.template = client.V1PodTemplate()
job.spec.template.spec = client.V1PodTemplateSpec()
job.spec.template.spec.restart_policy = "Never"
job.spec.template.spec.containers = [
make_container()
]

job.spec = client.V1JobSpec()

根据这个推理,在更高范围内不适用于 JobSpec 属性似乎很奇怪,因为它是Job 对象的定义。

但再次查看客户端的 documentation,可以观察到 Spec 属性被标记为可选,这解释了为什么我们能够创建一个 Job 实例,无需注入(inject) Spec

关于python - 尝试创建 Kubernetes 作业时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53120110/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com