<p>I'm trying to trigger cronjob manually(not scheduled) using fabric8 library
but getting the following error:</p>
<pre><code>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 &quot;app-chat-manual-947171&quot; 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 &quot;app-chat-man
ual-947171&quot; 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={}).
</code></pre>
<p>my code is running at the cluster:</p>
<p>maven dependency:</p>
<pre><code>&lt;dependency&gt;
            &lt;groupId&gt;io.fabric8&lt;/groupId&gt;
            &lt;artifactId&gt;kubernetes-client&lt;/artifactId&gt;
            &lt;version&gt;6.3.1&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
<p>java code:</p>
<pre><code>public static void triggerCronjob(String cronjobName, String applicableNamespace) {
        KubernetesClient kubernetesClient = new KubernetesClientBuilder().build();

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

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

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

        kubernetesClient.batch().v1().jobs().inNamespace(applicableNamespace).createOrReplace(job);
        kubernetesClient.close();
        System.out.println(&quot;CronJob triggered: applicableNamespace: &quot; + applicableNamespace + &quot;, cronjob name: &quot; + cronjobName);
    }
</code></pre>
<p>the code executed at the kubernetes cluster, but not form the application, it's an external program that's running in the cluster.</p>
<p>my goal is to trigger given job in a given namespace.</p>

Ali Taha
<p>If you want to trigger an already existing CronJob, you need to provide ownerReference for the existing CronJob in Job:</p>
<pre class="lang-java prettyprint-override"><code>// 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(&quot;batch/v1&quot;)
       .withKind(&quot;CronJob&quot;)
       .withName(cronJob.getMetadata().getName())
       .withUid(cronJob.getMetadata().getUid())
       .endOwnerReference()
       .addToAnnotations(&quot;cronjob.kubernetes.io/instantiate&quot;, &quot;manual&quot;)
       .endMetadata()
       .withSpec(cronJob.getSpec().getJobTemplate().getSpec())
       .build();
// Apply job object to Kubernetes Cluster
kubernetesClient.batch().v1()
       .jobs()
       .inNamespace(namespace)
       .resource(newJobToCreate)
       .create();
</code></pre>
