apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "stolon.fullname" . }}-proxy
  labels:
    app: {{ template "stolon.name" . }}
    chart: {{ template "stolon.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.proxy.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "stolon.name" . }}
      release: {{ .Release.Name }}
      component: stolon-proxy
  template:
    metadata:
      labels:
        app: {{ template "stolon.name" . }}
        release: {{ .Release.Name }}
        component: stolon-proxy
        stolon-cluster: {{ template "stolon.fullname" . }}
      annotations:
{{- with .Values.proxy.annotations }}
{{ toYaml . | indent 8 }}
{{- end }}
    spec:
{{- if .Values.proxy.priorityClassName }}
      priorityClassName: "{{ .Values.proxy.priorityClassName }}"
{{- end }}
      serviceAccountName: {{ template "stolon.serviceAccountName" . }}
{{- if .Values.image.pullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.image.pullSecrets | indent 8 }}
{{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - "/bin/bash"
            - "-ec"
            - |
              exec gosu stolon stolon-proxy
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: STPROXY_CLUSTER_NAME
              value: {{ template "stolon.clusterName" . }}
            - name: STPROXY_STORE_BACKEND
              value: {{ .Values.store.backend | quote}}
            {{- if eq .Values.store.backend "kubernetes" }}
            - name: STPROXY_KUBE_RESOURCE_KIND
              value: {{ .Values.store.kubeResourceKind  | quote}}
            {{- else }}
            - name: STPROXY_STORE_ENDPOINTS
              value: {{ .Values.store.endpoints | quote}}
            {{- end }}
            - name: STPROXY_LISTEN_ADDRESS
              value: {{ default "0.0.0.0" .Values.proxy.listenAddress | quote }}
            - name: STPROXY_METRICS_LISTEN_ADDRESS
              value: "0.0.0.0:{{ .Values.ports.metrics.containerPort }}"
            - name: STPROXY_DEBUG
              value: {{ .Values.debug | quote}}
{{- if .Values.proxy.extraEnv }}
{{ toYaml .Values.proxy.extraEnv | indent 12 }}
{{- end }}
          ports:
{{- range $key, $value := .Values.ports }}
            - name: {{ $key }}
{{ toYaml $value | indent 14 }}
{{- end }}
          resources:
{{ toYaml .Values.proxy.resources | indent 12 }}
          readinessProbe:
            tcpSocket:
              port: {{ .Values.ports.stolon.containerPort }}
            initialDelaySeconds: 10
            timeoutSeconds: 5
{{- with .Values.proxy.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.proxy.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.proxy.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
{{- end }}
