From bf214e7a9cc695060d754ee7724605e5f1d4905a Mon Sep 17 00:00:00 2001 From: Swapnil Bobade Date: Wed, 1 Feb 2023 17:25:30 +0530 Subject: [PATCH] Cronjob automation Signed-off-by: Swapnil Bobade --- README.md | 1 + examples/all.yaml | 9 +++ playbooks/main.yml | 3 + playbooks/ocp-cronjob.yml | 5 ++ playbooks/roles/ocp-cronjob/README.md | 66 +++++++++++++++++ playbooks/roles/ocp-cronjob/defaults/main.yml | 15 ++++ playbooks/roles/ocp-cronjob/tasks/main.yml | 70 +++++++++++++++++++ .../ocp-cronjob/templates/cron-job.yml.j2 | 29 ++++++++ 8 files changed, 198 insertions(+) create mode 100644 playbooks/ocp-cronjob.yml create mode 100644 playbooks/roles/ocp-cronjob/README.md create mode 100644 playbooks/roles/ocp-cronjob/defaults/main.yml create mode 100644 playbooks/roles/ocp-cronjob/tasks/main.yml create mode 100644 playbooks/roles/ocp-cronjob/templates/cron-job.yml.j2 diff --git a/README.md b/README.md index 6c75d839b..da430b722 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ This repository consists of additional ansible playbooks for the following: 1. Configure and run verification-tests 1. Enabling Stream Control Transmission Protocol (SCTP) and run e2e 1. Deploy Kubernetes NMState Operator and run e2e +1. Cronjob automation ## Assumptions: diff --git a/examples/all.yaml b/examples/all.yaml index caa921c94..2bfb59000 100644 --- a/examples/all.yaml +++ b/examples/all.yaml @@ -259,6 +259,15 @@ fio_git_token: "" fio_git_branch: "" fio_cleanup: true +#cron-job vars +enable_cronjob: false # Set true to enable the cronjob +cronjob_schedule: "* * * * *" +cronjob_concurrencyPolicy: "Allow" +cronjob_startingDeadlineSeconds: +cronjob_suspend: +cronjob_successfulJobsHistoryLimit: +cronjob_failedJobsHistoryLimit: + #ocp-verification-tests vars verification_enabled: false verification_dir: "/root/verification-tests" diff --git a/playbooks/main.yml b/playbooks/main.yml index 8718899e1..9db969e9f 100644 --- a/playbooks/main.yml +++ b/playbooks/main.yml @@ -86,6 +86,9 @@ - import_playbook: request-header-identity-provider.yml when: request_header_provider is defined and request_header_provider +- import_playbook: ocp-cronjob.yml + when: enable_cronjob is defined and enable_cronjob + - import_playbook: ocp-descheduler.yml when: descheduler_role_enable is defined and descheduler_role_enable diff --git a/playbooks/ocp-cronjob.yml b/playbooks/ocp-cronjob.yml new file mode 100644 index 000000000..51509648a --- /dev/null +++ b/playbooks/ocp-cronjob.yml @@ -0,0 +1,5 @@ +--- +- name: Start cronjob + hosts: bastion + roles: + - ocp-cronjob diff --git a/playbooks/roles/ocp-cronjob/README.md b/playbooks/roles/ocp-cronjob/README.md new file mode 100644 index 000000000..3937a2b20 --- /dev/null +++ b/playbooks/roles/ocp-cronjob/README.md @@ -0,0 +1,66 @@ +OCP Cronjob creation +======================================= + +The role *"roles/ocp-cronjob"* provides automated creation of cronjobs. User can add cronjob value in the role variables for creation **Role Variables** section. +This role will create the cronjob and we can monitor the jobs usig oc commands + + +Requirements +------------ + + - Running OCP 4.x cluster. + + +Role Variables +-------------- + +| Variable | Required | Default |Comments | +|-------------------------------------|----------|---------------------------|---------------------------------------------------------------------------------------------------------------------------------| +| enable_cronjob | no | false | Flag to be set to true to enable cronjob | +| cronjob_schedule | no | * * * * * | schedule field for the job is specified in cron format. Here, the job run every minute | +| cronjob_concurrencyPolicy | no | Allow | specifies how the job controller treats concurrent jobs within a cron job, values can be - ["Allow","forbid","replace"] | +| cronjob_startingDeadlineSeconds | no | 200 | starting deadline specifies a deadline (in seconds) for starting the job | +| cronjob_suspend | no | false | prevent subsequent runs of the cron job | +| cronjob_successfulJobsHistoryLimit | no | 3 | store the successful jobs history limit | +| cronjob_failedJobsHistoryLimit | no | 2 | store the failed jobs history limit | +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| + + +Dependencies +------------ + + - None + + +Example Playbook +---------------- + + - name: Start cronjob + hosts: bastion + roles: + - ocp-cronjob + + +Steps to run playbook +---------------------- + + - Copy the ocp4-playbooks-extras/examples/inventory to the home/working directory + - To execute the playbook run the below sample command + + +Sample Command +--------------- + +ansible-playbook -i inventory -e @examples/all.yaml ~/ocp4-playbooks-extras/playbooks/ocp-cronjob.yml + + +License +------- + +See LICENCE.txt + + +Author Information +------------------ + +Swapnil Bobade (swapnil.bobade@ibm.com) diff --git a/playbooks/roles/ocp-cronjob/defaults/main.yml b/playbooks/roles/ocp-cronjob/defaults/main.yml new file mode 100644 index 000000000..6c3063a8e --- /dev/null +++ b/playbooks/roles/ocp-cronjob/defaults/main.yml @@ -0,0 +1,15 @@ +--- +# defaults file for playbooks/roles/ocp-cronjob +# all these values can be updated as needed +container_name: "busybox" +container_image: "busybox" +cronjob_namespace: "openshift-cronjob" +delete_cronjob: false +job_name: "cronjob" +enable_cronjob: false +cronjob_schedule: "*/1 * * * *" +cronjob_concurrencyPolicy: "Allow" +cronjob_startingDeadlineSeconds: 200 +cronjob_suspend: false +cronjob_successfulJobsHistoryLimit: 4 +cronjob_failedJobsHistoryLimit: 2 diff --git a/playbooks/roles/ocp-cronjob/tasks/main.yml b/playbooks/roles/ocp-cronjob/tasks/main.yml new file mode 100644 index 000000000..0b0e5d8ac --- /dev/null +++ b/playbooks/roles/ocp-cronjob/tasks/main.yml @@ -0,0 +1,70 @@ +--- +# Creating namespace for cronjob +- name: Create namespace for cronjob + k8s: + state: present + definition: + apiVersion: v1 + kind: Namespace + metadata: + name: "{{ cronjob_namespace }}" + +# create cronjobs +- name: Create Cronjobs + block: + - name: Create Cronjob + template: + src: "{{ role_path }}/templates/cron-job.yml.j2" + dest: "{{ role_path }}/files/cronjob.yml" + + - name: Apply the cronjob + shell: oc apply -f "{{ role_path }}/files/cronjob.yml" + + - name: Check if pods are created + shell: oc get pods -n {{ cronjob_namespace }} --no-headers | wc -l + register: pod_count + until: pod_count.stdout|int > 0 + retries: 3 + delay: 30 + + - name: Check pod status + shell: oc wait -l parent=cronjobpi --for=condition=Ready pods + register: pod_status + + - debug: + msg: "{{ pod_status.stdout_lines }}" + + - name: Check if jobs are getting created + shell: oc get jobs -n {{ cronjob_namespace }} --no-headers | wc -l + register: job_count + until: job_count.stdout|int > 0 + retries: 3 + delay: 30 + + - name: Check if jobs are running + shell: oc get jobs -n {{ cronjob_namespace }} + register: output_jobs + + - name: Check if pods are running + shell: oc get pods -n {{ cronjob_namespace }} | grep "Completed\|Running" + register: output_pods + + - name: Fail if pods are not getting created + fail: + msg: "Pods are not getting created !" + when: pod_count.stdout|int == 0 + + - name: Fail if jobs are not getting created + fail: + msg: "Jobs are not getting created !" + when: job_count.stdout|int == 0 + + - debug: + msg: "{{ output_jobs.stdout_lines }}" + + - debug: + msg: "{{ output_pods.stdout_lines }}" + + - name: Delete cronjob if requested + shell: oc delete -f "{{ role_path }}/files/cronjob.yml" + when: delete_cronjob diff --git a/playbooks/roles/ocp-cronjob/templates/cron-job.yml.j2 b/playbooks/roles/ocp-cronjob/templates/cron-job.yml.j2 new file mode 100644 index 000000000..a35a8e2d1 --- /dev/null +++ b/playbooks/roles/ocp-cronjob/templates/cron-job.yml.j2 @@ -0,0 +1,29 @@ +--- +apiVersion: batch/v1 +kind: CronJob +metadata: + name: {{ job_name }} + namespace: "{{ cronjob_namespace }}" +spec: + schedule: "{{ cronjob_schedule }}" + concurrencyPolicy: "{{ cronjob_concurrencyPolicy }}" + startingDeadlineSeconds: {{ cronjob_startingDeadlineSeconds }} + suspend: {{ cronjob_suspend }} + successfulJobsHistoryLimit: {{ cronjob_successfulJobsHistoryLimit }} + failedJobsHistoryLimit: {{ cronjob_failedJobsHistoryLimit }} + jobTemplate: + spec: + template: + metadata: + labels: + parent: "cronjobpi" + spec: + containers: + - name: {{ container_name }} + image: {{ container_image }} +{% if cronjob_concurrencyPolicy or cronjob_startingDeadlineSeconds %} + command: ["sleep", "80"] +{% else %} + command: ["echo", "Hello Openshift !!"] +{% endif %} + restartPolicy: Never