Skip to content

Commit

Permalink
Merge pull request #196 from appuio/feat/cronjobs
Browse files Browse the repository at this point in the history
Add support for deploying arbitrary cronjobs
  • Loading branch information
haasad authored Apr 5, 2024
2 parents a11c106 + a40d70d commit 06d57db
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 0 deletions.
2 changes: 2 additions & 0 deletions class/defaults.yml
Original file line number Diff line number Diff line change
Expand Up @@ -411,3 +411,5 @@ parameters:
reserved: 4

secrets: {}

cronjobs: {}
73 changes: 73 additions & 0 deletions component/cronjobs.libsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
local com = import 'lib/commodore.libjsonnet';
local kap = import 'lib/kapitan.libjsonnet';
local kube = import 'lib/kube.libjsonnet';

local inv = kap.inventory();
local params = inv.parameters.openshift4_monitoring;

local makeCronjob(name, args) =
local scriptSecret = kube.Secret(name) {
metadata+: {
namespace: params.namespace,
},
data:: {},
stringData: {
'script.sh': args.script,
},
};
local image = if std.objectHas(args, 'image') then
args.image.image + ':' + args.image.tag else
params.images.oc.image + ':' + params.images.oc.tag;
[
scriptSecret,
kube.CronJob(name) {
metadata+: {
namespace: params.namespace,
},
spec+: {
schedule: args.schedule,
failedJobsHistoryLimit: 3,
successfulJobsHistoryLimit: 3,
jobTemplate+: {
spec+: {
template+: {
spec+: {
restartPolicy: 'Never',
containers_+: {
silence: kube.Container('job') {
image: image,
command: [ '/usr/local/bin/script.sh' ],
volumeMounts_+: {
scripts: {
mountPath: '/usr/local/bin/script.sh',
subPath: 'script.sh',
readOnly: true,
},
},
},
},
volumes_+: {
scripts: {
secret: {
secretName: scriptSecret.metadata.name,
defaultMode: std.parseOctal('0550'),
},
},
},
},
},
},
},
},
} + com.makeMergeable(std.get(args, 'config', {})),
];

local cronjobs = std.flattenArrays([
makeCronjob(name, params.cronjobs[name])
for name in std.objectFields(params.cronjobs)
if params.cronjobs[name] != null
]);

{
cronjobs: cronjobs,
}
3 changes: 3 additions & 0 deletions component/main.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ local patchRemoteWrite(promConfig, defaults) = promConfig {
local customRules =
prom.generateRules('custom-rules', params.rules);

local cronjobs = import 'cronjobs.libsonnet';

{
'00_namespace_labels': ns_patch,
'01_secrets': secrets,
Expand Down Expand Up @@ -132,4 +134,5 @@ local customRules =
silence: import 'silence.jsonnet',
[if params.capacityAlerts.enabled then 'capacity_rules']: capacity.rules,
[if std.length(customRules.spec.groups) > 0 then 'custom_rules']: customRules,
[if std.length(cronjobs.cronjobs) > 0 then 'cronjobs']: cronjobs.cronjobs,
}
69 changes: 69 additions & 0 deletions docs/modules/ROOT/pages/references/parameters.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,72 @@ remoteWriteDefaults:

A dict of default remote write configurations for the Prometheus component.
Those values are merged into each remote write configuration in `configs.prometheusK8s.remoteWrite` and `configsUserWorkload.prometheus.remoteWrite`.


== `cronjobs`

[horizontal]
type:: dict

A dict of arbitrary cronjobs to create in the `openshift-monitoring` namespace.
The key is the name of the cronjob and the values are its configuration options as shown below.

=== `schedule`

[horizontal]
type:: string

Schedule of the CronJob in cron syntax.

=== `script`

[horizontal]
type:: string

The script to execute as part of the cronjob.

=== `image`

[horizontal]
type:: dict
default:: `images.oc` from https://github.com/appuio/component-openshift4-monitoring/blob/master/class/defaults.yml[`class/defaults.yml`]

=== `image.image`

[horizontal]
type:: string

The image used by the cronjob.

=== `image.tag`

[horizontal]
type:: string

The image tag used by the cronjob.

=== `config`

[horizontal]
type:: dict
default:: `{}`

Any additional custom configuration for the cronjob.

=== Example

[source,yaml]
----
cronjobs:
my-cronjob:
schedule: "1 * * * *"
image:
image: quay.io/appuio/oc
tag: v4.13
script: |
#!/bin/sh
echo "this is an example"
config:
spec:
failedJobsHistoryLimit: 1
----
20 changes: 20 additions & 0 deletions tests/custom-rules.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,23 @@ parameters:
group_e:
"record:foo:sum": null
"record:bar:sum": null

# add test cases for cronjobs here to keep the number of test instances in check
cronjobs:
foo:
schedule: "1 * * * *"
script: |
#!/bin/sh
echo foo
bar:
schedule: "27 * * * *"
image:
image: quay.io/appuio/oc
tag: v4.13
script: |
#!/bin/sh
echo bar
config:
spec:
failedJobsHistoryLimit: 27
baz: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
apiVersion: v1
kind: Secret
metadata:
annotations: {}
labels:
name: bar
name: bar
namespace: openshift-monitoring
stringData:
script.sh: |
#!/bin/sh
echo bar
type: Opaque
---
apiVersion: batch/v1
kind: CronJob
metadata:
annotations: {}
labels:
name: bar
name: bar
namespace: openshift-monitoring
spec:
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 27
jobTemplate:
spec:
completions: 1
parallelism: 1
template:
metadata:
labels:
name: bar
spec:
containers:
- args: []
command:
- /usr/local/bin/script.sh
env: []
image: quay.io/appuio/oc:v4.13
imagePullPolicy: IfNotPresent
name: job
ports: []
stdin: false
tty: false
volumeMounts:
- mountPath: /usr/local/bin/script.sh
name: scripts
readOnly: true
subPath: script.sh
imagePullSecrets: []
initContainers: []
restartPolicy: Never
terminationGracePeriodSeconds: 30
volumes:
- name: scripts
secret:
defaultMode: 360
secretName: bar
schedule: 27 * * * *
successfulJobsHistoryLimit: 3
---
apiVersion: v1
kind: Secret
metadata:
annotations: {}
labels:
name: foo
name: foo
namespace: openshift-monitoring
stringData:
script.sh: |
#!/bin/sh
echo foo
type: Opaque
---
apiVersion: batch/v1
kind: CronJob
metadata:
annotations: {}
labels:
name: foo
name: foo
namespace: openshift-monitoring
spec:
concurrencyPolicy: Forbid
failedJobsHistoryLimit: 3
jobTemplate:
spec:
completions: 1
parallelism: 1
template:
metadata:
labels:
name: foo
spec:
containers:
- args: []
command:
- /usr/local/bin/script.sh
env: []
image: quay.io/appuio/oc:v4.14
imagePullPolicy: IfNotPresent
name: job
ports: []
stdin: false
tty: false
volumeMounts:
- mountPath: /usr/local/bin/script.sh
name: scripts
readOnly: true
subPath: script.sh
imagePullSecrets: []
initContainers: []
restartPolicy: Never
terminationGracePeriodSeconds: 30
volumes:
- name: scripts
secret:
defaultMode: 360
secretName: foo
schedule: 1 * * * *
successfulJobsHistoryLimit: 3

0 comments on commit 06d57db

Please sign in to comment.