Creating a Cron Job

Function

This API is used to create a cron job.

URI

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

Table 1 describes the parameters of this API.

Table 1 Parameter description

Parameter

Mandatory

Description

namespace

Yes

Object name and auth scope, such as for teams and projects.

pretty

No

If 'true', then the output is pretty printed.

Request

Request parameters:

For the description about request parameters, see Table 2.

Table 2 Request parameters

Parameter

Mandatory

Type

Description

apiVersion

Yes

String

APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values.

kind

Yes

String

Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase.

metadata

Yes

metadata object

Standard list metadata.

spec

Yes

spec object

Specification of the desired behavior of a cron job, including the schedule.

status

No

status object

Current status of a cron job.

Table 3 Data structure of the spec field

Parameter

Mandatory

Type

Description

concurrencyPolicy

No

String

Specifies how to treat concurrent executions of a Job. Valid values are: - 'Allow' (default): allows CronJobs to run concurrently; - 'Forbid': forbids concurrent runs, skipping next run if previous run hasn't finished yet; - 'Replace': cancels currently running job and replaces it with a new one.

failedJobsHistoryLimit

No

Integer

The number of failed finished jobs to retain. This is a pointer to distinguish between explicit zero and not specified. Defaults to 1.

jobTemplate

Yes

jobTemplate object

Specifies the job that will be created when executing a CronJob.

schedule

Yes

String

The schedule in Cron format.

startingDeadlineSeconds

No

Integer

Optional deadline in seconds for starting the job if it misses scheduled time for any reason. Missed jobs executions will be counted as failed ones.

successfulJobsHistoryLimit

No

Integer

The number of successful finished jobs to retain. This is a pointer to distinguish between explicit zero and not specified. Defaults to 3.

suspend

No

Boolean

This flag tells the controller to suspend subsequent executions, it does not apply to already started executions. Defaults to false.

Table 4 Data structure of the status field

Parameter

Mandatory

Type

Description

active

No

claimRef object

A list of pointers to currently running jobs.

lastScheduleTime

No

String

Information when was the last time the job was successfully scheduled.

Table 5 Data structure of the jobTemplate field

Parameter

Mandatory

Type

Description

metadata

No

metadata object

Standard object's metadata of the jobs created from this template.

spec

No

spec object

Specification of the desired behavior of the job.

Example request:

{
    "apiVersion": "batch/v1beta1",
    "kind": "CronJob",
    "metadata": {
        "name": "cronjob-test",
        "namespace": "default"
    },
    "spec": {
        "concurrencyPolicy": "Allow",
        "jobTemplate": {
            "spec": {
                "template": {
                    "metadata": {
                        "enable": true,
                        "labels": {
                            "sjname": "cronjob-test"
                        }
                    },
                    "spec": {
                        "containers": [
                            {
                                "image": "nginx:stable-perl",
                                "imagePullPolicy": "IfNotPresent",
                                "lifecycle": {},
                                "name": "container-0"
                            }
                        ],
                        "dnsPolicy": "ClusterFirst",
                        "imagePullSecrets": [
                            {
                                "name": "default-secret"
                            }
                        ],
                        "restartPolicy": "OnFailure"
                    }
                }
            }
        },
        "schedule": "*/59 * * * *"
    }
}

Response

Response parameters:

For the description about response parameters, see Table 2.

Example response:

{
    "apiVersion": "batch/v1beta1",
    "kind": "CronJob",
    "metadata": {
        "annotations": {
            "container.io/container-0": "https://console.huaweicloud.com/swr/dockerimage/nginx.png",
            "description": ""
        },
        "creationTimestamp": "2018-03-07T12:08:52Z",
        "enable": true,
        "name": "cronjob-test",
        "namespace": "default",
        "resourceVersion": "441019",
        "selfLink": "/apis/batch/v1beta1/namespaces/default/cronjobs/cronjob-test",
        "uid": "4d78b666-2200-11e8-96aa-fa163ecd089c"
    },
    "spec": {
        "concurrencyPolicy": "Allow",
        "failedJobsHistoryLimit": 1,
        "jobTemplate": {
            "metadata": {
                "creationTimestamp": null,
                "enable": true
            },
            "spec": {
                "template": {
                    "metadata": {
                        "creationTimestamp": null,
                        "enable": true,
                        "labels": {
                            "sjname": "cronjob-test"
                        }
                    },
                    "spec": {
                        "containers": [
                            {
                                "image": "nginx:stable-perl",
                                "imagePullPolicy": "IfNotPresent",
                                "lifecycle": {},
                                "name": "container-0",
                                "resources": {},
                                "terminationMessagePath": "/dev/termination-log",
                                "terminationMessagePolicy": "File"
                            }
                        ],
                        "dnsPolicy": "ClusterFirst",
                        "imagePullSecrets": [
                            {
                                "name": "default-secret"
                            }
                        ],
                        "restartPolicy": "OnFailure",
                        "schedulerName": "default-scheduler",
                        "securityContext": {},
                        "terminationGracePeriodSeconds": 30
                    }
                }
            }
        },
        "schedule": "*/59 * * * *",
        "successfulJobsHistoryLimit": 3,
        "suspend": false
    },
    "status": {}
}

Status Code

Table 6 describes the status code of this API.

Table 6 Status code

Status Code

Description

201

The request has been fulfilled, resulting in the creation of a new resource.

For the description about error status codes, see Status Code.