provisioning-job.yaml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. {{- /*
  2. Copyright VMware, Inc.
  3. SPDX-License-Identifier: APACHE-2.0
  4. */}}
  5. {{- if .Values.provisioning.enabled }}
  6. {{- $fullname := printf "%s-provisioning" (include "common.names.fullname" .) }}
  7. {{- $minioAlias := "provisioning" }}
  8. apiVersion: batch/v1
  9. kind: Job
  10. metadata:
  11. name: {{ $fullname }}
  12. namespace: {{ include "common.names.namespace" . | quote }}
  13. labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  14. app.kubernetes.io/component: minio-provisioning
  15. annotations:
  16. helm.sh/hook: post-install,post-upgrade
  17. helm.sh/hook-delete-policy: before-hook-creation
  18. {{- if .Values.commonAnnotations }}
  19. {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  20. {{- end }}
  21. spec:
  22. {{- if .Values.provisioning.cleanupAfterFinished.enabled }}
  23. ttlSecondsAfterFinished: {{ .Values.provisioning.cleanupAfterFinished.seconds }}
  24. {{- end }}
  25. parallelism: 1
  26. template:
  27. metadata:
  28. labels: {{- include "minio.labels.provisioning" . | nindent 8 }}
  29. app.kubernetes.io/component: minio-provisioning
  30. {{- if .Values.provisioning.podAnnotations }}
  31. annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.provisioning.podAnnotations "context" $) | nindent 8 }}
  32. {{- end }}
  33. spec:
  34. {{- include "minio.imagePullSecrets" . | nindent 6 }}
  35. {{- if .Values.provisioning.schedulerName }}
  36. schedulerName: {{ .Values.provisioning.schedulerName }}
  37. {{- end }}
  38. restartPolicy: OnFailure
  39. terminationGracePeriodSeconds: 0
  40. {{- if .Values.provisioning.podSecurityContext.enabled }}
  41. securityContext: {{- omit .Values.provisioning.podSecurityContext "enabled" | toYaml | nindent 8 }}
  42. {{- end }}
  43. serviceAccountName: {{ template "minio.serviceAccountName" . }}
  44. initContainers:
  45. - name: wait-for-available-minio
  46. image: {{ include "minio.image" . }}
  47. imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
  48. {{- if .Values.provisioning.containerSecurityContext.enabled }}
  49. securityContext: {{- omit .Values.provisioning.containerSecurityContext "enabled" | toYaml | nindent 12 }}
  50. {{- end }}
  51. command:
  52. - /bin/bash
  53. - -c
  54. - |-
  55. set -e;
  56. echo "Waiting for Minio";
  57. wait-for-port \
  58. --host={{ include "common.names.fullname" . }} \
  59. --state=inuse \
  60. --timeout=120 \
  61. {{ .Values.service.ports.api | int64 }};
  62. echo "Minio is available";
  63. {{- if .Values.provisioning.resources }}
  64. resources: {{- toYaml .Values.provisioning.resources | nindent 12 }}
  65. {{- end }}
  66. containers:
  67. - name: minio
  68. image: {{ include "minio.image" . }}
  69. imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
  70. {{- if .Values.provisioning.containerSecurityContext.enabled }}
  71. securityContext: {{- omit .Values.provisioning.containerSecurityContext "enabled" | toYaml | nindent 12 }}
  72. {{- end }}
  73. {{- if .Values.provisioning.command }}
  74. command: {{- include "common.tplvalues.render" (dict "value" .Values.provisioning.command "context" $) | nindent 12 }}
  75. {{- else }}
  76. command:
  77. - /bin/bash
  78. - -c
  79. - >-
  80. set -e;
  81. echo "Start Minio provisioning";
  82. function attachPolicy() {
  83. local tmp=$(mc admin $1 info {{ $minioAlias }} $2 | sed -n -e 's/^Policy.*: \(.*\)$/\1/p');
  84. IFS=',' read -r -a CURRENT_POLICIES <<< "$tmp";
  85. if [[ ! "${CURRENT_POLICIES[*]}" =~ "$3" ]]; then
  86. mc admin policy attach {{ $minioAlias }} $3 --$1=$2;
  87. fi;
  88. };
  89. function detachDanglingPolicies() {
  90. local tmp=$(mc admin $1 info {{ $minioAlias }} $2 | sed -n -e 's/^Policy.*: \(.*\)$/\1/p');
  91. IFS=',' read -r -a CURRENT_POLICIES <<< "$tmp";
  92. IFS=',' read -r -a DESIRED_POLICIES <<< "$3";
  93. for current in "${CURRENT_POLICIES[@]}"; do
  94. if [[ ! "${DESIRED_POLICIES[*]}" =~ "${current}" ]]; then
  95. mc admin policy detach {{ $minioAlias }} $current --$1=$2;
  96. fi;
  97. done;
  98. }
  99. function addUsersFromFile() {
  100. local username=$(grep -oP '^username=\K.+' $1);
  101. local password=$(grep -oP '^password=\K.+' $1);
  102. local disabled=$(grep -oP '^disabled=\K.+' $1);
  103. local policies_list=$(grep -oP '^policies=\K.+' $1);
  104. local set_policies=$(grep -oP '^setPolicies=\K.+' $1);
  105. mc admin user add {{ $minioAlias }} "${username}" "${password}";
  106. IFS=',' read -r -a POLICIES <<< "${policies_list}";
  107. for policy in "${POLICIES[@]}"; do
  108. attachPolicy user "${username}" "${policy}";
  109. done;
  110. if [ "${set_policies}" == "true" ]; then
  111. detachDanglingPolicies user "${username}" "${policies_list}";
  112. fi;
  113. local user_status="enable";
  114. if [[ "${disabled}" != "" && "${disabled,,}" == "true" ]]; then
  115. user_status="disable";
  116. fi;
  117. mc admin user "${user_status}" {{ $minioAlias }} "${username}";
  118. };
  119. {{- $minioUrl := printf "$MINIO_SCHEME://%s:%d" (include "common.names.fullname" .) (.Values.service.ports.api | int) }}
  120. {{- $minioRootUser := ternary ("$(<$MINIO_ROOT_USER_FILE)") ("$MINIO_ROOT_USER") (.Values.auth.useCredentialsFiles) }}
  121. {{- $minioRootPassword := ternary ("$(<$MINIO_ROOT_PASSWORD_FILE)") ("$MINIO_ROOT_PASSWORD") (.Values.auth.useCredentialsFiles) }}
  122. mc alias set {{ $minioAlias }} {{ $minioUrl }} {{ $minioRootUser }} {{ $minioRootPassword }};
  123. {{- range $config := .Values.provisioning.config }}
  124. {{- $options := list }}
  125. {{- range $name, $value := $config.options }}
  126. {{- $options = (printf "%s=%s" $name $value) | append $options }}
  127. {{- end }}
  128. {{- $options := join " " $options }}
  129. mc admin config set {{ $minioAlias }} {{ $config.name }} {{ $options }};
  130. {{- end }}
  131. mc admin service restart {{ $minioAlias }};
  132. {{- range $policy := .Values.provisioning.policies }}
  133. mc admin policy create {{ $minioAlias }} {{ $policy.name }} /etc/ilm/policy-{{ $policy.name }}.json;
  134. {{- end }}
  135. {{- range $user := .Values.provisioning.users }}
  136. mc admin user add {{ $minioAlias }} {{ $user.username }} {{ $user.password }};
  137. {{- range $policy := $user.policies }}
  138. attachPolicy user {{ $user.username }} {{ $policy }};
  139. {{- end }}
  140. {{- if $user.setPolicies }}
  141. detachDanglingPolicies user {{ $user.username }} "{{ join "," $user.policies }}";
  142. {{- end }}
  143. {{- $userStatus := ternary ("disable") ("enable") (and (not (empty $user.disabled)) $user.disabled) }}
  144. mc admin user {{ $userStatus }} {{ $minioAlias }} {{ $user.username }};
  145. {{- end }}
  146. {{- if gt (len .Values.provisioning.usersExistingSecrets) 0 }}
  147. while read -d '' configFile; do
  148. addUsersFromFile "${configFile}";
  149. done < <(find "/opt/bitnami/minio/users/" -type l -not -name '..data' -print0);
  150. {{- end }}
  151. {{- range $group := .Values.provisioning.groups }}
  152. mc admin group add {{ $minioAlias }} {{ $group.name }} {{ join " " $group.members }};
  153. {{- range $policy := $group.policies }}
  154. attachPolicy group {{ $group.name }} {{ $policy }};
  155. {{- end }}
  156. {{- if $group.setPolicies }}
  157. detachDanglingPolicies group {{ $group.name }} "{{ join "," $group.policies }}";
  158. {{- end }}
  159. {{- $groupStatus := ternary ("disable") ("enable") (and (not (empty $group.disabled)) $group.disabled) }}
  160. mc admin group {{ $groupStatus }} {{ $minioAlias }} {{ $group.name }};
  161. {{- end }}
  162. {{- $isDistributedMode := (eq .Values.mode "distributed") }}
  163. {{- range $bucket := .Values.provisioning.buckets }}
  164. {{- $target := printf "%s/%s" $minioAlias $bucket.name }}
  165. {{- $region := ternary (printf "--region=%s" $bucket.region) ("") (not (empty $bucket.region)) }}
  166. {{- $withLock := ternary ("--with-lock") ("") (and (not (empty $bucket.withLock)) $bucket.withLock) }}
  167. mc mb {{ $target }} --ignore-existing {{ $region }} {{ $withLock }};
  168. {{- if $bucket.lifecycle }}
  169. mc ilm import {{ $minioAlias }}/{{ $bucket.name }} < /etc/ilm/bucket-{{ $bucket.name }}.json;
  170. {{- end }}
  171. {{- with $bucket.quota }}
  172. {{- if eq .type "hard" }}
  173. mc quota set {{ $minioAlias }}/{{ $bucket.name }} {{ if .size }}--size {{ .size }}{{ end }};
  174. {{- else }}
  175. mc quota {{ .type }} {{ $minioAlias }}/{{ $bucket.name }} {{ if .size }}--size {{ .size }}{{ end }};
  176. {{- end }}
  177. {{- end }}
  178. {{- if $isDistributedMode }}
  179. {{- if (or ((empty $bucket.withLock)) (not $bucket.withLock)) }}
  180. {{- $versioning := default "Suspended" $bucket.versioning }}
  181. {{- if kindIs "bool" $bucket.versioning }}
  182. {{- $versioning = ternary "Versioned" "Suspended" $bucket.versioning }}
  183. {{- end }}
  184. {{- if eq $versioning "Versioned" }}
  185. mc version enable {{ $minioAlias }}/{{ $bucket.name }};
  186. {{- else if eq $versioning "Suspended" }}
  187. mc version suspend {{ $minioAlias }}/{{ $bucket.name }};
  188. {{- else if ne $versioning "Unchanged" }}
  189. {{- fail (printf "Invalid value '%s' for versioning of bucket '%s'" $versioning $bucket.name) }}
  190. {{- end }}
  191. {{- end }}
  192. {{- end }}
  193. {{- if $bucket.tags }}
  194. {{- $target := printf "%s/%s" $minioAlias $bucket.name }}
  195. {{- $tags := list }}
  196. {{- range $name, $value := $bucket.tags }}
  197. {{- $tags = (printf "%s=%s" $name $value) | append $tags }}
  198. {{- end }}
  199. {{- $tags := join "&" $tags | quote }}
  200. mc tag set {{ $target }} {{ $tags }};
  201. {{- end }}
  202. {{- end }}
  203. {{- if .Values.provisioning.extraCommands }}
  204. {{ join ";" .Values.provisioning.extraCommands | nindent 14 }};
  205. {{- end }}
  206. echo "End Minio provisioning";
  207. {{- end }}
  208. {{- if .Values.provisioning.args }}
  209. args: {{- include "common.tplvalues.render" (dict "value" .Values.provisioning.args "context" $) | nindent 12 }}
  210. {{- end }}
  211. env:
  212. - name: MINIO_SCHEME
  213. value: {{ ternary "https" "http" .Values.tls.enabled | quote }}
  214. {{- if .Values.auth.useCredentialsFiles }}
  215. - name: MINIO_ROOT_USER_FILE
  216. value: "/opt/bitnami/minio/secrets/root-user"
  217. {{- else }}
  218. - name: MINIO_ROOT_USER
  219. valueFrom:
  220. secretKeyRef:
  221. name: {{ include "minio.secretName" . }}
  222. key: root-user
  223. {{- end }}
  224. {{- if .Values.auth.useCredentialsFiles }}
  225. - name: MINIO_ROOT_PASSWORD_FILE
  226. value: "/opt/bitnami/minio/secrets/root-password"
  227. {{- else }}
  228. - name: MINIO_ROOT_PASSWORD
  229. valueFrom:
  230. secretKeyRef:
  231. name: {{ include "minio.secretName" . }}
  232. key: root-password
  233. {{- end }}
  234. {{- if .Values.tls.mountPath }}
  235. - name: MINIO_CERTS_DIR
  236. value: {{ .Values.tls.mountPath | quote }}
  237. {{- end }}
  238. envFrom:
  239. {{- if .Values.extraEnvVarsCM }}
  240. - configMapRef:
  241. name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
  242. {{- end }}
  243. {{- if .Values.extraEnvVarsSecret }}
  244. - secretRef:
  245. name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
  246. {{- end }}
  247. {{- if .Values.provisioning.resources }}
  248. resources: {{- toYaml .Values.provisioning.resources | nindent 12 }}
  249. {{- end }}
  250. volumeMounts:
  251. {{- if .Values.provisioning.enabled }}
  252. - name: minio-provisioning
  253. mountPath: /etc/ilm
  254. {{- end }}
  255. {{- if .Values.auth.useCredentialsFiles }}
  256. - name: minio-credentials
  257. mountPath: /opt/bitnami/minio/secrets/
  258. {{- end }}
  259. {{- if .Values.tls.enabled }}
  260. - name: minio-certs
  261. mountPath: {{ default "/certs" .Values.tls.mountPath }}
  262. - name: minio-client-certs
  263. mountPath: /.mc/certs
  264. {{- end }}
  265. {{- range $idx, $_ := .Values.provisioning.usersExistingSecrets }}
  266. - name: {{ printf "users-secret-%d" $idx }}
  267. mountPath: /opt/bitnami/minio/users/{{ $idx }}/
  268. {{- end }}
  269. {{- if .Values.provisioning.extraVolumeMounts }}
  270. {{- include "common.tplvalues.render" (dict "value" .Values.provisioning.extraVolumeMounts "context" $) | nindent 12 }}
  271. {{- end }}
  272. {{- if .Values.tolerations }}
  273. tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
  274. {{- end }}
  275. {{- if .Values.provisioning.nodeSelector }}
  276. nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.provisioning.nodeSelector "context" $) | nindent 8 }}
  277. {{- end }}
  278. volumes:
  279. {{- if .Values.provisioning.enabled }}
  280. - name: minio-provisioning
  281. configMap:
  282. name: {{ $fullname }}
  283. {{- end }}
  284. {{- if .Values.auth.useCredentialsFiles }}
  285. - name: minio-credentials
  286. secret:
  287. secretName: {{ include "minio.secretName" . }}
  288. {{- end }}
  289. {{- range $idx, $userSecret := .Values.provisioning.usersExistingSecrets }}
  290. - name: {{ printf "users-secret-%d" $idx }}
  291. secret:
  292. secretName: {{ $userSecret }}
  293. {{- end }}
  294. {{- if .Values.tls.enabled }}
  295. - name: minio-certs
  296. secret:
  297. secretName: {{ include "minio.tlsSecretName" . }}
  298. items:
  299. - key: tls.crt
  300. path: public.crt
  301. - key: tls.key
  302. path: private.key
  303. - key: ca.crt
  304. path: CAs/public.crt
  305. - name: minio-client-certs
  306. secret:
  307. secretName: {{ include "minio.tlsSecretName" . }}
  308. items:
  309. - key: ca.crt
  310. path: CAs/public.crt
  311. {{- end }}
  312. {{- if .Values.provisioning.extraVolumes }}
  313. {{- include "common.tplvalues.render" (dict "value" .Values.provisioning.extraVolumes "context" $) | nindent 8 }}
  314. {{- end }}
  315. {{- end }}