apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-%s" $.Release.Name "secrets"  }}
  namespace: {{ $.Release.Namespace }}
  annotations:
    "helm.sh/resource-policy": "keep"
type: Opaque
stringData:
  ## Secrets Change when the release is upgraded
  ## https://github.com/helm/helm-www/issues/1259
  ## This is a workaround to keep the secrets unchanged
  {{- if .Release.IsUpgrade }}

  {{- if .Values.oneuptimeSecret }}
  oneuptime-secret: {{ .Values.oneuptimeSecret | quote }}
  {{- else }}
  oneuptime-secret: {{ index (lookup "v1" "Secret" $.Release.Namespace (printf "%s-secrets" $.Release.Name)).data "oneuptime-secret" | b64dec }}
  {{- end }}
  {{- if .Values.encryptionSecret }}
  encryption-secret: {{ .Values.encryptionSecret | quote }}
  {{- else }}
  encryption-secret: {{ index (lookup "v1" "Secret" $.Release.Namespace (printf "%s-secrets" $.Release.Name)).data "encryption-secret" | b64dec }}
  {{- end }}

  {{- range $key, $val := $.Values.probes }}
  {{- if (index (lookup "v1" "Secret" $.Release.Namespace (printf "%s-secrets" $.Release.Name)).data (printf "probe-%s" $key)) }}
  {{printf "probe-%s" $key}}: {{ (index (lookup "v1" "Secret" $.Release.Namespace (printf "%s-secrets" $.Release.Name)).data (printf "probe-%s" $key) | b64dec) }}
  {{ else }}
  {{printf "probe-%s" $key}}: {{ randAlphaNum 32 | quote }}
  {{- end }}
  {{- end }}

  {{ else }} # install operation

  {{- if .Values.oneuptimeSecret }}
  oneuptime-secret: {{ .Values.oneuptimeSecret | quote }}
  {{- else }}
  oneuptime-secret: {{ randAlphaNum 32 | quote }}
  {{- end }}
  {{- if .Values.encryptionSecret }}
  encryption-secret: {{ .Values.encryptionSecret | quote }}
  {{- else }}
  encryption-secret: {{ randAlphaNum 32 | quote }}
  {{- end }}

  {{- range $key, $val := $.Values.probes }}
  {{printf "probe-%s" $key}}: {{ randAlphaNum 32 | quote }}
  {{- end }}

  {{ end }}

---

{{- if .Values.redis.enabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ .Release.Name }}-redis
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ .Release.Name }}-redis
    app.kubernetes.io/part-of: oneuptime
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: redis
    appname: oneuptime
  annotations:
    "helm.sh/resource-policy": "keep"
type: Opaque
stringData:
  {{- if .Release.IsUpgrade }}
  {{- if .Values.redis.auth.password }}
  redis-password: {{ .Values.redis.auth.password | quote }}
  {{- else }}
  {{- if (index (lookup "v1" "Secret" $.Release.Namespace (printf "%s-redis" $.Release.Name)).data "redis-password") }}
  redis-password: {{ index (lookup "v1" "Secret" $.Release.Namespace (printf "%s-redis" $.Release.Name)).data "redis-password" | b64dec }}
  {{- else }}
  redis-password: {{ randAlphaNum 32 | quote }}
  {{- end }}
  {{- end }}
  {{- else }}
  {{- if .Values.redis.auth.password }}
  redis-password: {{ .Values.redis.auth.password | quote }}
  {{- else }}
  redis-password: {{ randAlphaNum 32 | quote }}
  {{- end }}
  {{- end }}
{{- end }}

---

{{- if not $.Values.postgresql.enabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-%s" $.Release.Name "external-postgres"  }}
  annotations:
    "helm.sh/resource-policy": "keep"
type: Opaque
stringData:

  key: "postgres"

  {{- if $.Values.externalPostgres.password }}
  ## Add secret here for externalPostgresPassword
  password: {{ $.Values.externalPostgres.password | quote }}
  {{- end }}

  {{- if $.Values.externalPostgres.ssl.enabled -}}
  ## Add secret here for externalPostgresPassword
  {{- if $.Values.externalPostgres.ssl.ca }}
  ssl-ca: {{ printf "%s" $.Values.externalPostgres.ssl.ca | quote }}
  {{- end }}

  {{- if $.Values.externalPostgres.ssl.cert }}
  ssl-cert: {{ printf "%s" $.Values.externalPostgres.ssl.cert | quote }}
  {{- end }}

  {{- if $.Values.externalPostgres.ssl.key }}
  ssl-key: {{ printf "%s" $.Values.externalPostgres.ssl.key | quote }}
  {{- end }}

  {{- end -}}

{{- end }}
---


## External Redis Configuration
{{- if not $.Values.redis.enabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-%s" $.Release.Name "external-redis"  }}
  annotations:
    "helm.sh/resource-policy": "keep"
type: Opaque
stringData:

  key: "redis"
  {{- if $.Values.externalRedis.password }}
  ## Add secret here for externalRedisPassword
  password: {{ $.Values.externalRedis.password | quote }}
  {{- end }}

  {{- if $.Values.externalRedis.tls.enabled -}}

  ## Add secret here for externalRedisPassword
  {{- if $.Values.externalRedis.tls.ca }}
  tls-ca: {{ printf "%s" $.Values.externalRedis.tls.ca | quote }}
  {{- end }}


  {{- if $.Values.externalRedis.tls.cert }}
  tls-cert: {{ printf "%s" $.Values.externalRedis.tls.cert | quote }}
  {{- end }}

  {{- if $.Values.externalRedis.tls.key }}
  tls-key: {{ printf "%s" $.Values.externalRedis.tls.key | quote }}
  {{- end }}

  {{- end -}}

{{- end }}
---

{{- if .Values.postgresql.enabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ .Release.Name }}-postgresql
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ .Release.Name }}-postgresql
    app.kubernetes.io/part-of: oneuptime
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: postgresql
    appname: oneuptime
  annotations:
    "helm.sh/resource-policy": "keep"
type: Opaque
stringData:
  {{- if .Release.IsUpgrade }}
  {{- if .Values.postgresql.auth.postgresPassword }}
  postgres-password: {{ .Values.postgresql.auth.postgresPassword | quote }}
  {{- else if .Values.postgresql.auth.password }}
  postgres-password: {{ .Values.postgresql.auth.password | quote }}
  {{- else }}
  {{- if (index (lookup "v1" "Secret" $.Release.Namespace (printf "%s-postgresql" $.Release.Name)).data "postgres-password") }}
  postgres-password: {{ index (lookup "v1" "Secret" $.Release.Namespace (printf "%s-postgresql" $.Release.Name)).data "postgres-password" | b64dec }}
  {{- else }}
  postgres-password: {{ randAlphaNum 32 | quote }}
  {{- end }}
  {{- end }}
  {{- else }}
  {{- if .Values.postgresql.auth.postgresPassword }}
  postgres-password: {{ .Values.postgresql.auth.postgresPassword | quote }}
  {{- else if .Values.postgresql.auth.password }}
  postgres-password: {{ .Values.postgresql.auth.password | quote }}
  {{- else }}
  postgres-password: {{ randAlphaNum 32 | quote }}
  {{- end }}
  {{- end }}
{{- end }}

---

{{- if .Values.clickhouse.enabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ .Release.Name }}-clickhouse
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: clickhouse
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/component: clickhouse
    app.kubernetes.io/part-of: clickhouse
    app.kubernetes.io/managed-by: {{ .Release.Service }}
  annotations:
    "helm.sh/resource-policy": "keep"
type: Opaque
stringData:
  {{- if .Release.IsUpgrade }}
  {{- if .Values.clickhouse.auth.password }}
  admin-password: {{ .Values.clickhouse.auth.password | quote }}
  {{- else }}
  {{- if (index (lookup "v1" "Secret" $.Release.Namespace (printf "%s-clickhouse" $.Release.Name)).data "admin-password") }}
  admin-password: {{ index (lookup "v1" "Secret" $.Release.Namespace (printf "%s-clickhouse" $.Release.Name)).data "admin-password" | b64dec }}
  {{- else }}
  admin-password: {{ randAlphaNum 32 | quote }}
  {{- end }}
  {{- end }}
  {{- else }}
  {{- if .Values.clickhouse.auth.password }}
  admin-password: {{ .Values.clickhouse.auth.password | quote }}
  {{- else }}
  admin-password: {{ randAlphaNum 32 | quote }}
  {{- end }}
  {{- end }}
{{- end }}

---


# Clickhouse Configuration
{{- if not $.Values.clickhouse.enabled }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ printf "%s-%s" $.Release.Name "external-clickhouse"  }}
  annotations:
    "helm.sh/resource-policy": "keep"
type: Opaque
stringData:
  key: "clickhouse"

  {{- if $.Values.externalClickhouse.password }}
  ## Add secret here for clickhousePassword
  password: {{ $.Values.externalClickhouse.password | quote }}
  {{- end }}

  ## Add TLS secret here for clickhouse
  {{- if $.Values.externalClickhouse.tls.enabled -}}
  {{- if $.Values.externalClickhouse.tls.ca }}
  tls-ca: {{ printf "%s" $.Values.externalClickhouse.tls.ca | quote }}
  {{- end }}

  {{- if $.Values.externalClickhouse.tls.cert }}
  tls-cert: {{ printf "%s" $.Values.externalClickhouse.tls.cert | quote }}
  {{- end }}

  {{- if $.Values.externalClickhouse.tls.key }}
  tls-key: {{ printf "%s" $.Values.externalClickhouse.tls.key | quote }}
  {{- end }}

  {{- end -}}

{{- end }}
---
  
  