{{- if eq .Values.architecture "high-availability" }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}-relay
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: relay
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.relay.replicaCount }}
  {{- if .Values.relay.updateStrategy }}
  strategy: {{- toYaml .Values.relay.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: relay
  template:
    metadata:
      {{- if .Values.relay.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.relay.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: relay
        {{- if .Values.relay.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.relay.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "influxdb.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.relay.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.relay.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.relay.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.relay.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.relay.podAffinityPreset "component" "relay" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.relay.podAntiAffinityPreset "component" "relay" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.relay.nodeAffinityPreset.type "key" .Values.relay.nodeAffinityPreset.key "values" .Values.relay.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.relay.schedulerName }}
      schedulerName: {{ .Values.relay.schedulerName }}
      {{- end }}
      {{- if .Values.relay.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.relay.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      priorityClassName: {{ .Values.relay.priorityClassName | quote }}
      {{- if .Values.relay.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.relay.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.relay.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.relay.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.relay.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.relay.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.relay.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.relay.initContainers "context" $) | trim | nindent 8 }}
      {{- end }}
      containers:
        - name: relay
          image: {{ include "influxdb.relay.image" . }}
          imagePullPolicy: {{ .Values.relay.image.pullPolicy | quote }}
          {{- if .Values.relay.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.relay.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else }}
          {{- if .Values.relay.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.relay.command "context" $) | nindent 12 }}
          {{- else}}
          command:
            - /influxdb-relay
          {{- end }}
          {{- if .Values.relay.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.relay.args "context" $) | nindent 12 }}
          {{- else}}
          args:
            - -config
            - /conf/relay.toml
          {{- end }}
          {{- if .Values.relay.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.relay.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          env:
            {{- if .Values.relay.extraEnvVars }}
              {{- include "common.tplvalues.render" (dict "value" .Values.relay.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.relay.extraEnvVarsCM }}
            - configMapRef:
                name: {{ .Values.relay.extraEnvVarsCM }}
            {{- end }}
            {{- if .Values.relay.extraEnvVarsSecret }}
            - secretRef:
                name: {{ .Values.relay.extraEnvVarsSecret }}
            {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.relay.containerPorts.http }}
              protocol: TCP
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.relay.startupProbe.enabled }}
          startupProbe: {{- omit .Values.relay.startupProbe "enabled" | toYaml | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.relay.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.relay.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.relay.livenessProbe.enabled }}
          livenessProbe: {{- omit .Values.relay.livenessProbe "enabled" | toYaml | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.relay.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.relay.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.relay.readinessProbe.enabled }}
          readinessProbe: {{- omit .Values.relay.readinessProbe "enabled" | toYaml | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.relay.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.relay.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.relay.resources }}
          resources: {{- toYaml .Values.relay.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: relay-config
              mountPath: /conf
          {{- if .Values.relay.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.relay.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.relay.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.relay.sidecars "context" $) | trim | nindent 8 }}
        {{- end }}
      volumes:
        - name: relay-config
          configMap:
            name: {{ include "influxdb.relay.configmapName" . }}
      {{- if .Values.relay.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.relay.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
{{- end }}
