{{- $root := . -}}

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Release.Name }}-gerrit-stateful-set
  labels:
    app.kubernetes.io/component: gerrit
    app.kubernetes.io/instance: {{ .Release.Name }}
    chart: {{ template "gerrit.chart" . }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
    {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 4 }}
    {{- end }}
spec:
  serviceName: {{ .Release.Name }}-gerrit-service
  replicas: {{ .Values.gerrit.replicas }}
  updateStrategy:
    rollingUpdate:
      partition: {{ .Values.gerrit.updatePartition }}
  selector:
    matchLabels:
      app.kubernetes.io/component: gerrit
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/component: gerrit
        app.kubernetes.io/instance: {{ .Release.Name }}
        chart: {{ template "gerrit.chart" . }}
        heritage: {{ .Release.Service }}
        release: {{ .Release.Name }}
        {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 8 }}
        {{- end }}
        {{- if .Values.gerrit.additionalPodLabels }}
{{ toYaml .Values.gerrit.additionalPodLabels  | indent 8 }}
        {{- end }}
      annotations:
        chartRevision: "{{ .Release.Revision }}"
        {{- if .Values.gerrit.additionalAnnotations }}
{{ toYaml .Values.gerrit.additionalAnnotations  | indent 8 }}
        {{- end }}
    spec:
      {{- with .Values.gerrit.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.gerrit.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.gerrit.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.gerrit.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.gerrit.priorityClassName }}
      priorityClassName: {{ . }}
      {{- end }}
      terminationGracePeriodSeconds: {{ .Values.gerrit.gracefulStopTimeout }}
      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 }}
      - name: gerrit-init
        image: {{ template "registry" . }}{{ .Values.gerrit.images.gerritInit }}:{{ .Values.images.version }}
        imagePullPolicy: {{ .Values.images.imagePullPolicy }}
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        volumeMounts:
        - name: gerrit-site
          mountPath: "/var/gerrit"
        - name: git-repositories
          mountPath: "/var/mnt/git"
        - name: logs
          subPathExpr: "gerrit/$(POD_NAME)"
          mountPath: "/var/mnt/logs"
        - name: gerrit-init-config
          mountPath: "/var/config/gerrit-init.yaml"
          subPath: gerrit-init.yaml
        {{- if and .Values.nfsWorkaround.enabled .Values.nfsWorkaround.idDomain }}
        - name: nfs-config
          mountPath: "/etc/idmapd.conf"
          subPath: idmapd.conf
        {{- end }}
        {{- if and .Values.gerrit.pluginManagement.cache.enabled }}
        - name: gerrit-plugin-cache
          mountPath: "/var/mnt/plugins"
        {{- end }}
        {{ if eq .Values.gerrit.index.type "elasticsearch" -}}
        - name: gerrit-index-config
          mountPath: "/var/mnt/index"
        {{- end }}
        - name: gerrit-config
          mountPath: "/var/mnt/etc/config"
        - name: gerrit-secure-config
          mountPath: "/var/mnt/etc/secret"
        {{ if .Values.caCert -}}
        - name: tls-ca
          subPath: ca.crt
          mountPath: "/var/config/ca.crt"
        {{- end }}
        {{- range .Values.gerrit.additionalConfigMaps }}
        - name: {{ .name }}
          mountPath: "/var/mnt/data/{{ .subDir }}"
        {{- end }}
      containers:
      - name: gerrit
        image: {{ template "registry" . }}{{ .Values.gerrit.images.gerrit }}:{{ .Values.images.version }}
        imagePullPolicy: {{ .Values.images.imagePullPolicy }}
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        lifecycle:
          preStop:
            exec:
              command:
                - "/bin/ash"
                - "-c"
                - "kill -2 $(pidof java) && tail --pid=$(pidof java) -f /dev/null"
        ports:
        - name: gerrit-port
          containerPort: 8080
        {{- if .Values.gerrit.service.ssh.enabled }}
        - name: gerrit-ssh
          containerPort: 29418
        {{- end }}
        volumeMounts:
        - name: gerrit-site
          mountPath: "/var/gerrit"
        - name: git-repositories
          mountPath: "/var/mnt/git"
        - name: logs
          subPathExpr: "gerrit/$(POD_NAME)"
          mountPath: "/var/mnt/logs"
        {{- if and .Values.nfsWorkaround.enabled .Values.nfsWorkaround.idDomain }}
        - name: nfs-config
          mountPath: "/etc/idmapd.conf"
          subPath: idmapd.conf
        {{- end }}
        {{ if eq .Values.gerrit.index.type "elasticsearch" -}}
        - name: gerrit-index-config
          mountPath: "/var/mnt/index"
        {{- end }}
        - name: gerrit-config
          mountPath: "/var/mnt/etc/config"
        - name: gerrit-secure-config
          mountPath: "/var/mnt/etc/secret"
        {{- range .Values.gerrit.additionalConfigMaps }}
        - name: {{ .name }}
          mountPath: "/var/mnt/data/{{ .subDir }}"
        {{- end }}
        resources:
{{ toYaml .Values.gerrit.resources | indent 10 }}
        livenessProbe:
          httpGet:
            path: /config/server/healthcheck~status
            port: gerrit-port
{{ toYaml .Values.gerrit.livenessProbe | indent 10 }}
        readinessProbe:
          httpGet:
            path: /config/server/healthcheck~status
            port: gerrit-port
{{ toYaml .Values.gerrit.readinessProbe | indent 10 }}
        startupProbe:
          httpGet:
            path: /config/server/healthcheck~status
            port: gerrit-port
{{ toYaml .Values.gerrit.startupProbe | indent 10 }}
      volumes:
      {{ if not .Values.gerrit.persistence.enabled -}}
      - name: gerrit-site
        emptyDir: {}
      {{- end }}
      {{- if and .Values.gerrit.pluginManagement.cache.enabled }}
      - name: gerrit-plugin-cache
        persistentVolumeClaim:
          claimName: {{ .Release.Name }}-plugin-cache-pvc
      {{- end }}
      - name: git-repositories
        persistentVolumeClaim:
          {{- if .Values.gitRepositoryStorage.externalPVC.use }}
          claimName: {{ .Values.gitRepositoryStorage.externalPVC.name }}
          {{- else }}
          claimName: {{ .Release.Name }}-git-repositories-pvc
          {{- end }}
      - 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 }}
      - name: gerrit-init-config
        configMap:
          name: {{ .Release.Name }}-gerrit-init-configmap
      {{ if eq .Values.gerrit.index.type "elasticsearch" -}}
      - name: gerrit-index-config
        persistentVolumeClaim:
          claimName: {{ .Release.Name }}-gerrit-index-config-pvc
      {{- end }}
      - name: gerrit-config
        configMap:
          name: {{ .Release.Name }}-gerrit-configmap
      - name: gerrit-secure-config
        secret:
          secretName: {{ .Release.Name }}-gerrit-secure-config
      {{ if .Values.caCert -}}
      - name: tls-ca
        secret:
          secretName: {{ .Release.Name }}-tls-ca
      {{- end }}
      {{- range .Values.gerrit.additionalConfigMaps }}
      - name: {{ .name }}
        configMap:
          name: {{ if .data }}{{ $root.Release.Name }}-{{ .name }}{{ else }}{{ .name }}{{ end }}
      {{- end }}
      {{- if and .Values.nfsWorkaround.enabled .Values.nfsWorkaround.idDomain }}
      - name: nfs-config
        configMap:
          name: {{ .Release.Name }}-nfs-configmap
      {{- end }}
  {{ if .Values.gerrit.persistence.enabled -}}
  volumeClaimTemplates:
  - metadata:
      name: gerrit-site
      labels:
        app.kubernetes.io/component: gerrit
        app.kubernetes.io/instance: {{ .Release.Name }}
        chart: {{ template "gerrit.chart" . }}
        heritage: {{ .Release.Service }}
        release: {{ .Release.Name }}
        {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 8 }}
        {{- end }}
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: {{ .Values.gerrit.persistence.size }}
      storageClassName: {{ .Values.storageClasses.default.name }}
  {{- end }}
