1
votes

I am trying to schedule Jobs in Kubernetes.

https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/

The job can be created using the below command.

$ kubectl create -f ./cronjob.yaml
cronjob "hello" created

Is there any REST API using which the Job can be created from a Java client?

Thanks

2
kubectl is literally just a wrapper around a REST API.Oliver Charlesworth
So, we can hit the rest end point directly. Is there any documentation on the REST end points for Job scheudling?user1578872

2 Answers

3
votes

The respective REST endpoint is described in the official API reference. You will find the CronJob resource in the batch/v1beta1 API group. To create a new CronJob resource, you'll need a POST call to the /apis/batch/v1beta1/namespaces/{namespace}/cronjobs URL.

A respective HTTP request might look something like this:

POST /apis/batch/v1beta1/namespaces/default/cronjobs HTTP/1.1
Content-Type: application/json
Content-Length: ...
Authorization: ...
[other headers]

{
  "metadata": {
    "name": "some-cron"
  },
  "spec": {
    ...
  }
}

There are also older versions of the same resource, for example in the batch/v2alpha1 API group. As a rule of thumb, I'd recommend using the newest API version available to you. Especially, do not rely on alpha APIs in production; they tend to deprecate pretty quickly between releases.

To create a batch/v1beta1 CronJob using the Java client, have a look at the createNamespacedCronJob method of the io.kubernetes.client.openapi.apis.BatchV1beta1Api class.

0
votes

HTTP Request

POST /apis/batch/v1beta1/namespaces/{namespace}/cronjobs

You can take a look here for API overview: cronjob-v1beta1-batch

Add CronJob object in request Body.

{
  "apiVersion": "batch/v1beta1",
  "kind": "CronJob",
  "metadata": {
    "name": "hello"
  },
  "spec": {
    "schedule": "*/1 * * * *",
    "jobTemplate": {

    }
  }
}

Check here its spec: writing-a-cron-job-spec

Part of swagger.json

"post": {
 "description": "create a CronJob",
 "consumes": [
  "*/*"
 ],
 "produces": [
  "application/json",
  "application/yaml",
  "application/vnd.kubernetes.protobuf"
 ],
 "schemes": [
  "https"
 ],
 "tags": [
  "batch_v1beta1"
 ],
 "operationId": "createBatchV1beta1NamespacedCronJob",
 "parameters": [
  {
   "name": "body",
   "in": "body",
   "required": true,
   "schema": {
    "$ref": "#/definitions/io.k8s.api.batch.v1beta1.CronJob"
   }
  }
 ],
 "responses": {
  "200": {
   "description": "OK",
   "schema": {
    "$ref": "#/definitions/io.k8s.api.batch.v1beta1.CronJob"
   }
  },
  "201": {
   "description": "Created",
   "schema": {
    "$ref": "#/definitions/io.k8s.api.batch.v1beta1.CronJob"
   }
  },
  "202": {
   "description": "Accepted",
   "schema": {
    "$ref": "#/definitions/io.k8s.api.batch.v1beta1.CronJob"
   }
  },
  "401": {
   "description": "Unauthorized"
  }
 },
 "x-kubernetes-action": "post",
 "x-kubernetes-group-version-kind": {
  "group": "batch",
  "kind": "CronJob",
  "version": "v1beta1"
 }
}

See full swagger.json