Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

353
Visualizações
Getting Status code 422 when creating Kubernetes Job using nodejs

I am creating a Kubernetes Job within NodeJS class After importing the library @kubernetes/client-node, I created an object to use the module BatchV1Api inside the function which I am exporting to other class in which I have defined the body of the Kubernetes job like this:

//listJobs.js

import { post } from '../kubeClient.js';

const kubeRoute = async (ctx) => {
    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;

Then I created a router class to request the post method like:

import post from './listJobs.js';

const apiRouter = new Router();
apiRouter.post('/api/v1/newJob', post);

when executing the application and requesting the route localhost:3000/api/v1/newJob as a post request in postman, it is showing status code 422 (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.

KubeJobTerminalError

Does anyone have any idea, why there is 422 code at the end?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Status code 422 Unprocessable Entity means that server understand the content type, and the syntax of the request is correct, but it was unable to process the contained instructions.

In your case though, the Job manifest looks off.

I'm not an expert in JavaScript kubernetes client, but newJob body looks weird. The resulting yaml should look like this

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

In your case the second spec is a child of spec. It should be a child of template, so:

{
  "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"
          }
        ],
        "restartPolicy": "Never"
      }
    }
  }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda