Skip to content

Commit

Permalink
use backoffLimit to actually fail cronjobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Schnitzel committed May 15, 2019
1 parent 356f5c7 commit 735aa79
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 92 deletions.
8 changes: 4 additions & 4 deletions images/commons/lagoon/cronjob.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/bin/sh

exit_trap() {
# We want to exit with 0 so OpenShift does not try to restart us
# see https://docs.openshift.com/container-platform/3.6/dev_guide/jobs.html#creating-a-job-known-issues
exit 0
rv=$?
# TODO: Send Lagoon API information about our CronJob Success or Failure
exit $rv
}

# on exit, always call exit_trap to always exit with 0
# on exit, always call exit_trap
trap exit_trap EXIT

echo "$(date --utc +%FT%TZ) CRONJOB: $@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ parameters:
- name: PERSISTENT_STORAGE_NAME
description: Name of the Persistent Volume Claim to use
required: true
- name: PERSISTENT_STORAGE_CLASS
description: Name of the Storage Class to use
value: ''
- name: PERSISTENT_STORAGE_SIZE
description: Size of the Storage to request
value: '5Gi'
- name: DEPLOYMENT_STRATEGY
description: Strategy of Deploymentconfig
value: "Rolling"
- name: SERVICE_IMAGE
description: Pullable image of service
required: true
- name: CRONJOB_NAME
description: Name of this cronjob
required: true
Expand All @@ -46,12 +58,6 @@ parameters:
- name: CRONJOB_COMMAND
description: Command of this cronjob
required: true
- name: DEPLOYMENT_STRATEGY
description: Strategy of Deploymentconfig
value: "Rolling"
- name: SERVICE_IMAGE
description: Pullable image of service
required: true
objects:
- apiVersion: batch/v1beta1
kind: CronJob
Expand All @@ -60,18 +66,34 @@ objects:
spec:
schedule: "${CRONJOB_SCHEDULE}"
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 1
successfulJobsHistoryLimit: 0
failedJobsHistoryLimit: 1
jobTemplate:
metadata:
labels:
cronjob: ${CRONJOB_NAME}
branch: ${SAFE_BRANCH}
project: ${SAFE_PROJECT}
parent: ${SERVICE_NAME}
spec:
backoffLimit: 1
template:
metadata:
labels:
cronjob: ${CRONJOB_NAME}
branch: ${SAFE_BRANCH}
project: ${SAFE_PROJECT}
parent: cronjob-${SERVICE_NAME}-${CRONJOB_NAME}
parent: ${SERVICE_NAME}
spec:
tolerations:
- effect: NoSchedule
key: autoscaled
operator: Equal
value: 'true'
- effect: NoSchedule
key: appuio.ch/autoscaled
operator: Equal
value: 'true'
volumes:
- name: ${PERSISTENT_STORAGE_NAME}
persistentVolumeClaim:
Expand All @@ -81,28 +103,29 @@ objects:
defaultMode: 420
secretName: lagoon-sshkey
containers:
- name: cronjob-${SERVICE_NAME}-${CRONJOB_NAME}
image: ${SERVICE_IMAGE}
command:
- /lagoon/cronjob.sh
- "${CRONJOB_COMMAND}"
volumeMounts:
- name: ${PERSISTENT_STORAGE_NAME}
mountPath: ${PERSISTENT_STORAGE_PATH}
envFrom:
- configMapRef:
name: lagoon-env
env:
## LAGOON_GIT_SHA is injected directly and not loaded via `lagoon-env` config
## This will cause the cli to redeploy on every deployment, even the files have not changed
- name: LAGOON_GIT_SHA
value: ${LAGOON_GIT_SHA}
- name: SERVICE_NAME
value: ${SERVICE_NAME}
volumeMounts:
- name: ${PERSISTENT_STORAGE_NAME}
mountPath: ${PERSISTENT_STORAGE_PATH}
- mountPath: /var/run/secrets/lagoon/sshkey/
name: lagoon-sshkey
readOnly: true
restartPolicy: OnFailure
- image: ${SERVICE_IMAGE}
command:
- /lagoon/cronjob.sh
- "${CRONJOB_COMMAND}"
name: cronjob-${SERVICE_NAME}-${CRONJOB_NAME}
envFrom:
- configMapRef:
name: lagoon-env
env:
## LAGOON_GIT_SHA is injected directly and not loaded via `lagoon-env` config
## This will cause the cli to redeploy on every deployment, even the files have not changed
- name: LAGOON_GIT_SHA
value: ${LAGOON_GIT_SHA}
- name: SERVICE_NAME
value: ${SERVICE_NAME}
volumeMounts:
- name: ${PERSISTENT_STORAGE_NAME}
mountPath: ${PERSISTENT_STORAGE_PATH}
- mountPath: /var/run/secrets/lagoon/sshkey/
name: lagoon-sshkey
readOnly: true
resources:
requests:
cpu: 10m
memory: 10Mi
restartPolicy: Never
114 changes: 114 additions & 0 deletions images/oc-build-deploy-dind/openshift-templates/cli/custom-cronjob.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
apiVersion: v1
kind: Template
metadata:
creationTimestamp: null
name: lagoon-openshift-template-cli-cronjob
parameters:
- name: SERVICE_NAME
description: Name of this service
required: true
- name: SAFE_BRANCH
description: Which branch this belongs to, special chars replaced with dashes
required: true
- name: SAFE_PROJECT
description: Which project this belongs to, special chars replaced with dashes
required: true
- name: BRANCH
description: Which branch this belongs to, original value
required: true
- name: PROJECT
description: Which project this belongs to, original value
required: true
- name: LAGOON_GIT_SHA
description: git hash sha of the current deployment
required: true
- name: SERVICE_ROUTER_URL
description: URL of the Router for this service
value: ""
- name: OPENSHIFT_PROJECT
description: Name of the Project that this service is in
required: true
- name: REGISTRY
description: Registry where Images are pushed to
required: true
- name: DEPLOYMENT_STRATEGY
description: Strategy of Deploymentconfig
value: "Rolling"
- name: SERVICE_IMAGE
description: Pullable image of service
required: true
- name: CRONJOB_NAME
description: Name of this cronjob
required: true
- name: CRONJOB_SCHEDULE
description: Schedule of this cronjob
required: true
- name: CRONJOB_COMMAND
description: Command of this cronjob
required: true
objects:
- apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: cronjob-${SERVICE_NAME}-${CRONJOB_NAME}
spec:
schedule: "${CRONJOB_SCHEDULE}"
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 0
failedJobsHistoryLimit: 1
jobTemplate:
metadata:
labels:
cronjob: ${CRONJOB_NAME}
branch: ${SAFE_BRANCH}
project: ${SAFE_PROJECT}
parent: ${SERVICE_NAME}
spec:
backoffLimit: 1
template:
metadata:
labels:
cronjob: ${CRONJOB_NAME}
branch: ${SAFE_BRANCH}
project: ${SAFE_PROJECT}
parent: ${SERVICE_NAME}
spec:
tolerations:
- effect: NoSchedule
key: autoscaled
operator: Equal
value: 'true'
- effect: NoSchedule
key: appuio.ch/autoscaled
operator: Equal
value: 'true'
volumes:
- name: lagoon-sshkey
secret:
defaultMode: 420
secretName: lagoon-sshkey
containers:
- image: ${SERVICE_IMAGE}
command:
- /lagoon/cronjob.sh
- "${CRONJOB_COMMAND}"
name: cronjob-${SERVICE_NAME}-${CRONJOB_NAME}
envFrom:
- configMapRef:
name: lagoon-env
env:
## LAGOON_GIT_SHA is injected directly and not loaded via `lagoon-env` config
## This will cause the cli to redeploy on every deployment, even the files have not changed
- name: LAGOON_GIT_SHA
value: ${LAGOON_GIT_SHA}
- name: SERVICE_NAME
value: ${SERVICE_NAME}
volumeMounts:
- mountPath: /var/run/secrets/lagoon/sshkey/
name: lagoon-sshkey
readOnly: true
resources:
requests:
cpu: 10m
memory: 10Mi
restartPolicy: Never
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,24 @@ objects:
spec:
schedule: "${CRONJOB_SCHEDULE}"
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 1
successfulJobsHistoryLimit: 0
failedJobsHistoryLimit: 1
jobTemplate:
metadata:
labels:
cronjob: ${CRONJOB_NAME}
branch: ${SAFE_BRANCH}
project: ${SAFE_PROJECT}
parent: ${SERVICE_NAME}
spec:
backoffLimit: 1
template:
metadata:
labels:
cronjob: ${CRONJOB_NAME}
branch: ${SAFE_BRANCH}
project: ${SAFE_PROJECT}
parent: cronjob-${SERVICE_NAME}-${CRONJOB_NAME}
parent: ${SERVICE_NAME}
spec:
containers:
- name: cronjob-${SERVICE_NAME}-${CRONJOB_NAME}
Expand All @@ -82,4 +89,8 @@ objects:
value: ${LAGOON_GIT_SHA}
- name: SERVICE_NAME
value: ${SERVICE_NAME}
restartPolicy: OnFailure
resources:
requests:
cpu: 10m
memory: 10Mi
restartPolicy: Never
Loading

0 comments on commit 735aa79

Please sign in to comment.