apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-git-backend-deployment
  labels:
    app.kubernetes.io/component: git-backend
    app.kubernetes.io/instance: {{ .Release.Name }}
    chart: {{ template "gerrit-replica.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 4 }}
    {{- end }}
spec:
  replicas: {{ .Values.gitBackend.replicas }}
  strategy:
    rollingUpdate:
      maxSurge: {{ .Values.gitBackend.maxSurge }}
      maxUnavailable: {{ .Values.gitBackend.maxUnavailable }}
  selector:
    matchLabels:
      app.kubernetes.io/component: git-backend
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/component: git-backend
        app.kubernetes.io/instance: {{ .Release.Name }}
        chart: {{ template "gerrit-replica.chart" . }}
        heritage: {{ .Release.Service }}
        release: {{ .Release.Name }}
        {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 8 }}
        {{- end }}
        {{- if .Values.gitBackend.additionalPodLabels }}
{{ toYaml .Values.gitBackend.additionalPodLabels  | indent 8 }}
        {{- end }}
      annotations:
        chartRevision: "{{ .Release.Revision }}"
    spec:
      {{- with .Values.gitBackend.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.gitBackend.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.gitBackend.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.gitBackend.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        fsGroup: 100
      {{ if .Values.images.registry.ImagePullSecret.name -}}
      imagePullSecrets:
      - name: {{ .Values.images.registry.ImagePullSecret.name }}
      {{- range .Values.images.additionalImagePullSecrets }}
      - name: {{ . }}
      {{- end }}
      {{- end }}
      initContainers:
      {{- if and .Values.nfsWorkaround.enabled .Values.nfsWorkaround.chownOnStartup }}
      - name: nfs-init
        image: {{ .Values.images.busybox.registry -}}/busybox:{{- .Values.images.busybox.tag }}
        command:
        - sh
        - -c
        args:
        - |
          chown 1000:100 /var/mnt/logs
          chown 1000:100 /var/mnt/git
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        volumeMounts:
        - name: logs
          subPathExpr: "gerrit-replica/$(POD_NAME)"
          mountPath: "/var/mnt/logs"
        - name: git-repositories
          mountPath: "/var/mnt/git"
        {{- if .Values.nfsWorkaround.idDomain }}
        - name: nfs-config
          mountPath: "/etc/idmapd.conf"
          subPath: idmapd.conf
        {{- end }}
      {{- end }}
      containers:
      - name: apache-git-http-backend
        imagePullPolicy: {{ .Values.images.imagePullPolicy }}
        image: {{ template "registry" . }}{{ .Values.gitBackend.image }}:{{ .Values.images.version }}
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        ports:
        - name: http-port
          containerPort: 80
        resources:
{{ toYaml .Values.gitBackend.resources | indent 10 }}
        livenessProbe:
          tcpSocket:
            port: http-port
{{ toYaml .Values.gitBackend.livenessProbe | indent 10 }}
        readinessProbe:
          tcpSocket:
            port: http-port
{{ toYaml .Values.gitBackend.readinessProbe | indent 10 }}
        volumeMounts:
        - name: git-repositories
          mountPath: "/var/gerrit/git"
        - name: logs
          subPathExpr: "apache-git-http-backend/$(POD_NAME)"
          mountPath: "/var/log/apache2"
        {{- if and .Values.nfsWorkaround.enabled .Values.nfsWorkaround.idDomain }}
        - name: nfs-config
          mountPath: "/etc/idmapd.conf"
          subPath: idmapd.conf
        {{- end }}
        - name: git-backend-secret
          readOnly: true
          subPath: .htpasswd
          mountPath: "/var/apache/credentials/.htpasswd"
      {{ if .Values.istio.enabled -}}
      - name: istio-proxy
        image: auto
        lifecycle:
          preStop:
            exec:
              command:
              - "/bin/sh"
              - "-c"
              - "while [ $(netstat -plunt | grep tcp | grep -v envoy | wc -l | xargs) -ne 0 ]; do sleep 1; done"
      {{- end }}
      volumes:
      - name: git-repositories
        persistentVolumeClaim:
          {{- if .Values.gitRepositoryStorage.externalPVC.use }}
          claimName: {{ .Values.gitRepositoryStorage.externalPVC.name }}
          {{- else }}
          claimName: {{ .Release.Name }}-git-repositories-pvc
          {{- end }}
      - name: git-backend-secret
        secret:
          secretName: {{ .Release.Name }}-git-backend-secret
      - name: logs
        {{ if .Values.logStorage.enabled -}}
        persistentVolumeClaim:
          {{- if .Values.logStorage.externalPVC.use }}
          claimName: {{ .Values.logStorage.externalPVC.name }}
          {{- else }}
          claimName: {{ .Release.Name }}-log-pvc
          {{- end }}
        {{ else -}}
        emptyDir: {}
        {{- end }}
      {{- if and .Values.nfsWorkaround.enabled .Values.nfsWorkaround.idDomain }}
      - name: nfs-config
        configMap:
          name: {{ .Release.Name }}-nfs-configmap
      {{- end }}
