Cron Job

A Cron Job creates jobs that run on a specific timed interval. You’ll want to create a YAML file describing the cron job:

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: xx-ecr-login-cron
spec:
  schedule: "* */8 * * *"
  successfulJobsHistoryLimit: 2
  failedJobsHistoryLimit: 2
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      backoffLimit: 4
      template:
        spec:
          serviceAccountName: ecr-cron-sa
          terminationGracePeriodSeconds: 0
          restartPolicy: Never
          containers:
          - name: ecr-login
            imagePullPolicy: Always
            image: simoncomputing/ecr-login:1.0
            command:
              - "/login.sh"
            env:
              - name: ECR_URL
                valueFrom:
                  secretKeyRef:
                    name: aws-info
                    key: ecr-url
              - name: AWS_ACCOUNT
                valueFrom:
                  secretKeyRef:
                    name: aws-info
                    key: aws-account
              - name: AWS_REGION
                valueFrom:
                  secretKeyRef:
                    name: aws-info
                    key: region
              - name: NAMESPACES
                value: "default"

This is an example of running a cron job that will create a container to run from a predefined Docker image.

The schedule is in a standard Cron format.

Note that you wil also want to run it with a service account that you’ve created with the appropriate permissions.

k9.batch.list_cron_jobs(namespace: Optional[str] = None)[source]

List all cluster cron jobs :param namespace: Then namespace to list cron jobs from. If None, uses the default namespace. :return: A list of dictionary items with name and created.

k9.batch.create_cron_job(body: dict, namespace: Optional[str] = None)[source]

Create a cron job

Parameters
  • body – The cron job definition object.

  • namespace – Then namespace to create cronjob in. If None, uses the default namespace.

Returns

V1beta1CronJob

k9.batch.delete_cron_job(name: str, namespace: Optional[str] = None)[source]

Deletes the specified cron job

Parameters
  • name – Name of cron job

  • namespace – Then namespace to delete cron job from. If None, uses the default namespace.

Returns

None if cron job doesn’t exist, V1Status

k9.batch.get_cron_job(name: str, namespace: Optional[str] = None)[source]

Gets the specified cron job

Parameters
  • name – Name of cron job

  • namespace – Then namespace to get cron job from. If None, uses the default namespace.

Returns

V1beta1CronJob

k9.batch.cron_job_exists(name: str, namespace: Optional[str] = None)[source]

Checks for the cron job’s existence.

Parameters
  • name – Name of cron job to look for.

  • namespace – Then namespace to check for cron job. If None, uses the default namespace.

Returns

True if specified cron job exists