apiVersion: batch/v1
kind: CronJob
metadata:
  name:  {{ .Release.Name }}-git-gc
  labels:
    app.kubernetes.io/component: git-gc
    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:
  schedule: {{ .Values.gitGC.schedule | quote }}
  concurrencyPolicy: "Forbid"
  jobTemplate:
    spec:
      template:
        metadata:
          annotations:
            cluster-autoscaler.kubernetes.io/safe-to-evict: "false"
          {{ if .Values.istio.enabled }}
            sidecar.istio.io/inject: "false"
          {{- end }}
          labels:
            app.kubernetes.io/component: git-gc
            app.kubernetes.io/instance: {{ .Release.Name }}
            chart: {{ template "gerrit-replica.chart" . }}
            heritage: {{ .Release.Service }}
            release: {{ .Release.Name }}
            {{- if .Values.additionalLabels }}
{{ toYaml .Values.additionalLabels  | indent 12 }}
            {{- end }}
            {{- if .Values.gitGC.additionalPodLabels }}
{{ toYaml .Values.gitGC.additionalPodLabels  | indent 12 }}
            {{- end }}
        spec:
          {{- with .Values.gitGC.tolerations }}
          tolerations:
            {{- toYaml . | nindent 10 }}
          {{- end }}
          {{- with .Values.gitGC.nodeSelector }}
          nodeSelector:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.gitGC.affinity }}
          affinity:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          restartPolicy: OnFailure
          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: "git-gc/$(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: git-gc
            imagePullPolicy: {{ .Values.images.imagePullPolicy }}
            image: {{ template "registry" . }}{{ .Values.gitGC.image }}:{{ .Values.images.version }}
            resources:
{{ toYaml .Values.gitGC.resources | indent 14 }}
            env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            volumeMounts:
            - name: git-repositories
              mountPath: "/var/gerrit/git"
            - name: logs
              subPathExpr: "git-gc/$(POD_NAME)"
              mountPath: "/var/log/git"
            {{- if and .Values.nfsWorkaround.enabled .Values.nfsWorkaround.idDomain }}
            - name: nfs-config
              mountPath: "/etc/idmapd.conf"
              subPath: idmapd.conf
            {{- 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: 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 }}
