{{- if eq .Values.architecture "high-availability" }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "influxdb.fullname" . }}-relay
  labels: 
    {{- include "influxdb.labels" . | nindent 4 }}
    app.kubernetes.io/component: relay
spec:
  replicas: {{ .Values.relay.replicaCount }}
  strategy:
    type: {{ .Values.relay.updateStrategy }}
    {{- if (eq "Recreate" .Values.relay.updateStrategy) }}
    rollingUpdate: null
    {{- end }}
  selector:
    matchLabels: 
      {{- include "influxdb.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: relay
  template:
    metadata:
      labels: 
        {{- include "influxdb.labels" . | nindent 8 }}
        app.kubernetes.io/component: relay
    spec:
{{- include "influxdb.imagePullSecrets" . | nindent 6 }}
      affinity:
        {{- if eq .Values.relay.antiAffinity "hard" }}
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - topologyKey: "kubernetes.io/hostname"
              labelSelector:
                matchLabels: 
                  {{- include "influxdb.matchLabels" . | nindent 18 }}
                  app.kubernetes.io/component: relay
        {{- else if eq .Values.relay.antiAffinity "soft" }}
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 1
              podAffinityTerm:
                topologyKey: kubernetes.io/hostname
                labelSelector:
                  matchLabels: 
                    {{- include "influxdb.matchLabels" . | nindent 20 }}
                    app.kubernetes.io/component: relay
        {{- end }}
        {{- if .Values.relay.nodeAffinity }}
        nodeAffinity: {{- toYaml .Values.relay.nodeAffinity | nindent 10 }}
        {{- end }}
      {{- if .Values.relay.nodeSelector }}
      nodeSelector: {{- include "influxdb.tplValue" (dict "value" .Values.relay.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.relay.tolerations }}
      tolerations: {{- include "influxdb.tplValue" (dict "value" .Values.relay.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.relay.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.relay.securityContext.fsGroup }}
      {{- end }}
      containers:
        - name: relay
          image: {{ include "influxdb.relay.image" . }}
          imagePullPolicy: {{ .Values.relay.image.pullPolicy | quote }}
          {{- if .Values.relay.securityContext.enabled }}
          securityContext:
            runAsUser: {{ .Values.relay.securityContext.runAsUser }}
          {{- end }}
          command:
            - /influxdb_relay
          args:
            - -config
            - /conf/relay.toml
          ports:
            - name: http
              containerPort: {{ .Values.relay.containerPorts.http }}
              protocol: TCP
          {{- if .Values.relay.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: http
            initialDelaySeconds: {{ .Values.relay.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.relay.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.relay.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.relay.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.relay.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.relay.readinessProbe.enabled }}
          readinessProbe:
            tcpSocket:
              port: http
            initialDelaySeconds: {{ .Values.relay.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.relay.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.relay.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.relay.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.relay.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.relay.resources }}
          resources: {{- toYaml .Values.relay.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: relay-config
              mountPath: /conf
      volumes:
        - name: relay-config
          configMap:
            name: {{ include "influxdb.relay.configmapName" . }}
{{- end }}
