Home > OS >  Java code manually Triggering kubernetes cronjob from the cluster
Java code manually Triggering kubernetes cronjob from the cluster

Time:01-17

I'm trying to trigger cronjob manually(not scheduled) using fabric8 library but getting the following error:

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={}).

my code is running at the cluster:

maven dependency:

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

java code:

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);
    }

the code executed at the kubernetes cluster, but not form the application, it's an external program that's running in the cluster.

my goal is to trigger given job in a given namespace.

CodePudding user response:

The error message is indicating that the Kubernetes API is returning a status code of 422, which is indicating that the Job object you are trying to create is invalid. Specifically, it is saying that the "spec.template.spec.containers[0].name" field is required but is not present in the Job object you are trying to create.

You can try adding a container name while creating JobBuilder object like this:

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

This should fix the issue.

CodePudding user response:

If you want to trigger an already existing CronJob, you need to provide ownerReference for the existing CronJob in Job:

// Get already existing CronJob
CronJob cronJob = kubernetesClient.batch().v1()
       .cronjobs()
       .inNamespace(namespace)
       .withName(cronJobName)
       .get();

// Create new Job object referencing CronJob
Job newJobToCreate = new JobBuilder()
       .withNewMetadata()
       .withName(jobName)
       .addNewOwnerReference()
       .withApiVersion("batch/v1")
       .withKind("CronJob")
       .withName(cronJob.getMetadata().getName())
       .withUid(cronJob.getMetadata().getUid())
       .endOwnerReference()
       .addToAnnotations("cronjob.kubernetes.io/instantiate", "manual")
       .endMetadata()
       .withSpec(cronJob.getSpec().getJobTemplate().getSpec())
       .build();
// Apply job object to Kubernetes Cluster
kubernetesClient.batch().v1()
       .jobs()
       .inNamespace(namespace)
       .resource(newJobToCreate)
       .create();
  • Related