|
| 1 | +{{/* |
| 2 | +Creates the Redis password secret via a pre-install/pre-upgrade Job when the |
| 3 | +bundled redis subchart is enabled with auth on. |
| 4 | + |
| 5 | +The Job is idempotent: if the secret already exists, it exits without change. |
| 6 | +This keeps the password stable across Helm upgrades and ArgoCD re-syncs — |
| 7 | +unlike a template-based `randAlphaNum` approach, which changes on every render |
| 8 | +and would either lock clients out or force an ArgoCD ignoreDifferences rule. |
| 9 | + |
| 10 | +The runtime "secret already exists -> exit 0" check is also the migration and |
| 11 | +bring-your-own-secret guard: an upgrade of an install that already has the |
| 12 | +secret (or a user who pre-created their own) is left untouched. |
| 13 | + |
| 14 | +The resulting Secret has no Helm release labels/annotations and no |
| 15 | +ownerReferences, so ArgoCD does not track or diff it. |
| 16 | +*/}} |
| 17 | +{{/* |
| 18 | +Skip the bootstrap entirely when redis.auth.password is set: on that path the |
| 19 | +operator self-manages the credential (Bitnami uses redis.auth.password directly) |
| 20 | +and there is no chart secret to create. Safe ONLY because appsmith.validateRedisAuth |
| 21 | +(see _helpers.tpl, invoked from configMap.yaml) rejects every redis.auth.password |
| 22 | +configuration except the self-managed one (existingSecret: "" + a matching |
| 23 | +APPSMITH_REDIS_URL) — so this can no longer leave a non-empty existingSecret |
| 24 | +pointing at a secret the hook never creates. |
| 25 | +*/}} |
| 26 | +{{- if and .Values.redis.enabled .Values.redis.auth.enabled (not .Values.redis.auth.password) }} |
| 27 | +{{- $secretName := include "appsmith.redisSecretName" . -}} |
| 28 | +{{- $passwordKey := .Values.redis.auth.existingSecretPasswordKey -}} |
| 29 | +{{- $jobName := printf "%s-redis-password-init" (include "appsmith.fullname" .) | trunc 63 | trimSuffix "-" -}} |
| 30 | +{{- $namespace := include "appsmith.namespace" . -}} |
| 31 | +--- |
| 32 | +apiVersion: v1 |
| 33 | +kind: ServiceAccount |
| 34 | +metadata: |
| 35 | + name: {{ $jobName }} |
| 36 | + namespace: {{ $namespace }} |
| 37 | + labels: |
| 38 | + {{- include "appsmith.labels" . | nindent 4 }} |
| 39 | + annotations: |
| 40 | + helm.sh/hook: pre-install,pre-upgrade |
| 41 | + helm.sh/hook-weight: "-5" |
| 42 | + helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded |
| 43 | +--- |
| 44 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 45 | +kind: Role |
| 46 | +metadata: |
| 47 | + name: {{ $jobName }} |
| 48 | + namespace: {{ $namespace }} |
| 49 | + labels: |
| 50 | + {{- include "appsmith.labels" . | nindent 4 }} |
| 51 | + annotations: |
| 52 | + helm.sh/hook: pre-install,pre-upgrade |
| 53 | + helm.sh/hook-weight: "-5" |
| 54 | + helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded |
| 55 | +rules: |
| 56 | + # Check whether the password secret already exists (scoped to one name). |
| 57 | + - apiGroups: [""] |
| 58 | + resources: ["secrets"] |
| 59 | + resourceNames: [{{ $secretName | quote }}] |
| 60 | + verbs: ["get"] |
| 61 | + # Create it if it doesn't. resourceNames can't scope `create` (the name |
| 62 | + # is in the request body, not the URL), so this is namespace-scoped. |
| 63 | + - apiGroups: [""] |
| 64 | + resources: ["secrets"] |
| 65 | + verbs: ["create"] |
| 66 | +--- |
| 67 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 68 | +kind: RoleBinding |
| 69 | +metadata: |
| 70 | + name: {{ $jobName }} |
| 71 | + namespace: {{ $namespace }} |
| 72 | + labels: |
| 73 | + {{- include "appsmith.labels" . | nindent 4 }} |
| 74 | + annotations: |
| 75 | + helm.sh/hook: pre-install,pre-upgrade |
| 76 | + helm.sh/hook-weight: "-5" |
| 77 | + helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded |
| 78 | +subjects: |
| 79 | + - kind: ServiceAccount |
| 80 | + name: {{ $jobName }} |
| 81 | + namespace: {{ $namespace }} |
| 82 | +roleRef: |
| 83 | + kind: Role |
| 84 | + name: {{ $jobName }} |
| 85 | + apiGroup: rbac.authorization.k8s.io |
| 86 | +--- |
| 87 | +apiVersion: batch/v1 |
| 88 | +kind: Job |
| 89 | +metadata: |
| 90 | + name: {{ $jobName }} |
| 91 | + namespace: {{ $namespace }} |
| 92 | + labels: |
| 93 | + {{- include "appsmith.labels" . | nindent 4 }} |
| 94 | + annotations: |
| 95 | + helm.sh/hook: pre-install,pre-upgrade |
| 96 | + helm.sh/hook-weight: "0" |
| 97 | + helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded |
| 98 | +spec: |
| 99 | + ttlSecondsAfterFinished: 60 |
| 100 | + backoffLimit: 3 |
| 101 | + template: |
| 102 | + metadata: |
| 103 | + labels: |
| 104 | + {{- include "appsmith.labels" . | nindent 8 }} |
| 105 | + spec: |
| 106 | + serviceAccountName: {{ $jobName }} |
| 107 | + restartPolicy: Never |
| 108 | + {{- if .Values.image.pullSecrets }} |
| 109 | + imagePullSecrets: |
| 110 | + - name: {{ .Values.image.pullSecrets }} |
| 111 | + {{- end }} |
| 112 | + containers: |
| 113 | + - name: create-password |
| 114 | + image: {{ include "appsmith.redisPasswordInitImage" . | quote }} |
| 115 | + imagePullPolicy: {{ .Values.redisAuth.passwordInit.image.pullPolicy }} |
| 116 | + command: |
| 117 | + - /bin/sh |
| 118 | + - -c |
| 119 | + - | |
| 120 | + set -eu |
| 121 | + SECRET_NAME={{ $secretName | quote }} |
| 122 | + NAMESPACE={{ $namespace | quote }} |
| 123 | + PASSWORD_KEY={{ $passwordKey | quote }} |
| 124 | + if kubectl -n "$NAMESPACE" get secret "$SECRET_NAME" >/dev/null 2>&1; then |
| 125 | + echo "Secret $SECRET_NAME already exists; leaving it untouched." |
| 126 | + exit 0 |
| 127 | + fi |
| 128 | + PASSWORD=$(tr -dc 'A-Za-z0-9' </dev/urandom | head -c 24) |
| 129 | + kubectl -n "$NAMESPACE" create secret generic "$SECRET_NAME" \ |
| 130 | + --from-literal="$PASSWORD_KEY"="$PASSWORD" |
| 131 | + echo "Created secret $SECRET_NAME." |
| 132 | + securityContext: |
| 133 | + allowPrivilegeEscalation: false |
| 134 | + readOnlyRootFilesystem: true |
| 135 | + runAsNonRoot: true |
| 136 | + runAsUser: 1000 |
| 137 | + capabilities: |
| 138 | + drop: ["ALL"] |
| 139 | +{{- end }} |
0 commit comments