简体   繁体   English

在 YAML 文件中使用 arguments 执行 Python 代码

[英]Execute Python code with arguments inside YAML file

I have a python code where I am passing arguments to a function using CLICK package.我有一个 python 代码,其中我使用 CLICK ZEFE90A8E604A7C840E888D03 将 arguments 传递给 function。 I have dockerized this code and using the image inside a yaml file to deploy this inside minikube on my Windows machine.我已将此代码 docker 化,并使用 yaml 文件中的图像将其部署在我的 Windows 机器上的 minikube 中。 It worked fine without the arguments but with argument passing, it is giving field immutable error.它在没有 arguments 的情况下工作正常,但通过参数传递,它给出了字段不可变错误。

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 file 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;",
            ]

Complete Error完全错误

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

Problem问题

Author of the question made some changes in his Job.问题的作者在他的工作中做了一些改变。 After the update he got a "field is immutable" error.更新后他得到一个“字段是不可变的”错误。

This is caused by the fact that .spec.template field in Job is immutable and can not be updated.这是因为 Job 中的.spec.template 字段是不可变的,无法更新。

Solution解决方案

Delete the old Job and create a new one with necessary changes.删除旧的 Job 并创建一个带有必要更改的新 Job。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM