|
| 1 | +# This hook sets the `controller.kubernetes.io/pod-deletion-cost` on every pod to the creation timestamp. |
| 2 | +# The Kubernetes rollout will then terminate pods by order of creation, allowing topologySpreadConstraints to succeed in environments with not much nodes headroom. |
| 3 | +{{- if .Values.upgradeOrderJob.enabled }} |
| 4 | +--- |
| 5 | +apiVersion: v1 |
| 6 | +kind: ServiceAccount |
| 7 | +metadata: |
| 8 | + name: {{ include "osu-web-chart.fullname" . }}-upgrade-order |
| 9 | + labels: |
| 10 | + {{- include "osu-web-chart.labels" (dict "root" . "options" (dict "component" "upgrade-order")) | nindent 4 }} |
| 11 | + tier: wait-for-deploy |
| 12 | + annotations: |
| 13 | + "helm.sh/hook": pre-upgrade |
| 14 | + "helm.sh/hook-weight": "-5" |
| 15 | + "helm.sh/hook-delete-policy": before-hook-creation |
| 16 | +--- |
| 17 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 18 | +kind: Role |
| 19 | +metadata: |
| 20 | + name: {{ include "osu-web-chart.fullname" . }}-upgrade-order |
| 21 | + labels: |
| 22 | + {{- include "osu-web-chart.labels" (dict "root" . "options" (dict "component" "upgrade-order")) | nindent 4 }} |
| 23 | + tier: wait-for-deploy |
| 24 | + annotations: |
| 25 | + "helm.sh/hook": pre-upgrade |
| 26 | + "helm.sh/hook-weight": "-5" |
| 27 | + "helm.sh/hook-delete-policy": before-hook-creation |
| 28 | +rules: |
| 29 | + - apiGroups: |
| 30 | + - "" |
| 31 | + resources: |
| 32 | + - pods |
| 33 | + verbs: |
| 34 | + - get |
| 35 | + - list |
| 36 | + - patch |
| 37 | +--- |
| 38 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 39 | +kind: RoleBinding |
| 40 | +metadata: |
| 41 | + name: {{ include "osu-web-chart.fullname" . }}-upgrade-order |
| 42 | + labels: |
| 43 | + {{- include "osu-web-chart.labels" (dict "root" . "options" (dict "component" "upgrade-order")) | nindent 4 }} |
| 44 | + tier: wait-for-deploy |
| 45 | + annotations: |
| 46 | + "helm.sh/hook": pre-upgrade |
| 47 | + "helm.sh/hook-weight": "-5" |
| 48 | + "helm.sh/hook-delete-policy": before-hook-creation |
| 49 | +subjects: |
| 50 | + - kind: ServiceAccount |
| 51 | + name: {{ include "osu-web-chart.fullname" . }}-upgrade-order |
| 52 | + apiGroup: "" |
| 53 | +roleRef: |
| 54 | + kind: Role |
| 55 | + name: {{ include "osu-web-chart.fullname" . }}-upgrade-order |
| 56 | + apiGroup: "" |
| 57 | +--- |
| 58 | +apiVersion: batch/v1 |
| 59 | +kind: Job |
| 60 | +metadata: |
| 61 | + name: {{ include "osu-web-chart.fullname" . }}-upgrade-order |
| 62 | + labels: |
| 63 | + {{- include "osu-web-chart.labels" (dict "root" . "options" (dict "component" "upgrade-order")) | nindent 4 }} |
| 64 | + tier: wait-for-deploy |
| 65 | + annotations: |
| 66 | + "helm.sh/hook": pre-upgrade |
| 67 | + "helm.sh/hook-weight": "-5" |
| 68 | + "helm.sh/hook-delete-policy": before-hook-creation |
| 69 | +spec: |
| 70 | + template: |
| 71 | + spec: |
| 72 | + restartPolicy: Never |
| 73 | + serviceAccountName: {{ include "osu-web-chart.fullname" . }}-upgrade-order |
| 74 | + securityContext: |
| 75 | + {{- toYaml .Values.upgradeOrderJob.podSecurityContext | nindent 8 }} |
| 76 | + containers: |
| 77 | + - name: kubectl |
| 78 | + securityContext: |
| 79 | + {{- toYaml .Values.upgradeOrderJob.securityContext | nindent 12 }} |
| 80 | + image: {{ .Values.upgradeOrderJob.image }} |
| 81 | + {{- with .Values.upgradeOrderJob.imagePullPolicy }} |
| 82 | + imagePullPolicy: {{ . }} |
| 83 | + {{- end }} |
| 84 | + command: |
| 85 | + - /bin/bash |
| 86 | + - -c |
| 87 | + - | |
| 88 | + set -e |
| 89 | +
|
| 90 | + POD_TIMESTAMPS=$( |
| 91 | + kubectl get pods --selector=app.kubernetes.io/instance={{ .Release.Name }} -o json \ |
| 92 | + | jq -r '.items[] | "\(.metadata.name) \(.metadata.creationTimestamp | fromdateiso8601)"' |
| 93 | + ) |
| 94 | +
|
| 95 | + echo "$POD_TIMESTAMPS" | while read -r pod timestamp; do |
| 96 | + kubectl annotate --overwrite pod/"$pod" controller.kubernetes.io/pod-deletion-cost="$timestamp" |
| 97 | + done |
| 98 | + resources: |
| 99 | + {{- toYaml (.Values.upgradeOrderJob.resources | default .Values.resources) | nindent 12 }} |
| 100 | + {{- with .Values.nodeSelector }} |
| 101 | + nodeSelector: |
| 102 | + {{- toYaml . | nindent 12 }} |
| 103 | + {{- end }} |
| 104 | + {{- with .Values.tolerations }} |
| 105 | + tolerations: |
| 106 | + {{- toYaml . | nindent 12 }} |
| 107 | + {{- end }} |
| 108 | +{{- end -}} |
0 commit comments