使用 YAML 文件中的参数执行 Python 代码
Execute Python code with arguments inside YAML file
我有一个 python 代码,我在其中使用 CLICK 包将参数传递给函数。我已经对这段代码进行了 docker 化,并使用 yaml 文件中的图像将其部署在我的 Windows 机器上的 minikube 中。它在没有参数的情况下工作正常,但是通过参数传递,它给出了字段不可变错误。
import click
@click.command()
@click.option(
"--host_name",
required=True,
help="The host name of the db",
)
@click.option(
"--port",
required=True,
help="Port where db is deployed",
)
def cli_function(host_name, port):
CONNECTION = (
" host="
+ host_name
+ " port="
+ port
........
)
if __name__ == "__main__":
cli_function()
YAML 文件
spec:
containers:
- name: database
image: docker image
imagePullPolicy: Always
command: ["/bin/bash", "-c"]
args:
[
"python3 /database_connection.py --host_name db-service --port 5432;",
]
完全错误
The Job "timescaledbingestor" is invalid: spec.template: Invalid value: core.PodTemplateSpec{ObjectMeta:v1.ObjectMeta{Name:"", GenerateName:"", Namespace:"", SelfLink:"", UID:"", ResourceVersion:"", Generation:0, CreationTimestamp:v1.Time{Time:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)}}, DeletionTimestamp:(*v1.Time)(nil), DeletionGracePeriodSeconds:(*int64)(nil), Labels:map[string]string{"app":"timescaledbingestor", "controller-uid":"407ae300-1455-4e41-bf88-ca7e01fe3d70", "job-name":"timescaledbingestor"}, Annotations:map[string]string(nil), OwnerReferences:[]v1.OwnerReference(nil), Finalizers:[]string(nil), ClusterName:"", ManagedFields:[]v1.ManagedFieldsEntry(nil)}, Spec:core.PodSpec{Volumes:[]core.Volume(nil), InitContainers:[]core.Container(nil), Containers:[]core.Container{core.Container{Name:"timescaledbingestor-v1", Image:"628548651667.dkr.ecr.us-west-2.amazonaws.com/timescaledb-ingester", Command:[]string{"bash", "-c", "python3 data_sync.py --host_name timescaledb-service --port 5432 --user postgres --password password"}, Args:[]string(nil), WorkingDir:"", Ports:[]core.ContainerPort(nil), EnvFrom:[]core.EnvFromSource(nil), Env:[]core.EnvVar(nil), Resources:core.ResourceRequirements{Limits:core.ResourceList(nil), Requests:core.ResourceList(nil)}, VolumeMounts:[]core.VolumeMount(nil), VolumeDevices:[]core.VolumeDevice(nil), LivenessProbe:(*core.Probe)(nil), ReadinessProbe:(*core.Probe)(nil), StartupProbe:(*core.Probe)(nil), Lifecycle:(*core.Lifecycle)(nil), TerminationMessagePath:"/dev/termination-log", TerminationMessagePolicy:"File", ImagePullPolicy:"Always", SecurityContext:(*core.SecurityContext)(nil), Stdin:false, StdinOnce:false, TTY:false}}, EphemeralContainers:[]core.EphemeralContainer(nil), RestartPolicy:"Never", TerminationGracePeriodSeconds:(*int64)(0xc00b865cb0), ActiveDeadlineSeconds:(*int64)(nil), DNSPolicy:"ClusterFirst", NodeSelector:map[string]string(nil), ServiceAccountName:"", AutomountServiceAccountToken:(*bool)(nil), NodeName:"", SecurityContext:(*core.PodSecurityContext)(0xc005cd7f00), ImagePullSecrets:[]core.LocalObjectReference(nil), Hostname:"", Subdomain:"", SetHostnameAsFQDN:(*bool)(nil), Affinity:(*core.Affinity)(nil), SchedulerName:"default-scheduler", Tolerations:[]core.Toleration(nil), HostAliases:[]core.HostAlias(nil), PriorityClassName:"", Priority:(*int32)(nil), PreemptionPolicy:(*core.PreemptionPolicy)(nil), DNSConfig:(*core.PodDNSConfig)(nil), ReadinessGates:[]core.PodReadinessGate(nil), RuntimeClassName:(*string)(nil), Overhead:core.ResourceList(nil), EnableServiceLinks:(*bool)(nil), TopologySpreadConstraints:[]core.TopologySpreadConstraint(nil)}}: field is immutable
问题
问题的作者对他的工作做了一些改变。更新后,他收到“字段不可变”错误。
这是由于.spec.template
field in Job is immutable无法更新造成的
解决方案
删除旧作业并创建一个进行必要更改的新作业。
我有一个 python 代码,我在其中使用 CLICK 包将参数传递给函数。我已经对这段代码进行了 docker 化,并使用 yaml 文件中的图像将其部署在我的 Windows 机器上的 minikube 中。它在没有参数的情况下工作正常,但是通过参数传递,它给出了字段不可变错误。
import click
@click.command()
@click.option(
"--host_name",
required=True,
help="The host name of the db",
)
@click.option(
"--port",
required=True,
help="Port where db is deployed",
)
def cli_function(host_name, port):
CONNECTION = (
" host="
+ host_name
+ " port="
+ port
........
)
if __name__ == "__main__":
cli_function()
YAML 文件
spec:
containers:
- name: database
image: docker image
imagePullPolicy: Always
command: ["/bin/bash", "-c"]
args:
[
"python3 /database_connection.py --host_name db-service --port 5432;",
]
完全错误
The Job "timescaledbingestor" is invalid: spec.template: Invalid value: core.PodTemplateSpec{ObjectMeta:v1.ObjectMeta{Name:"", GenerateName:"", Namespace:"", SelfLink:"", UID:"", ResourceVersion:"", Generation:0, CreationTimestamp:v1.Time{Time:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)}}, DeletionTimestamp:(*v1.Time)(nil), DeletionGracePeriodSeconds:(*int64)(nil), Labels:map[string]string{"app":"timescaledbingestor", "controller-uid":"407ae300-1455-4e41-bf88-ca7e01fe3d70", "job-name":"timescaledbingestor"}, Annotations:map[string]string(nil), OwnerReferences:[]v1.OwnerReference(nil), Finalizers:[]string(nil), ClusterName:"", ManagedFields:[]v1.ManagedFieldsEntry(nil)}, Spec:core.PodSpec{Volumes:[]core.Volume(nil), InitContainers:[]core.Container(nil), Containers:[]core.Container{core.Container{Name:"timescaledbingestor-v1", Image:"628548651667.dkr.ecr.us-west-2.amazonaws.com/timescaledb-ingester", Command:[]string{"bash", "-c", "python3 data_sync.py --host_name timescaledb-service --port 5432 --user postgres --password password"}, Args:[]string(nil), WorkingDir:"", Ports:[]core.ContainerPort(nil), EnvFrom:[]core.EnvFromSource(nil), Env:[]core.EnvVar(nil), Resources:core.ResourceRequirements{Limits:core.ResourceList(nil), Requests:core.ResourceList(nil)}, VolumeMounts:[]core.VolumeMount(nil), VolumeDevices:[]core.VolumeDevice(nil), LivenessProbe:(*core.Probe)(nil), ReadinessProbe:(*core.Probe)(nil), StartupProbe:(*core.Probe)(nil), Lifecycle:(*core.Lifecycle)(nil), TerminationMessagePath:"/dev/termination-log", TerminationMessagePolicy:"File", ImagePullPolicy:"Always", SecurityContext:(*core.SecurityContext)(nil), Stdin:false, StdinOnce:false, TTY:false}}, EphemeralContainers:[]core.EphemeralContainer(nil), RestartPolicy:"Never", TerminationGracePeriodSeconds:(*int64)(0xc00b865cb0), ActiveDeadlineSeconds:(*int64)(nil), DNSPolicy:"ClusterFirst", NodeSelector:map[string]string(nil), ServiceAccountName:"", AutomountServiceAccountToken:(*bool)(nil), NodeName:"", SecurityContext:(*core.PodSecurityContext)(0xc005cd7f00), ImagePullSecrets:[]core.LocalObjectReference(nil), Hostname:"", Subdomain:"", SetHostnameAsFQDN:(*bool)(nil), Affinity:(*core.Affinity)(nil), SchedulerName:"default-scheduler", Tolerations:[]core.Toleration(nil), HostAliases:[]core.HostAlias(nil), PriorityClassName:"", Priority:(*int32)(nil), PreemptionPolicy:(*core.PreemptionPolicy)(nil), DNSConfig:(*core.PodDNSConfig)(nil), ReadinessGates:[]core.PodReadinessGate(nil), RuntimeClassName:(*string)(nil), Overhead:core.ResourceList(nil), EnableServiceLinks:(*bool)(nil), TopologySpreadConstraints:[]core.TopologySpreadConstraint(nil)}}: field is immutable
问题
问题的作者对他的工作做了一些改变。更新后,他收到“字段不可变”错误。
这是由于.spec.template
field in Job is immutable无法更新造成的
解决方案
删除旧作业并创建一个进行必要更改的新作业。