Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable affinity on cronjob #487

Merged
merged 4 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading