# helm_charts_secret-config.yaml
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "mattermost-team-edition.fullname" . }}-config-json
  labels:
    app.kubernetes.io/name: {{ include "mattermost-team-edition.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    helm.sh/chart: {{ include "mattermost-team-edition.chart" . }}
type: Opaque
data:
  {{- /* Make a deep enough copy of the default config */}}
  {{- $c := dict }}
  {{- range $key, $dictVal := .Values.configJSON }}
  {{- $dictCopy := merge (dict) $dictVal }}
  {{- $_ := set $c $key $dictCopy }}
  {{- end }}

  {{- /* Update the copied default config based on .Values */}}

  {{- if or .Values.configJSON.SqlSettings.DriverName .Values.configJSON.SqlSettings.DataSource }}
  {{- $message := "Use 'mysql' or 'externalDB' to instead of using a configuration with:\n\nconfigJSON:\n  SqlSettings\n    DriverName: ...\n    DataSource: ..." }}
  {{- print "\n\nDIRECT CONFIGURATION NOT SUPPORTED:\n-----------------------------------\n\n" $message | fail }}
  {{- end }}

  {{- if .Values.externalDB.enabled }}
  {{- $_ := set $c.SqlSettings "DriverName" (.Values.externalDB.externalDriverType) }}
  {{- $_ := set $c.SqlSettings "DataSource" (.Values.externalDB.externalConnectionString) }}
  {{- else }}
  {{- $_ := set $c.SqlSettings "DriverName" "mysql" }}
  {{- $_ := set $c.SqlSettings "DataSource" (print .Values.mysql.mysqlUser ":" .Values.mysql.mysqlPassword "@tcp(" .Release.Name "-mysql:3306)/" .Values.mysql.mysqlDatabase "?charset=utf8mb4,utf8&readTimeout=30s&writeTimeout=30s") }}
  {{- end }}

  {{- $_ := set $c.SqlSettings "AtRestEncryptKey" (.Values.configJSON.SqlSettings.AtRestEncryptKey | default (randAlphaNum 32)) }}
  {{- $_ := set $c.FileSettings "PublicLinkSalt" (.Values.configJSON.FileSettings.PublicLinkSalt | default (randAlphaNum 32)) }}
  {{- $_ := set $c.EmailSettings "InviteSalt" (.Values.configJSON.EmailSettings.InviteSalt | default (randAlphaNum 32)) }}

  {{- /* Render the processed config as a JSON string */}}
  {{- /* NOTE: Mounted at /mattermost/config/config.json on the mattermost pod */}}
  config.json: {{ $c | toJson | b64enc }}