<p>I am creating a Kubernetes Job within NodeJS class After importing the library <code>@kubernetes/client-node</code>, I created an object to use the module <code>BatchV1Api</code> inside the function which I am exporting to other class in which I have defined the body of the Kubernetes job like this:</p>
<p>//listJobs.js</p>
<pre><code>import { post } from '../kubeClient.js';

const kubeRoute = async (ctx) =&gt; {
    const newJob = {
        metadata: {
            name: 'countdown',
        },
        spec: {
            template: {
                metadata: {
                    name: 'countdown',
                },
            },
            spec: {
                containers: [
                    {
                        name: 'counter',
                        image: 'centos:7',
                        command: 'bin/bash, -c, for i in 9 8 7 6 5 4 3 2 1 ; do echo $i ; done',
                    }],
                restartPolicy: 'Never',
            },
        },
    };

    const kubeClient = post();
    kubeClient.createNamespacedJob('default', newJob);
    ctx.body = {
        // listConfigMap: (await kubeClient.listConfigMapForAllNamespaces()).body,
        listJobs: (await kubeClient.listJobForAllNamespaces()).body,
        // listService: (await kubeClient.listServiceForAllNamespaces()).body,
    };
};

export default kubeRoute;
</code></pre>
<p>Then I created a router class to request the post method like:</p>
<pre><code>import post from './listJobs.js';

const apiRouter = new Router();
apiRouter.post('/api/v1/newJob', post);
</code></pre>
<p>when executing the application and requesting the route <code>localhost:3000/api/v1/newJob</code> as a post request in postman, it is showing status code <code>422</code> (with some very long output, as in the screenshot) in the vs code terminal and some Kubernetes information in postman body, but it is not creating any job or pod.</p>
<p><a href="https://i.stack.imgur.com/nQlk2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nQlk2.png" alt="KubeJobTerminalError" /></a></p>
<p>Does anyone have any idea, why there is <code>422</code> code at the end?</p>

<p>Status code 422 <strong>Unprocessable Entity</strong> means that server understand the content type, and the syntax of the request is correct, but it was unable to process the contained instructions.</p>
<p>In your case though, the Job manifest looks off.</p>
<p>I'm not an expert in JavaScript kubernetes client, but <code>newJob</code> body looks weird. The resulting yaml should look like this</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: batch/v1
kind: Job
metadata:
  name: countdown
spec:
  template:
    spec:
      containers:
      - name: counter
        image: centos:7
        command: 'bin/bash, -c, for i in {9..1} ; do echo $i ; done' #fixed this one for you
      restartPolicy: Never
</code></pre>
<p>In your case the second <code>spec</code> is a child of <code>spec</code>. It should be a child of <code>template</code>, so:</p>
<pre class="lang-json prettyprint-override"><code>{
  &quot;metadata&quot;: {
    &quot;name&quot;: &quot;countdown&quot;
  },
  &quot;spec&quot;: {
    &quot;template&quot;: {
      &quot;spec&quot;: {
        &quot;containers&quot;: [
          {
            &quot;name&quot;: &quot;counter&quot;,
            &quot;image&quot;: &quot;centos:7&quot;,
            &quot;command&quot;: &quot;bin/bash, -c, for i in {9..1} ; do echo $i ; done&quot;
          }
        ],
        &quot;restartPolicy&quot;: &quot;Never&quot;
      }
    }
  }
}
</code></pre>
