Home > Blockchain >  Kubernetes client Job create
Kubernetes client Job create

Time:06-18

I am using kubernetes client (https://github.com/kubernetes-client/javascript) to create job and I have set up service account for the pod which is creating the job. However, I am getting this error when executing the job creation.

body: {
  kind: 'Status',
  apiVersion: 'v1',
  metadata: {},
  status: 'Failure',
  message: 'Job.batch "compiler-job" is invalid: spec.template.spec.containers: Required value',
  reason: 'Invalid',
  details: {
    name: 'compiler-job',
    group: 'batch',
    kind: 'Job',
    causes: [Array]
  },
  code: 422
},

I am sure there is something wrong with the body options that i am passing to k8sBatchV1Api.createNamespacedJob() but I am not sure what I am doing wrong. Here is the snippet of the manifest.

const kc = new k8s.KubeConfig();
kc.loadFromCluster();
const k8sBatchV1Api = kc.makeApiClient(k8s.BatchV1Api);

k8sBatchV1Api.createNamespacedJob('default', {
    apiVersion: 'batch/v1',
    kind: 'Job',
    metadata: {
        name: 'compiler-job'
    },
    spec: {
        template: {
            metadata: {
                name: 'compiler-job'
            },
            spec: {
                containers: {
                    image: 'perl',
                    name: 'compiler-job',
                    command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
                },
                restartPolicy: "OnFailure"
            }
        }
    }
}).catch((e: any) => console.log(e));

Here is the serviceaccount.yaml file

apiVersion: v1
kind: ServiceAccount
metadata:
  name: create-job
  namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: default
  name: create-job-role
rules:
  - apiGroups: [ "batch", "extensions" ]
    resources: [ "jobs" ]
    verbs: [ "get", "list", "watch", "create", "update", "patch", "delete" ]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: create-job-rolebinding
  namespace: default
subjects:
  - kind: ServiceAccount
    name: create-job
    namespace: default
roleRef:
  kind: ClusterRole
  name: create-job-role
  apiGroup: rbac.authorization.k8s.io

Been spending almost a week and have no clue.

CodePudding user response:

I think that might be because containers should be a list, can you try this?

const kc = new k8s.KubeConfig();
kc.loadFromCluster();
const k8sBatchV1Api = kc.makeApiClient(k8s.BatchV1Api);

k8sBatchV1Api.createNamespacedJob('default', {
    apiVersion: 'batch/v1',
    kind: 'Job',
    metadata: {
        name: 'compiler-job'
    },
    spec: {
        template: {
            metadata: {
                name: 'compiler-job'
            },
            spec: {
                containers: [{
                    image: 'perl',
                    name: 'compiler-job',
                    command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
                }],
                restartPolicy: "OnFailure"
            }
        }
    }
}).catch((e: any) => console.log(e));
  • Related