{{- if and .Values.logStorage.enabled .Values.logStorage.cleanup.enabled }}
apiVersion: batch/v1
kind: CronJob
metadata:
  name: {{ .Release.Name }}-log-cleaner
  labels:
    app.kubernetes.io/component: log-cleaner
    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.logStorage.cleanup.schedule | quote }}
  concurrencyPolicy: "Forbid"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app.kubernetes.io/component: log-cleaner
            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.logStorage.cleanup.additionalPodLabels }}
{{ toYaml .Values.logStorage.cleanup.additionalPodLabels  | indent 12 }}
            {{- end }}
        {{ if .Values.istio.enabled -}}
          annotations:
            sidecar.istio.io/inject: "false"
        {{- end }}
        spec:
          restartPolicy: OnFailure
          containers:
          - name: log-cleaner
            imagePullPolicy: {{ .Values.images.imagePullPolicy }}
            image: {{ .Values.images.busybox.registry -}}/busybox:{{- .Values.images.busybox.tag }}
            command:
            - sh
            - -c
            args:
            - |
              find /var/logs/ \
                -mindepth 1 \
                -type f \
                -mtime +{{ .Values.logStorage.cleanup.retentionDays }} \
                -print \
                -delete
              find /var/logs/ -type d -empty -delete
            resources:
{{ toYaml .Values.logStorage.cleanup.resources | indent 14 }}
            volumeMounts:
            - name: logs
              mountPath: "/var/logs"
          volumes:
          - name: logs
            persistentVolumeClaim:
              {{- if .Values.logStorage.externalPVC.use }}
              claimName: {{ .Values.logStorage.externalPVC.name }}
              {{- else }}
              claimName: {{ .Release.Name }}-log-pvc
              {{- end }}
{{- end }}
