apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: {{ template "vault.fullname" . }}
  labels:
    app: {{ template "vault.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  annotations:
{{ toYaml .Values.annotations | indent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: {{ template "vault.name" . }}
        release: {{ .Release.Name }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
    spec:
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        {{- if .Values.vault.dev }}
        command: ["vault", "server", "-dev", "-dev-listen-address", "[::]:8200"]
        {{- else }}
        command: ["vault", "server", "-config", "/vault/config/config.json"]
        {{- end }}
        {{- if .Values.lifecycle }}
        lifecycle:
{{ tpl .Values.lifecycle . | indent 10 }}
        {{- end }}
        ports:
        - containerPort: {{ .Values.service.port }}
          name: api
        - containerPort: 8201
          name: cluster-address
        livenessProbe:
          # Alive if it is listening for clustering traffic
          tcpSocket:
            port: {{ .Values.service.port }}
        readinessProbe:
          # Ready depends on preference
          httpGet:
            path: /v1/sys/health?
              {{- if .Values.vault.readiness.readyIfSealed -}}sealedcode=204&{{- end }}
              {{- if .Values.vault.readiness.readyIfStandby -}}standbycode=204&{{- end }}
              {{- if .Values.vault.readiness.readyIfUninitialized -}}uninitcode=204&{{- end }}
            port: {{ .Values.service.port }}
            scheme: {{ if .Values.vault.config.listener.tcp.tls_disable -}}HTTP{{- else -}}HTTPS{{- end }}
        securityContext:
          readOnlyRootFilesystem: true
          capabilities:
            add:
            - IPC_LOCK
        env:
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
          - name: VAULT_CLUSTER_ADDR
            value: "https://$(POD_IP):8201"
        {{- if .Values.vault.extraEnv }}
{{ toYaml .Values.vault.extraEnv | indent 10 }}
        {{- end }}
        volumeMounts:
        - name: vault-config
          mountPath: /vault/config/
        - name: vault-root
          mountPath: /root/
        {{- range .Values.vault.customSecrets }}
        - name: {{ .secretName }}
          mountPath: {{ .mountPath }}
        {{- end }}
{{- if .Values.vault.extraContainers }}
{{ toYaml .Values.vault.extraContainers | indent 6}}
{{- end }}
        resources:
{{ toYaml .Values.resources | indent 10 }}
      {{- if .Values.affinity }}
      {{- if .Values.consulAgent.join }}
      - name: {{ .Chart.Name }}-consul-agent
        image: "{{ .Values.consulAgent.repository }}:{{ .Values.consulAgent.tag }}"
        imagePullPolicy: {{ .Values.consulAgent.pullPolicy }}
        securityContext:
          readOnlyRootFilesystem: true
        volumeMounts:
        - name: consul-data
          mountPath: /etc/consul
        {{- if .Values.consulAgent.gossipKeySecretName }}
        - name: consul-gossip-key
          mountPath: /etc/consul/secrets
          readOnly: true
        {{- end }}
        command:
          - "/bin/sh"
          - "-ec"
          - |
            {{- if .Values.consulAgent.gossipKeySecretName }}
            if [ -e /etc/consul/secrets/gossip-key ]; then
              echo "{\"encrypt\": \"$(base64 /etc/consul/secrets/gossip-key)\"}" > /etc/consul/encrypt.json
              GOSSIP_KEY="-config-file /etc/consul/encrypt.json"
            fi
            {{- end }}

            exec /bin/consul agent \
              $GOSSIP_KEY \
              -join={{- .Values.consulAgent.join }} \
              -data-dir=/etc/consul
      {{- end }}
      affinity:
{{ tpl .Values.affinity . | indent 8 }}
      {{- end }}
      volumes:
        - name: vault-config
          configMap:
            name: "{{ template "vault.fullname" . }}-config"
        - name: vault-root
          emptyDir: {}
        {{- range .Values.vault.customSecrets }}
        - name: {{ .secretName }}
          secret:
            secretName: {{ .secretName }}
        {{- end }}
{{- if .Values.vault.extraVolumes }}
{{ toYaml .Values.vault.extraVolumes | indent 8}}
{{- end }}
        {{- if .Values.consulAgent.join }}
        - name: consul-data
          emptyDir: {}
        {{- end }}
        {{- if .Values.consulAgent.gossipKeySecretName }}
        - name: consul-gossip-key
          secret:
            secretName: {{ .Values.consulAgent.gossipKeySecretName }}
        {{- end }}
      {{- if .Values.imagePullSecret }}
      imagePullSecrets:
        - name: {{ .Values.imagePullSecret }}
      {{- end }}
