_helpers.tpl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. {{/* vim: set filetype=mustache: */}}
  2. {{/*
  3. Return the proper Redis image name
  4. */}}
  5. {{- define "redis.image" -}}
  6. {{ include "common.images.image" (dict "imageRoot" .Values.image "global" .Values.global) }}
  7. {{- end -}}
  8. {{/*
  9. Return the proper Redis Sentinel image name
  10. */}}
  11. {{- define "redis.sentinel.image" -}}
  12. {{ include "common.images.image" (dict "imageRoot" .Values.sentinel.image "global" .Values.global) }}
  13. {{- end -}}
  14. {{/*
  15. Return the proper image name (for the metrics image)
  16. */}}
  17. {{- define "redis.metrics.image" -}}
  18. {{ include "common.images.image" (dict "imageRoot" .Values.metrics.image "global" .Values.global) }}
  19. {{- end -}}
  20. {{/*
  21. Return the proper image name (for the init container volume-permissions image)
  22. */}}
  23. {{- define "redis.volumePermissions.image" -}}
  24. {{ include "common.images.image" (dict "imageRoot" .Values.volumePermissions.image "global" .Values.global) }}
  25. {{- end -}}
  26. {{/*
  27. Return sysctl image
  28. */}}
  29. {{- define "redis.sysctl.image" -}}
  30. {{ include "common.images.image" (dict "imageRoot" .Values.sysctl.image "global" .Values.global) }}
  31. {{- end -}}
  32. {{/*
  33. Return the proper Docker Image Registry Secret Names
  34. */}}
  35. {{- define "redis.imagePullSecrets" -}}
  36. {{- include "common.images.pullSecrets" (dict "images" (list .Values.image .Values.sentinel.image .Values.metrics.image .Values.volumePermissions.image .Values.sysctl.image) "global" .Values.global) -}}
  37. {{- end -}}
  38. {{/*
  39. Return the appropriate apiVersion for networkpolicy.
  40. */}}
  41. {{- define "networkPolicy.apiVersion" -}}
  42. {{- if semverCompare ">=1.4-0, <1.7-0" .Capabilities.KubeVersion.GitVersion -}}
  43. {{- print "extensions/v1beta1" -}}
  44. {{- else -}}
  45. {{- print "networking.k8s.io/v1" -}}
  46. {{- end -}}
  47. {{- end -}}
  48. {{/*
  49. Return the appropriate apiGroup for PodSecurityPolicy.
  50. */}}
  51. {{- define "podSecurityPolicy.apiGroup" -}}
  52. {{- if semverCompare ">=1.14-0" .Capabilities.KubeVersion.GitVersion -}}
  53. {{- print "policy" -}}
  54. {{- else -}}
  55. {{- print "extensions" -}}
  56. {{- end -}}
  57. {{- end -}}
  58. {{/*
  59. Return true if a TLS secret object should be created
  60. */}}
  61. {{- define "redis.createTlsSecret" -}}
  62. {{- if and .Values.tls.enabled .Values.tls.autoGenerated (and (not .Values.tls.existingSecret) (not .Values.tls.certificatesSecret)) }}
  63. {{- true -}}
  64. {{- end -}}
  65. {{- end -}}
  66. {{/*
  67. Return the secret containing Redis TLS certificates
  68. */}}
  69. {{- define "redis.tlsSecretName" -}}
  70. {{- $secretName := coalesce .Values.tls.existingSecret .Values.tls.certificatesSecret -}}
  71. {{- if $secretName -}}
  72. {{- printf "%s" (tpl $secretName $) -}}
  73. {{- else -}}
  74. {{- printf "%s-crt" (include "common.names.fullname" .) -}}
  75. {{- end -}}
  76. {{- end -}}
  77. {{/*
  78. Return the path to the cert file.
  79. */}}
  80. {{- define "redis.tlsCert" -}}
  81. {{- if (include "redis.createTlsSecret" . ) -}}
  82. {{- printf "/opt/bitnami/redis/certs/%s" "tls.crt" -}}
  83. {{- else -}}
  84. {{- required "Certificate filename is required when TLS in enabled" .Values.tls.certFilename | printf "/opt/bitnami/redis/certs/%s" -}}
  85. {{- end -}}
  86. {{- end -}}
  87. {{/*
  88. Return the path to the cert key file.
  89. */}}
  90. {{- define "redis.tlsCertKey" -}}
  91. {{- if (include "redis.createTlsSecret" . ) -}}
  92. {{- printf "/opt/bitnami/redis/certs/%s" "tls.key" -}}
  93. {{- else -}}
  94. {{- required "Certificate Key filename is required when TLS in enabled" .Values.tls.certKeyFilename | printf "/opt/bitnami/redis/certs/%s" -}}
  95. {{- end -}}
  96. {{- end -}}
  97. {{/*
  98. Return the path to the CA cert file.
  99. */}}
  100. {{- define "redis.tlsCACert" -}}
  101. {{- if (include "redis.createTlsSecret" . ) -}}
  102. {{- printf "/opt/bitnami/redis/certs/%s" "ca.crt" -}}
  103. {{- else -}}
  104. {{- required "Certificate CA filename is required when TLS in enabled" .Values.tls.certCAFilename | printf "/opt/bitnami/redis/certs/%s" -}}
  105. {{- end -}}
  106. {{- end -}}
  107. {{/*
  108. Return the path to the DH params file.
  109. */}}
  110. {{- define "redis.tlsDHParams" -}}
  111. {{- if .Values.tls.dhParamsFilename -}}
  112. {{- printf "/opt/bitnami/redis/certs/%s" .Values.tls.dhParamsFilename -}}
  113. {{- end -}}
  114. {{- end -}}
  115. {{/*
  116. Create the name of the shared service account to use
  117. */}}
  118. {{- define "redis.serviceAccountName" -}}
  119. {{- if .Values.serviceAccount.create -}}
  120. {{ default (include "common.names.fullname" .) .Values.serviceAccount.name }}
  121. {{- else -}}
  122. {{ default "default" .Values.serviceAccount.name }}
  123. {{- end -}}
  124. {{- end -}}
  125. {{/*
  126. Create the name of the master service account to use
  127. */}}
  128. {{- define "redis.masterServiceAccountName" -}}
  129. {{- if .Values.master.serviceAccount.create -}}
  130. {{ default (printf "%s-master" (include "common.names.fullname" .)) .Values.master.serviceAccount.name }}
  131. {{- else -}}
  132. {{- if .Values.serviceAccount.create -}}
  133. {{ template "redis.serviceAccountName" . }}
  134. {{- else -}}
  135. {{ default "default" .Values.master.serviceAccount.name }}
  136. {{- end -}}
  137. {{- end -}}
  138. {{- end -}}
  139. {{/*
  140. Create the name of the replicas service account to use
  141. */}}
  142. {{- define "redis.replicaServiceAccountName" -}}
  143. {{- if .Values.replica.serviceAccount.create -}}
  144. {{ default (printf "%s-replica" (include "common.names.fullname" .)) .Values.replica.serviceAccount.name }}
  145. {{- else -}}
  146. {{- if .Values.serviceAccount.create -}}
  147. {{ template "redis.serviceAccountName" . }}
  148. {{- else -}}
  149. {{ default "default" .Values.replica.serviceAccount.name }}
  150. {{- end -}}
  151. {{- end -}}
  152. {{- end -}}
  153. {{/*
  154. Return the configuration configmap name
  155. */}}
  156. {{- define "redis.configmapName" -}}
  157. {{- if .Values.existingConfigmap -}}
  158. {{- printf "%s" (tpl .Values.existingConfigmap $) -}}
  159. {{- else -}}
  160. {{- printf "%s-configuration" (include "common.names.fullname" .) -}}
  161. {{- end -}}
  162. {{- end -}}
  163. {{/*
  164. Return true if a configmap object should be created
  165. */}}
  166. {{- define "redis.createConfigmap" -}}
  167. {{- if empty .Values.existingConfigmap }}
  168. {{- true -}}
  169. {{- end -}}
  170. {{- end -}}
  171. {{/*
  172. Get the password secret.
  173. */}}
  174. {{- define "redis.secretName" -}}
  175. {{- if .Values.auth.existingSecret -}}
  176. {{- printf "%s" (tpl .Values.auth.existingSecret $) -}}
  177. {{- else -}}
  178. {{- printf "%s" (include "common.names.fullname" .) -}}
  179. {{- end -}}
  180. {{- end -}}
  181. {{/*
  182. Get the password key to be retrieved from Redis&reg; secret.
  183. */}}
  184. {{- define "redis.secretPasswordKey" -}}
  185. {{- if and .Values.auth.existingSecret .Values.auth.existingSecretPasswordKey -}}
  186. {{- printf "%s" .Values.auth.existingSecretPasswordKey -}}
  187. {{- else -}}
  188. {{- printf "redis-password" -}}
  189. {{- end -}}
  190. {{- end -}}
  191. {{/*
  192. Returns the available value for certain key in an existing secret (if it exists),
  193. otherwise it generates a random value.
  194. */}}
  195. {{- define "getValueFromSecret" }}
  196. {{- $len := (default 16 .Length) | int -}}
  197. {{- $obj := (lookup "v1" "Secret" .Namespace .Name).data -}}
  198. {{- if $obj }}
  199. {{- index $obj .Key | b64dec -}}
  200. {{- else -}}
  201. {{- randAlphaNum $len -}}
  202. {{- end -}}
  203. {{- end }}
  204. {{/*
  205. Return Redis&reg; password
  206. */}}
  207. {{- define "redis.password" -}}
  208. {{- if or .Values.auth.enabled .Values.global.redis.password }}
  209. {{- if not (empty .Values.global.redis.password) }}
  210. {{- .Values.global.redis.password -}}
  211. {{- else if not (empty .Values.auth.password) -}}
  212. {{- .Values.auth.password -}}
  213. {{- else -}}
  214. {{- include "getValueFromSecret" (dict "Namespace" .Release.Namespace "Name" (include "redis.secretName" .) "Length" 10 "Key" (include "redis.secretPasswordKey" .)) -}}
  215. {{- end -}}
  216. {{- end -}}
  217. {{- end }}
  218. {{/* Check if there are rolling tags in the images */}}
  219. {{- define "redis.checkRollingTags" -}}
  220. {{- include "common.warnings.rollingTag" .Values.image }}
  221. {{- include "common.warnings.rollingTag" .Values.sentinel.image }}
  222. {{- include "common.warnings.rollingTag" .Values.metrics.image }}
  223. {{- end -}}
  224. {{/*
  225. Compile all warnings into a single message, and call fail.
  226. */}}
  227. {{- define "redis.validateValues" -}}
  228. {{- $messages := list -}}
  229. {{- $messages := append $messages (include "redis.validateValues.topologySpreadConstraints" .) -}}
  230. {{- $messages := append $messages (include "redis.validateValues.architecture" .) -}}
  231. {{- $messages := append $messages (include "redis.validateValues.podSecurityPolicy.create" .) -}}
  232. {{- $messages := append $messages (include "redis.validateValues.tls" .) -}}
  233. {{- $messages := without $messages "" -}}
  234. {{- $message := join "\n" $messages -}}
  235. {{- if $message -}}
  236. {{- printf "\nVALUES VALIDATION:\n%s" $message | fail -}}
  237. {{- end -}}
  238. {{- end -}}
  239. {{/* Validate values of Redis&reg; - spreadConstrainsts K8s version */}}
  240. {{- define "redis.validateValues.topologySpreadConstraints" -}}
  241. {{- if and (semverCompare "<1.16-0" .Capabilities.KubeVersion.GitVersion) .Values.replica.topologySpreadConstraints -}}
  242. redis: topologySpreadConstraints
  243. Pod Topology Spread Constraints are only available on K8s >= 1.16
  244. Find more information at https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/
  245. {{- end -}}
  246. {{- end -}}
  247. {{/* Validate values of Redis&reg; - must provide a valid architecture */}}
  248. {{- define "redis.validateValues.architecture" -}}
  249. {{- if and (ne .Values.architecture "standalone") (ne .Values.architecture "replication") -}}
  250. redis: architecture
  251. Invalid architecture selected. Valid values are "standalone" and
  252. "replication". Please set a valid architecture (--set architecture="xxxx")
  253. {{- end -}}
  254. {{- if and .Values.sentinel.enabled (not (eq .Values.architecture "replication")) }}
  255. redis: architecture
  256. Using redis sentinel on standalone mode is not supported.
  257. To deploy redis sentinel, please select the "replication" mode
  258. (--set "architecture=replication,sentinel.enabled=true")
  259. {{- end -}}
  260. {{- end -}}
  261. {{/* Validate values of Redis&reg; - PodSecurityPolicy create */}}
  262. {{- define "redis.validateValues.podSecurityPolicy.create" -}}
  263. {{- if and .Values.podSecurityPolicy.create (not .Values.podSecurityPolicy.enabled) }}
  264. redis: podSecurityPolicy.create
  265. In order to create PodSecurityPolicy, you also need to enable
  266. podSecurityPolicy.enabled field
  267. {{- end -}}
  268. {{- end -}}
  269. {{/* Validate values of Redis&reg; - TLS enabled */}}
  270. {{- define "redis.validateValues.tls" -}}
  271. {{- if and .Values.tls.enabled (not .Values.tls.autoGenerated) (not .Values.tls.existingSecret) (not .Values.tls.certificatesSecret) }}
  272. redis: tls.enabled
  273. In order to enable TLS, you also need to provide
  274. an existing secret containing the TLS certificates or
  275. enable auto-generated certificates.
  276. {{- end -}}
  277. {{- end -}}
  278. {{/* Define the suffix utilized for external-dns */}}
  279. {{- define "redis.externalDNS.suffix" -}}
  280. {{ printf "%s.%s" (include "common.names.fullname" .) .Values.useExternalDNS.suffix }}
  281. {{- end -}}
  282. {{/* Compile all annotations utilized for external-dns */}}
  283. {{- define "redis.externalDNS.annotations" -}}
  284. {{- if and .Values.useExternalDNS.enabled .Values.useExternalDNS.annotationKey }}
  285. {{ .Values.useExternalDNS.annotationKey }}hostname: {{ include "redis.externalDNS.suffix" . }}
  286. {{- range $key, $val := .Values.useExternalDNS.additionalAnnotations }}
  287. {{ $.Values.useExternalDNS.annotationKey }}{{ $key }}: {{ $val | quote }}
  288. {{- end }}
  289. {{- end }}
  290. {{- end }}