Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
kubernetes-client javascript - create job from cronjob

What will be the equivalent for creating a job:

kubectl -n my-ns create job --from=cronjob/my-cron-job my-job

using javascript kubernetes-client ?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

No, unfortunately you can't.

Please check my old answer in Create job from cronjob in kubernetes .yaml file question.

In short: you are able to do that (create job from cronjob) only using kubectl cli


Way to create regular job, without --from=cronjob:

const k8s = require('@kubernetes/client-node');
const kc = new k8s.KubeConfig();

kc.loadFromCluster();

const k8sBatchV1Api = kc.makeApiClient(k8s.BatchV1Api);

//creation of the job
k8sBatchV1Api.createNamespacedJob('dev', {
    apiVersion: 'batch/v1',
    kind: 'Job',
    metadata: {
    ...
       }
    },
    spec: {
    ...
          }
       }
    }}).catch(e => console.log(e))

Example was taken from @Popopame answer in How to create a Job and a secret from Kubernetes Javascript Client

over 4 years ago · Santiago Trujillo Denunciar

0

Found the solution to my question, with the help of this comment

This is how it looks like with javascript kubernetes-client, used in a pod inside the cluster (rbac permission might be needed, im running on k8s docker-desktop so didnt need):

const createJobFromCronJob = async (
    cronJobNamespace: string,
    cronJobName: string,
    jobNamespace: string,
    jobName: string) => {
    // next 4 lines can be initialized somewhere else
    const kubeConfig = new k8s.KubeConfig();
    kubeConfig.loadFromCluster();
    const batchV1Api = kubeConfig.makeApiClient(k8s.BatchV1Api);
    const batchV1beta1Api = kubeConfig.makeApiClient(k8s.BatchV1beta1Api);
    try {
        const cronJob = await batchV1beta1Api.readNamespacedCronJob(cronJobName, cronJobNamespace);
        const cronJobSpec = cronJob.body.spec.jobTemplate.spec;
        const job = new k8s.V1Job();
        const metadata = new k8s.V1ObjectMeta();
        job.apiVersion = 'batch/v1';
        job.kind = 'Job';
        job.spec = cronJobSpec;
        metadata.name = jobName;
        metadata.annotations = {
            'cronjob.kubernetes.io/instantiate': 'manual',
        };
        job.metadata = metadata;
        const result = await batchV1Api.createNamespacedJob(jobNamespace, job);
        console.log('job created');
    } catch (err) {
        console.error(`failed to create job: ${err.message}`);
        throw err;
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda