Skip to content

Commit

Permalink
Cronjob automation
Browse files Browse the repository at this point in the history
Signed-off-by: Swapnil Bobade <[email protected]>
  • Loading branch information
swapnil-bobade27 committed Jan 1, 2023
1 parent d631cef commit 926d0ec
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 1 deletion.
9 changes: 9 additions & 0 deletions examples/all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,12 @@ fio_git_username: ""
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:
4 changes: 3 additions & 1 deletion playbooks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
- 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

5 changes: 5 additions & 0 deletions playbooks/ocp-cronjob.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Start cronjob
hosts: bastion
roles:
- ocp-cronjob
66 changes: 66 additions & 0 deletions playbooks/roles/ocp-cronjob/README.md
Original file line number Diff line number Diff line change
@@ -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 | * * * * * | can be updated as needed | |
| cronjob_concurrencyPolicy | no | Allow | Value can be anything from ["Allow","forbid","replace"] |
| cronjob_startingDeadlineSeconds | no | 200 | Can be updated as needed |
| cronjob_suspend | no | false | can be true or false |
| cronjob_successfulJobsHistoryLimit | no | 3 | can be updated as needed |
| cronjob_failedJobsHistoryLimit | no | 2 | can be updated as needed |
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


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 ([email protected])
15 changes: 15 additions & 0 deletions playbooks/roles/ocp-cronjob/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions playbooks/roles/ocp-cronjob/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions playbooks/roles/ocp-cronjob/templates/cron-job.yml.j2
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 926d0ec

Please sign in to comment.