Skip to content

Commit

Permalink
fix: enable affinity on cronjob (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo authored Jun 4, 2024
1 parent 42aa528 commit 5c64688
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
30 changes: 30 additions & 0 deletions packages/kontinuous/tests/__snapshots__/extends-ovh.prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ spec:
value: pg-hasura-r
- name: RETENTION_DAYS
value: \\"60\\"
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: nodepool
operator: In
values:
- prod-build
tolerations:
- key: pool
operator: Equal
value: ci
effect: NoSchedule
---
apiVersion: batch/v1
kind: CronJob
Expand Down Expand Up @@ -239,6 +254,21 @@ spec:
value: pg-keycloak-r
- name: RETENTION_DAYS
value: \\"60\\"
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: nodepool
operator: In
values:
- prod-build
tolerations:
- key: pool
operator: Equal
value: ci
effect: NoSchedule
---
apiVersion: batch/v1
kind: Job
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ spec:
value: pg-r
- name: RETENTION_DAYS
value: \\"60\\"
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: nodepool
operator: In
values:
- prod-build
tolerations:
- key: pool
operator: Equal
value: ci
effect: NoSchedule
---
apiVersion: batch/v1
kind: Job
Expand Down
15 changes: 15 additions & 0 deletions packages/kontinuous/tests/__snapshots__/pg.prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ spec:
value: cnpg-r
- name: RETENTION_DAYS
value: \\"60\\"
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: nodepool
operator: In
values:
- prod-build
tolerations:
- key: pool
operator: Equal
value: ci
effect: NoSchedule
---
apiVersion: batch/v1
kind: Job
Expand Down
27 changes: 16 additions & 11 deletions plugins/contrib/patches/40-add-jobs-affinity-and-tolerations.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
module.exports = (manifests, options) => {
const { affinityToAdd = {}, tolerationsToAdd = [] } = options
const {
affinityToAdd = {},
tolerationsToAdd = [],
cronjobEnabled = true,
} = options
for (const manifest of manifests) {
if (manifest.kind !== "Job") {
const { kind } = manifest
if (!(kind === "Job" || (kind === "CronJob" && cronjobEnabled))) {
continue
}

manifest.spec.template.spec = manifest.spec.template.spec || {}
const templateSpec = manifest.spec.template.spec
const parentSpec = kind === "CronJob" ? manifest.spec.jobTemplate : manifest

parentSpec.spec.template.spec ||= {}
const templateSpec = parentSpec.spec.template.spec

// Add or merge affinity
templateSpec.affinity = templateSpec.affinity || {}
templateSpec.affinity.nodeAffinity =
templateSpec.affinity.nodeAffinity || {}
templateSpec.affinity.nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution =
templateSpec.affinity.nodeAffinity
.preferredDuringSchedulingIgnoredDuringExecution || []
templateSpec.affinity ||= {}
templateSpec.affinity.nodeAffinity ||= {}
templateSpec.affinity.nodeAffinity.preferredDuringSchedulingIgnoredDuringExecution ||=
[]
const existingPreferences =
templateSpec.affinity.nodeAffinity
.preferredDuringSchedulingIgnoredDuringExecution
Expand All @@ -31,7 +36,7 @@ module.exports = (manifests, options) => {
}

// Add or merge tolerations
templateSpec.tolerations = templateSpec.tolerations || []
templateSpec.tolerations ||= []
const newTolerations = tolerationsToAdd.filter(
(tolerationToAdd) =>
!templateSpec.tolerations.some(
Expand Down

0 comments on commit 5c64688

Please sign in to comment.