{{/*
Common name helpers
*/}}
{{- define "vantage.fullname" -}}
{{ .Release.Name }}
{{- end -}}

{{- define "vantage.labels" -}}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end -}}

{{/*
vantage.server.env renders the server container's environment.

It lives here because two workloads need it identically: the Deployment and the
pre-upgrade migration Job. A Job that connected to a different database than the
pods it migrates for would be worse than no Job at all, so there is one copy and
both read it.
*/}}
{{- define "vantage.server.env" -}}
- name: MONGO_URI
  {{- $mongoUri := tpl .Values.server.env.mongoUri . }}
  {{- if and (not .Values.mongo.enabled) (contains (printf "%s-mongo" .Release.Name) $mongoUri) }}
  {{- fail "mongo.enabled is false, so server.env.mongoUri must point at an external MongoDB rather than the in-chart one" }}
  {{- end }}
  value: {{ $mongoUri | quote }}
- name: REDIS_ADDR
  {{- if .Values.redis.enabled }}
  value: "{{ .Release.Name }}-redis:{{ .Values.redis.port }}"
  {{- else }}
  {{- if not .Values.redis.addr }}
  {{- fail "redis.enabled is false, so redis.addr must be set to an external Redis host:port" }}
  {{- end }}
  value: {{ .Values.redis.addr | quote }}
  {{- end }}
{{- if .Values.redis.auth.existingSecret }}
- name: REDIS_USERNAME
  valueFrom:
    secretKeyRef:
      name: {{ .Values.redis.auth.existingSecret }}
      key: {{ .Values.redis.auth.usernameKey }}
      optional: true
- name: REDIS_PASSWORD
  valueFrom:
    secretKeyRef:
      name: {{ .Values.redis.auth.existingSecret }}
      key: {{ .Values.redis.auth.passwordKey }}
{{- else }}
{{- if .Values.redis.auth.username }}
- name: REDIS_USERNAME
  value: {{ .Values.redis.auth.username | quote }}
{{- end }}
{{- if .Values.redis.auth.password }}
- name: REDIS_PASSWORD
  value: {{ .Values.redis.auth.password | quote }}
{{- end }}
{{- end }}
- name: GRPC_HOST
  value: {{ .Values.server.env.grpcHost | quote }}
- name: GRPC_PORT
  value: {{ .Values.server.service.grpcPort | quote }}
- name: HTTP_PORT
  value: {{ .Values.server.service.httpPort | quote }}
- name: KEY_ENCRYPTION_KEY
  value: {{ .Values.server.env.keyEncryptionKey | quote }}
- name: GUACD_ADDR
  value: "{{ .Release.Name }}-guacd:{{ .Values.guacd.service.port }}"
- name: APP_ROOT_LABEL
  value: {{ .Values.server.env.appRootLabel | quote }}
- name: PROXY_ADVERTISE_HOST
  value: {{ .Values.server.env.proxyAdvertiseHost | quote }}
- name: PROXY_LISTEN_HOST
  value: {{ .Values.server.env.proxyListenHost | quote }}
{{- if eq .Values.server.env.deploymentType "cloud" }}
- name: VANTAGE_DEPLOYMENT
  value: "cloud"
- name: FREE_INSTANCE_REAP_AFTER
  value: {{ .Values.server.env.freeInstanceReapAfter | quote }}
{{- end }}
- name: POD_IP
  valueFrom:
    fieldRef:
      fieldPath: status.podIP
{{- end -}}
