Java代码从群集手动触发kubernetes cronjob

yzuktlbb  于 2023-01-16  发布在  Kubernetes
关注(0)|答案(1)|浏览(224)

我尝试使用fabric8库手动触发cronjob(未计划),但收到以下错误:

Caused by: io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: POST at: https://172.20.0.1:443/apis/batch/v1/
namespaces/engineering/jobs. Message: Job.batch "app-chat-manual-947171" is invalid: spec.template.spec.containers[0].name: Re
quired value. Received status: Status(apiVersion=v1, code=422, details=StatusDetails(causes=[StatusCause(field=spec.template.spec.co
ntainers[0].name, message=Required value, reason=FieldValueRequired, additionalProperties={})], group=batch, kind=Job, name=ap
p-chat-manual-947171, retryAfterSeconds=null, uid=null, additionalProperties={}), kind=Status, message=Job.batch "app-chat-man
ual-947171" is invalid: spec.template.spec.containers[0].name: Required value, metadata=ListMeta(_continue=null, remainingItemCount=
null, resourceVersion=null, selfLink=null, additionalProperties={}), reason=Invalid, status=Failure, additionalProperties={}).

我的代码正在集群上运行:
Maven依赖性:

<dependency>
            <groupId>io.fabric8</groupId>
            <artifactId>kubernetes-client</artifactId>
            <version>6.3.1</version>
</dependency>

java代码:

public static void triggerCronjob(String cronjobName, String applicableNamespace) {
        KubernetesClient kubernetesClient = new KubernetesClientBuilder().build();

        final String podName = String.format("%s-manual-%s", cronjobName.length() > 38 ? cronjobName.substring(0, 38) : cronjobName,
                new Random().nextInt(999999));

        System.out.println("triggerCronjob method invoked, applicableNamespace: " + applicableNamespace
                + ", cronjobName: " + cronjobName + ", podName: " + podName);

        Job job = new JobBuilder()
                .withApiVersion("batch/v1")
                .withNewMetadata()
                .withName(podName)
                .endMetadata()
                .withNewSpec()
                .withBackoffLimit(4)
                .withNewTemplate()
                .withNewSpec()
                .addNewContainer()
                .withName(podName)
                .withImage("perl")
                .withCommand("perl", "-Mbignum=bpi", "-wle", "print bpi(2000)")
                .endContainer()
                .withRestartPolicy("Never")
                .endSpec()
                .endTemplate()
                .endSpec().build();

        kubernetesClient.batch().v1().jobs().inNamespace(applicableNamespace).createOrReplace(job);
        kubernetesClient.close();
        System.out.println("CronJob triggered: applicableNamespace: " + applicableNamespace + ", cronjob name: " + cronjobName);
    }

在kubernetes群集上执行的代码,但不构成应用程序,它是在群集中运行的外部程序。
我的目标是触发给定名称空间中的给定作业。

qaxu7uf2

qaxu7uf21#

错误消息表明Kubernetes API返回状态代码422,这表明您尝试创建的作业对象无效。具体来说,它表明“spec.template.spec.containers[0].name”字段是必需的,但在您尝试创建的作业对象中不存在。
您可以尝试在创建JobBuilder对象时添加容器名称,如下所示:

.addNewContainer()
.withName("container-name")
.withImage("perl")
.withCommand("perl", "-Mbignum=bpi", "-wle", "print bpi(2000)")
.endContainer()

这应该可以解决问题。

相关问题