Skip to content

Commit 8b15b88

Browse files
committed
refactor(helm): DRY non-preemptible node affinity into shared helpers
Consolidates three duplicate nodeAffinity blocks (backup.mongo, restore, cronJobs.syncMongoProductionData) into a single top-level value (nonPreemptibleNodeAffinity) and two reusable helper templates: - sefaria.nonPreemptibleNodeAffinity: renders nodeAffinity inside an existing affinity block (used by mongo-backup, mongo-backup-extra) - sefaria.nonPreemptibleAffinityBlock: renders a full affinity block (used by sync-mongo-production-data, mongo-restore) Made-with: Cursor
1 parent 8d444e4 commit 8b15b88

6 files changed

Lines changed: 37 additions & 39 deletions

File tree

helm-chart/sefaria/templates/_helpers.tpl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,29 @@ tasks: {{ .Values.deployEnv }}-tasks
169169
{{- merge (fromYaml (include "sefaria.tasks.internalQueues" . )) .Values.tasks.queues | toYaml }}
170170
{{- end }}
171171

172+
{{/*
173+
Node affinity to avoid preemptible/spot GKE nodes.
174+
Renders a nodeAffinity block from .Values.nonPreemptibleNodeAffinity.
175+
Usage inside an existing affinity: block:
176+
{{- include "sefaria.nonPreemptibleNodeAffinity" . | nindent <N> }}
177+
Usage when no affinity: block exists yet (wraps in affinity:):
178+
{{- include "sefaria.nonPreemptibleAffinityBlock" . | nindent <N> }}
179+
*/}}
180+
{{- define "sefaria.nonPreemptibleNodeAffinity" -}}
181+
{{- with .Values.nonPreemptibleNodeAffinity }}
182+
nodeAffinity:
183+
{{- toYaml . | nindent 2 }}
184+
{{- end }}
185+
{{- end -}}
186+
187+
{{- define "sefaria.nonPreemptibleAffinityBlock" -}}
188+
{{- with .Values.nonPreemptibleNodeAffinity }}
189+
affinity:
190+
nodeAffinity:
191+
{{- toYaml . | nindent 4 }}
192+
{{- end }}
193+
{{- end -}}
194+
172195
{{- define "config.domainModules" }}
173196
{{- $map := dict -}}
174197
{{- $deployEnv := .Values.deployEnv -}}

helm-chart/sefaria/templates/cronjob/mongo-backup-extra.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ spec:
2727
values:
2828
- mongo
2929
topologyKey: kubernetes.io/hostname
30-
{{- if not (empty .Values.backup.mongo.nodeAffinity) }}
31-
nodeAffinity:
32-
{{- toYaml .Values.backup.mongo.nodeAffinity | nindent 14 }}
33-
{{- end }}
30+
{{- include "sefaria.nonPreemptibleNodeAffinity" . | nindent 12 }}
3431
tolerations:
3532
- key: schedule-on-database-vm
3633
operator: "Equal"

helm-chart/sefaria/templates/cronjob/mongo-backup.yaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ spec:
2727
values:
2828
- mongo
2929
topologyKey: kubernetes.io/hostname
30-
{{- if not (empty .Values.backup.mongo.nodeAffinity) }}
31-
nodeAffinity:
32-
{{- toYaml .Values.backup.mongo.nodeAffinity | nindent 14 }}
33-
{{- end }}
30+
{{- include "sefaria.nonPreemptibleNodeAffinity" . | nindent 12 }}
3431
tolerations:
3532
- key: schedule-on-database-vm
3633
operator: "Equal"

helm-chart/sefaria/templates/cronjob/sync-mongo-production-data.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ spec:
1919
cluster-autoscaler.kubernetes.io/safe-to-evict: "false"
2020
spec:
2121
serviceAccount: {{ .Values.restore.serviceAccount }}
22-
{{- if not (empty .Values.cronJobs.syncMongoProductionData.nodeAffinity) }}
23-
affinity:
24-
nodeAffinity:
25-
{{- toYaml .Values.cronJobs.syncMongoProductionData.nodeAffinity | nindent 14 }}
26-
{{- end }}
22+
{{- include "sefaria.nonPreemptibleAffinityBlock" . | nindent 10 }}
2723
volumes:
2824
- name: shared-volume
2925
emptyDir: {}

helm-chart/sefaria/templates/jobs/mongo-restore.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ spec:
1717
template:
1818
spec:
1919
serviceAccount: {{ .Values.restore.serviceAccount }}
20-
{{- if not (empty .Values.restore.nodeAffinity) }}
21-
affinity:
22-
nodeAffinity:
23-
{{- toYaml .Values.restore.nodeAffinity | nindent 10 }}
24-
{{- end }}
20+
{{- include "sefaria.nonPreemptibleAffinityBlock" . | nindent 6 }}
2521
volumes:
2622
- name: shared-volume
2723
emptyDir:

helm-chart/sefaria/values.yaml

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ releaseImageTag:
88

99
sandbox: "false"
1010

11+
# Shared node affinity applied to all long-running mongo jobs (backup, restore, sync).
12+
# Prevents scheduling on preemptible GKE nodes that Google can reclaim mid-run.
13+
# Set to {} in environment values to allow any node pool.
14+
nonPreemptibleNodeAffinity:
15+
requiredDuringSchedulingIgnoredDuringExecution:
16+
nodeSelectorTerms:
17+
- matchExpressions:
18+
- key: cloud.google.com/gke-preemptible
19+
operator: DoesNotExist
20+
1121
# This value sets the name of the environment and it's associated objects. Some
1222
# suggestions for the values are prod/dev/test
1323
deployEnv: "dev"
@@ -45,13 +55,6 @@ restore:
4555
bucket: sefaria-mongo-backup
4656
# tarball:
4757
serviceAccount: database-backup-read
48-
# Avoid GKE preemptible nodes during restore. Set nodeAffinity: {} to allow any pool.
49-
nodeAffinity:
50-
requiredDuringSchedulingIgnoredDuringExecution:
51-
nodeSelectorTerms:
52-
- matchExpressions:
53-
- key: cloud.google.com/gke-preemptible
54-
operator: DoesNotExist
5558
# config to backup environment DB
5659
backup:
5760
mongo:
@@ -63,13 +66,6 @@ backup:
6366
archiveBucket: sefaria-mongo-archive
6467
serviceAccount: database-backup-write
6568
version: 4.4
66-
# Avoid GKE preemptible nodes mid-dump. Set nodeAffinity: {} to allow any pool.
67-
nodeAffinity:
68-
requiredDuringSchedulingIgnoredDuringExecution:
69-
nodeSelectorTerms:
70-
- matchExpressions:
71-
- key: cloud.google.com/gke-preemptible
72-
operator: DoesNotExist
7369
postgres:
7470
enabled: false
7571
version: 10.3
@@ -450,13 +446,6 @@ cronJobs:
450446
enabled: false
451447
syncMongoProductionData:
452448
enabled: false
453-
# Avoid GKE preemptible nodes (VM can be reclaimed mid-download/restore). Set nodeAffinity: {} to allow any pool.
454-
nodeAffinity:
455-
requiredDuringSchedulingIgnoredDuringExecution:
456-
nodeSelectorTerms:
457-
- matchExpressions:
458-
- key: cloud.google.com/gke-preemptible
459-
operator: DoesNotExist
460449

461450
localSettings:
462451
DEBUG: true

0 commit comments

Comments
 (0)